Validation model with compareAttribute

Hi folks,

I start my studies with Yii fw and did a activerecord model to test and I have some questions about validations and the models structure:

  • whenever I need do a compare validation (to create a user for exemple), I need create a safeAttributes function to put all my table attributes?

  • I aways need put the public variables on the top of my models to it working correctly?

  • I aways need to create a methods model() and tablename() in my models?

here is my actual model:




class Test extends CActiveRecord

{

  /**

   * Returns the static model of the specified AR class.

   * @return CActiveRecord the static model class

   */

  public static function model($className=__CLASS__)

  {

    return parent::model($className);

  }


  /**

   * @return string the associated database table name

   */

  public function tableName()

  {

    return 'Test';

  }

  

  public function safeAttributes()

  {

    return array('email_repeat, name, email');

  }

  public function rules()

  {

    return array( array('email', 'compare', 'compareAttribute' => 'email_repeat', 'on' => 'create'),

                  array('name, email, email_repeat', 'required'),

                  array('email', 'email') );

  }

  

}



and here is my controller:




  class TestController extends CController

  {

    public function actionCreate()

    {

      $test = new Test;

      $test->scenario = 'create';

      if(isset($_POST['Test'])) 

      {

        $test->attributes = $_POST['Test'];

        if($test->save()) $this->redirect(Yii::app()->user->returnUrl);

      }

      

      $this->render('test', array('test' => $test));

    }

  }



Sorry for my limited English.

Thanks.

  1. You can call parent::safeAttributes() to get the default safe attribute list, and then merge them with any additional attributes that are safe.

  2. You can declare either public member variables or properties via getters/setters for any additional attributes.

  3. Yes, model() method is required; tableName() is required too if the table name is different from the class name.

Qiang, thanks for you answer, but I do not understand how compare validations work in yii.

I know that compare two differents frameworks isn’t correct, but in rails to validate two fields with compare validations I just need put validates_confirmation_of :fieldname on my model.

Can you show me some exemple of a model with compare validation ?

Thanks.

Hi there,

i add on to explain more about the enquiries. let’s take your code for example.

Model:




class Test extends CActiveRecord

{

  public $name;

  public $email;

  public $email_repeat;


  public static function model($className=__CLASS__)

  {

    return parent::model($className);

  }


  public function tableName()

  {

    return 'Test';

  }

  

  public function safeAttributes()

  {

    return array(

        parent::safeAttributes,

        'create'=>'email_repeat, name, email',

        'login'=>'email, name',

    );

  }


  public function rules()

  {

    return array(

        array('email', 'compare', 'compareAttribute' => 'email_repeat', 'on' => 'create'),

        array('name, email, email_repeat', 'required'),

        array('email', 'email')

    );

  }

}



Controller:




  class TestController extends CController

  {

    public function actionCreate()

    {

      $test = new Test;

      $test->scenario = 'create';

      if(isset($_POST['Test'])) 

      {

        $test->attributes = $_POST['Test'];

        if($test->save()) $this->redirect(Yii::app()->user->returnUrl);

      }

      

      $this->render('test', array('test' => $test));

    }

  }



in this instance, test activerecord is used by two scenario ‘create’ and ‘login’. so in ‘create’ scenario, only the ‘email_repeat, name, email’ are required for creation. whereas in ‘login’ scenario, ‘email, name’ are required for login and the rest of the attributes are ignored.

for compare validation,

array(‘email’, ‘compare’, ‘compareAttribute’ => ‘email_repeat’, ‘on’ => ‘create’),

will compare email with email_repeat value in ‘create’ scenario only.

hopefully i answered your enquiries.

cheers.