如何在yii2中验证


How to live validate from in yii2?

如何在不单击表单按钮的情况下实时验证表单?

型号:

public function rules() {
    return [
        ['CodeKargah', 'CodeKargah_check'],
        // ...
        // ...
        // ...
    ];
}
public function CodeKargah_check($attribute) {
    $zero = substr($this - > $attribute, 0, 1);
    $len = strlen((string)($this - > $attribute));
    if ($zero == '0' && $len == 10) {
        return null;
    }
    else {
        $this - > addError($attribute, Yii::t('app', 'First number must be zero'));
    }
} 

提交表单后,即可运行

我解决了:

public function rules() {
    return [
           ['CodeKargah', 'integer', 'min' => 100000000, 'tooSmall' => Yii::t('app', 'The value entered must be 10 digits.')],
           ['CodeKargah', 'match', 'pattern' => '/^0/'],
        // ...
        // ...
        // ...
    ];
}