Yii2 - 验证比较表现奇怪

时间:2016-04-18 13:25:04

标签: yii2 yii2-validation

比较验证是动作奇怪 - 无论如何,电子邮件永远不会相同,并且始终显示错误。

这是验证代码:

public function rules()
    {
        return [

            [['first_name', 'last_name', 'email', 'conf_email'], 'required'],
            ['title', 'match', 'pattern' => '/^[a-zA-Z]{0,100}$/',
                'message' => 'Title must contain only letters.'],
            [['first_name', 'last_name'], 'match', 'pattern' => '/^[a-zA-Z]{0,45}$/', 
                'message' => 'The {attribute} must contain only letters.'],
            [['email', 'first_name', 'last_name'], 'trim'],
            [['email', 'conf_email'], 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'UniqueValidator'],
            ['conf_email', 'compare', 'compareAttribute'=>'email', 'skipOnEmpty' => false, 
                'message' => 'Emails do not match.'],
        ];
    }

表单的代码:

<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\Url;

$form = ActiveForm::begin([
        'action' => Url::toRoute([
            'team/ajax-add-team-member',
            'id'       => $team->idteam,
            'portalID' => $portal->idportal
        ]),
        'enableAjaxValidation' => true,
        'validationUrl'        => '/team/team-validation',
        'id'                   => 'team-invite-form',
        'options'              => [
            'class' => 'clearfix'
        ]
]) ?>

<div class="col-md-12">
    <div class="row">
        <div class="col-md-2 col-sm-2 col-xs-12">
            <?php echo $form->field($model, 'title')->textInput() ?>
        </div>
        <div class="col-md-5 col-sm-5 col-xs-12">
            <?php echo $form->field($model, 'first_name')->textInput() ?>
        </div>
        <div class="col-md-5 col-sm-5 col-xs-12">
            <?php echo $form->field($model, 'last_name')->textInput() ?>
        </div>
    </div>
</div>

<div class="col-md-12">
    <?php
        echo $form->field($model, 'email')->textInput();
        echo $form->field($model, 'conf_email')->textInput();
        echo $form->field($model, 'is_medical_professional')->checkbox();
    ?>
</div>

<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<?php
    echo Html::submitButton('Send', ['id' => 'add-team-member-form-btn', 'class' => 'btn btn-default']);
    ActiveForm::end();
?>

这里发生了什么o.O?

1 个答案:

答案 0 :(得分:0)

Thiis线看起来很奇怪:

['email', 'UniqueValidator'],

尝试使用['email', 'unique'],代替

如果这样做无济于事 - 请提供更多详情。你遇到了什么确切的错误?