1

I have a Yii form accept first name, last name and email from user. Using an add more link, users can add multiple rows of those three elements.

For email validation, unique and required are set in model rules and everything works fine. I am using JavaScript to create addition row on clicking add more link.

Problem

On the first row my values are John, Newman, johnnewman@gmail.com and the second row, i'm entering Mathew, Heyden, johnnewman@gmail.com. In this case email address is duplicated. None of the validation rules (require and unique) is capable of validating this. Can some one suggest a better method to validate this ?

Update: I created a custom validation function and i guess this is enough to solve my problem. Can someone tell me how to access the whole form data / post data in a custom validation function ?

public function uniqueOnForm($attribute){ 
            // This post data is not working
            error_log($_REQUEST, true);
            $this->addError($attribute, 'Sorry, email address shouldn\'t be repeated');
        }
Rohit Gaikwad
  • 817
  • 2
  • 8
  • 24
Tismon Varghese
  • 849
  • 1
  • 6
  • 17
  • You can use both JS to validate if those fields have the same value and then in controller you can validate them again by comparing them to each other. It will be better if you create a `beforeSave` function in your model. – Criesto Sep 10 '15 at 06:07

3 Answers3

1

You can try this:

<?php
public function rules()
{
    return array(
        array('first_name', 'checkUser')
    );
}

public function checkUser($attribute)
{
    if($this->first_name == $this->other_first_name){
         $this->addError($attribute, 'Please select another first name');
    }
}
?>

You can also look into this extension

Criesto
  • 1,985
  • 4
  • 32
  • 41
0

You can write custom validator:

//protected/extensions/validators
class UniqueMailValidator extends CValidator
{

    /**
     * @inheritdoc
     */
    protected function validateAttribute($object, $attribute)
    {
        $record = YourModel::model()->findAllByAttributes(array('email' => $object->$attribute));
        if ($record) {
            $object->addError($attribute, 'Email are exists in db.');
        }
    }
}

// in your model
public function rules()
{
    return array(
        array('email', 'ext.validators.UniqueMailValidator'),
    ...

Or better try to use THIS

Danila Ganchar
  • 10,266
  • 13
  • 49
  • 75
0
public function rules(){
    return array(
       //other rules
       array('email', 'validEmail'),
    )

}

public function validEmail($attribute, $params){
    if(!empty($this->email) && is_array($this->email)){
       $isduplicate = $this->isDuplicate($this->email);
       if($isduplicate){
           $this->addError('email', 'Email address must be unique!');
       }
    }
}

private function isDuplicate($arr){
    if(count(array_unique($arr)) < count($arr)){
        return true;
    }
    else {
        return false;
    }
}

because you are using tabular input (multiple row) , so make sure input field as an array. might be like this :

<?php echo $form->textField($model, 'email[]'); ?>