【问题标题】:unique validation is not working in Update case in YII?唯一验证在 YII 的更新案例中不起作用?
【发布时间】:2013-08-21 07:52:21
【问题描述】:

我已对员工代码应用了唯一验证。它适用于 Add Emp 案例。但我不明白为什么它在更新的情况下不起作用,这是我用于此的代码:

我的规则方法是:

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

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

【问题讨论】:

  • Default scenario names 是insert,update,search,没有create 场景。好吧,除非你手动指定它。
  • 当你说它对更新不起作用时,你是什么意思?不是验证吗?你有错误吗?你能显示你的控制器代码吗?

标签: php validation yii modal-dialog unique


【解决方案1】:

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

无论如何,你可以:

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

【讨论】:

    【解决方案2】:

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

    员工代码“一些价值”已被占用。

    例如(带有条件的新规则)

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

    我假设user_id 是主键。

    您可能希望对update 和create 有不同的场景,因为您不需要对create 场景进行任何额外检查。单一场景也可以,但你可能想做这样的事情

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

    附:如其中一个 cmets 所述,您可能希望使用 insert 而不是 create,因为默认情况下,新创建模型的场景是 insert。这是比较常用的名称。

    【讨论】:

      猜你喜欢
      • 2021-08-07
      • 2020-10-02
      • 2016-09-11
      • 2017-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-04
      相关资源
      最近更新 更多