1

Compare validation is action strange - no matter what, emails are never the same, and error is shown all the time.

This is the code for 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.'],
        ];
    }

And code for the form:

<?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();
?>

What is going on here o.O?

robsch
  • 9,358
  • 9
  • 63
  • 104
Sasha
  • 8,521
  • 23
  • 91
  • 174
  • 2
    I see you trim email but not conf_email, try to trim both or none – Gramotei Apr 19 '16 at 06:05
  • As Hutsi stated email should be of type unique (why does it have to be unique at all?). And are you sure with title, first and last name? The rules even don't allow spaces. So I suspect you have different type of errors but you think it is only caused be the compare rule. Or are you sure with the error? Which error do you get? – robsch Apr 19 '16 at 06:06
  • Can you show the complete model and the action code? – Alex Apr 19 '16 at 11:29

1 Answers1

0

Thiis line looks strange:

['email', 'UniqueValidator'],

Try using ['email', 'unique'], instead

If this will not help - provide more details. What exact error do you faced?

aderushev
  • 818
  • 8
  • 11