Php 唯一验证在YII的更新案例中不起作用?

Php 唯一验证在YII的更新案例中不起作用?,php,validation,yii,modal-dialog,unique,Php,Validation,Yii,Modal Dialog,Unique,我已对员工代码应用了唯一验证。这是工作良好的添加电磁脉冲情况。但我不明白为什么它在更新时不起作用,以下是我用于此的代码: 我的规则方法是: public function rules() { return array( array('is_active', 'numerical', 'integerOnly'=>true), array('first_name, last_name, employee_code, username, passwor

我已对员工代码应用了唯一验证。这是工作良好的添加电磁脉冲情况。但我不明白为什么它在更新时不起作用,以下是我用于此的代码:

我的规则方法是:

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'),
    );

请有人指出我错在哪里。请帮帮我。

看起来,字段“员工代码”的值放入表单字段,您正在尝试保存它。它不是唯一的,也不是保存

无论如何,你可以:

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

在更新操作中

您还可以在验证规则中删除场景:

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

当您更新记录时,它已经存在于数据库中,并且您要验证的值也已经存在。这就是为什么在进行更新时,应将要更新的记录从检查中排除的原因。否则检查将失败,并显示如下消息

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'),
),
员工代码已获取某些值

例如,带有条件的新规则

array(
    'employee_code',
    'unique',
    'criteria' => array(
        'condition' => 'user_id != :id',
        'params' => array(':id' => $this->user_id),
    ),
    'on' => array('create', 'update'),
),
我假设user_id是主键

在形式上,您可能希望有不同的更新和创建场景,因为您不需要对创建场景进行任何附加检查。单个场景也可以,但您可能需要这样做

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'),
),

注意:如其中一条评论中所述,您可能希望使用insert而不是create,因为默认情况下,新创建模型的场景是insert。这是更常用的名称。

有插入、更新、搜索,没有创建场景。嗯,除非你手动指定。当你说它不适用于更新时,你是什么意思?这不是在验证吗?你有错误吗?你能显示你的控制器代码吗?