0

in my site if email is registered in my database I would add a error

$this->addError('email' ,'This Email already registered');

but in Update form I do not want see this error

What is a simple way to solve my problem?

this is my users model:

<?php

/**
 * This is the model class for table "users".

class Users extends CActiveRecord
{
    //   public $captcha; 
    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return 'users';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('username, email,password', 'required'),
            array('roles_id', 'numerical', 'integerOnly'=>true),
            array('username, password', 
                      'length',
                      'max'=>255,
                      'min'=>4
                      ),
                   array('email', 'comp_email'),
               array('username', 'comp_username'),

            array('DataCreated, LastUpdated', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, username, password, DataCreated, LastUpdated, roles_id', 'safe', 'on'=>'search'),

        );
    }

    /**
     * @return array relational rules.
     */


    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'id' => 'ID',
                  'email'=>'Email',
            'username' => 'Username',
            'password' => 'Password',
            'DataCreated' => 'Data Created',
            'LastUpdated' => 'Last Updated',
            'roles_id' => 'Roles',
        );
    }


    public function search()
    {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id',$this->id);
        $criteria->compare('username',$this->username,true);
        $criteria->compare('password',$this->password,true);
        $criteria->compare('DataCreated',$this->DataCreated,true);
        $criteria->compare('LastUpdated',$this->LastUpdated,true);
        $criteria->compare('roles_id',$this->roles_id);

        return new CActiveDataProvider($this, array(
            'criteria'=>$criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return Users the static model class
     */
    public static function model($className=__CLASS__)
    {
        return parent::model($className);
    }




          public function comp_username($attributes , $params)


                {
               $username = Yii::app()->db->createCommand()
                      ->select('username')
                      ->from('users')
                      ->queryAll();

                   $y = (count($username));
            for ($x=0;$x<$y;$x++)
            {
                $usernameE[$x] = $username[$x]['username'];
            }


          foreach ($usernameE as $u)
          {
              if($this->username == $u)
              {
                  $this->addError('username' ,'This Username already registered');
                  break;

              }
          }
      }




             public function comp_email($attributes , $params)
      {
               $email = Yii::app()->db->createCommand()
                      ->select('email')
                      ->from('users')
                      ->queryAll();

                   $y = (count($email));
            for ($x=0;$x<$y;$x++)
            {
                $emailE[$x] = $email[$x]['email'];
            }


          foreach ($emailE as $u)
          {
              if($this->email == $u)
              {
                  $this->addError('email' ,'This Email already registered');
                  break;

              }
          }
      }


      public function getUsernameEmail($id)
      {
             $emailUsername = Yii::app()->db->createCommand()
                      ->select('*')
                      ->from('users')
                     ->where('id=:id', array(':id'=>$id))
                      ->queryAll();

              return $emailUsername;


      }
}

and this is my action Update in my controller:

public function actionUpdate($id)
    {
        $model=$this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
         $this->performAjaxValidation($model);

        if(isset($_POST['Users']))
        {
            $model->attributes=$_POST['Users'];
                  $id=$model->id;
                $useremail =  Users::model()->getUsernameEmail($id);

             $useremailX= $useremail[0]['username'];
            $model->username=$useremailX;
             $useremailX= $useremail[0]['email'];
            $model->email=$useremailX;

                 $model->password=  crypt($model->password,'salt');
        if($model->save())
            $this->redirect(array('view','id'=>$model->id));
    }

    $this->render('update',array(
        'model'=>$model,
    ));
}
Saltern
  • 1,305
  • 2
  • 16
  • 42

2 Answers2

1

You can achieve this by applying specific scenarios to your rules.

The Yii WIKI topic on the subject is a good reference.

In your rules, you can specify which scenarios to apply the rule to.

array('email', 'unique','message'=>'Email already exists!', 'on'=>'insert')

Please note that Yii automatically injects specific scenarios, depending on how the object is created.

  • insert
  • update
  • search

You can specify your own custom scenario.

$model = Customer::model()->findByPK($customerID);
$model->scenario = 'purchase';
crafter
  • 6,246
  • 1
  • 34
  • 46
0

this is how to validate, you can set error message to be empty.

public function rules()
{
    // NOTE: you should only define rules for those attributes that
    // will receive user inputs.
    return array(
            //First parameter is your field name of table which has email value
        array('email', 'email','message'=>"The email isn't correct"),
        array('email', 'unique','message'=>'Email already exists!'),            
    );
}

https://stackoverflow.com/a/12778419/1727357

or you can make your own validator:

public function uniqueEmail($attribute, $params)
{
     // Set $emailExist variable true or false by using your custom query on checking in database table if email exist or not.
    // You can user $this->{$attribute} to get attribute value.

     $emailExist = true;

     if($emailExist)
     {

        //do what your want
        $this->addError('email','Email already exists');
     }
}

User this validation method in rules:

array('email', 'uniqueEmail','message'=>'Email already exists!'),    
Community
  • 1
  • 1
YaakovHatam
  • 2,314
  • 2
  • 22
  • 40
  • no! i can add error in create account for user. i want Do not add error for that user in update page. – Saltern Jun 29 '14 at 06:28