在名称中包含模式后,必填字段不起作用 - Yii2

时间:2016-05-26 15:40:52

标签: php yii2 yii2-basic-app yii2-validation

我正在经历尴尬的局面。

required的{​​{1}}字段外,所有规则都正常工作。

first_name

并且,在将'FirstNameLength' => ['first_name', 'string', 'min' => 3, 'tooShort'=>'First name should contain atleast 3 character'], 'FirstNameTrim' => ['first_name', 'filter', 'filter' => 'trim'], 'FirstNameRequired' => ['first_name', 'required','message'=>'* First name is required'], 'FirstNameRequired' => ['first_name','match','pattern'=>"/^[a-zA-Z']+$/",'message' => '* First Name can only contain alphabets charater'], 纳入规则之后,它正在发生。如果我删除或评论pattern规则,则表明它正在运行。

pattern

我的动机是不允许除'FirstNameLength' => ['first_name', 'string', 'min' => 3, 'tooShort'=>'First name should contain atleast 3 character'], 'FirstNameTrim' => ['first_name', 'filter', 'filter' => 'trim'], 'FirstNameRequired' => ['first_name', 'required','message'=>'* First name is required'], //'FirstNameRequired' => ['first_name','match','pattern'=>"/^[a-zA-Z']+$/",'message' => '* First Name can only contain alphabets charater'], 以外的任何spacenumberspecial character。并且,这种模式规则可以正常工作。但是,唯一的问题是现在它没有采用'规则。

欢迎任何帮助/提示/建议。

1 个答案:

答案 0 :(得分:1)

似乎你正在重新定义/覆盖键FirstNameRequired,然后最后覆盖前一个..

我建议您使用常规表示法来验证规则(没有FirstNameRequired键

['first_name', 'required','message'=>'* First name is required'],
['first_name','match','pattern'=>"/^[a-zA-Z']+$/",'message' => '* First Name can only contain alphabets charater'],