2013-08-21 2 views
1

나는 Employee Code에 대해 고유 한 유효성 검증을 적용했습니다. 엠페셜 케이스를 추가해도 문제가 없습니다.YII의 업데이트 케이스에서 고유 유효성 검사가 작동하지 않습니까?

내 규칙 방법은 다음과 같습니다 : 내가 잘못 곳

public function rules() 
{ 

    return array(
     array('is_active', 'numerical', 'integerOnly'=>true), 
     array('first_name, last_name, employee_code, username, password, role,joining_date,', 'required','on'=>array('create','update')), 

     array('employee_code', 'numerical', 'integerOnly'=>true), 

     array('employee_code', 'unique','on'=>array('create','update')), 
     array('employee_code', 'length', 'min' => 4, 'max'=>4, 'message'=>Yii::t("translation", "{attribute} is too short.")), 
     array('username','email'),  
     array('username','valid_username','on'=>array('create')), 
     array('username', 'required','on'=>array('forgotPassword')), 

     array('currentPassword, newPassword, newPasswordRepeat', 'required','on'=>array('change')), 
     array('newPassword', 'compare', 'compareAttribute'=>'newPasswordRepeat','on'=>array('change'),'message'=>'New and Confirm Password do not match.'), 
     array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>array('forgotPassword')), 
     array('joining_date', 'safe'), 
     array('years', 'safe'), 
     array('user_id, first_name, last_name, employee_code, username, password, role, joining_date, pending_regular_leave, pending_medical_leave, allocated_regular_leave, allocated_medical_leave, is_active', 'safe', 'on'=>'search'), 
    ); 

사람이 식별 할 수 바랍니다하지만이 업데이트의 경우에는 작동하지 않는 이유는 점점 아니에요, 여기 내가이 사용했던 코드입니다. 도와주세요.

+2

[기본 시나리오 이름] (http://www.yiiframework.com/wiki/266/understanding-scenarios/#hh2)은'insert','update','search'이며'create' 시나리오는 없습니다 . 글쎄, 당신이 수동으로 지정하지 않는 한. –

+0

업데이트로 작동하지 않는다고 말씀 하셨다면 어떻게됩니까? 그것이 유효하지 않습니까? 오류가 있습니까? 컨트롤러 코드를 보여줄 수 있습니까? – Pitchinnate

답변

0

필드 "직원 코드"의 값이 양식 필드에 저장되어있는 것으로 보입니다. 그리고 그것은 독특하지 않으며, 그것은 저장되지 않습니다.

어쨌든, 다음을 수행 할 수 있습니다

$employee->validate(array('/* Here is list of attributes, except employee code */')); 

나 :

$employee->save(array('/* Here is list of attributes, except employee code */')); 

갱신 의 행동.

또한 유효성 검사 규칙에 시나리오를 삭제할 수 있습니다 : 당신이 데이터베이스에 이미 기록과 이미 검증하고자하는 값을 업데이트

array('employee_code', 'unique','on'=>array('insert'); 
0

도 존재한다. 업데이트를 수행 할 때 수표에서 업데이트 할 레코드를 제외해야하는 이유가 여기에 있습니다. 그렇지 않으면 수표는 다음과 같은 메시지와 함께 실패합니다.

직원 코드 "some value"가 이미 사용되었습니다.

예를 들어

(기준에 새 규칙)

array(
    'employee_code', 
    'unique', 
    'criteria' => array(
     'condition' => 'user_id != :id', 
     'params' => array(':id' => $this->user_id), 
    ), 
    'on' => array('create', 'update'), 
), 

나는 user_id 기본 키라고 가정한다.

create 시나리오에 대한 추가 검사가 필요하지 않기 때문에 공식적으로 updatecreate에 대해 다른 시나리오를 사용할 수 있습니다. 단일 시나리오는 효과가 있지만 이와 비슷한 작업을 수행 할 수도 있습니다.

array(
    'employee_code', 
    'unique', 
    'on' => array('create'), 
), 
array(
    'employee_code', 
    'unique', 
    'criteria' => array(
     'condition' => 'user_id != :id', 
     'params' => array(':id' => $this->user_id), 
    ), 
    'on' => array('update'), 
), 

P.S. 설명 중 하나에서 언급했듯이 기본적으로 새로 생성 된 모델의 시나리오는 insert이므로 create 대신 insert을 사용할 수 있습니다. 이것은 일반적으로 사용되는 이름입니다.

관련 문제