About Setter Validation Question

my DB have a field “repayment_date”,it’s timestamp.

i want to PSOT a value "RepaymentDateText" to set the "repayment_date". the value is like this: 2013 -04-27

the getter part is OK




        public function getRepaymentDateText()

        {

                if (empty($this->repayment_date)) {

                        return null;

                } else {

                        return date('Y-m-d',$this->repayment_date);

                }

        }



setter part is written like this




        public function setRepaymentDateText($value)

        {

                if(empty($value) || !$value) return false;

                $this->repayment_date = strtotime($value.' 00:00:00');

        }



The GII default in Controller using $ Model-> attributes = $ _POST [’***’] This automatic filtering the POST over RepaymentDateText the value.




        public function actionUpdate($id)

        {

                $model=$this->loadModel($id);


                // Uncomment the following line if AJAX validation is needed

                // $this->performAjaxValidation($model);


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

                {

                        $model->attributes=$_POST['Blacklist'];

                        if($model->save())

                                $this->redirect(array('view','id'=>$model->id));

                }


                $this->render('update',array(

                        'model'=>$model,

                ));

        }



I can activate setRepaymentDateText in the model beforeSave use the following code, but this writing is not very appropriate.




$this->RepaymentDateText = $_POST['Blacklist']['RepaymentDateText'];



The first question: How to write better?

The second problem: $ _POST [‘blacklist’] [‘RepaymentDateText’] This value should be how to verify before more complex yii specification? Written in the rules ()? Please give me a sample code , thank you.

This code should not be in your data model:


$this->RepaymentDateText = $_POST['Blacklist']['RepaymentDateText'];

You could have an attribute within your model that stores the date within that string format, set it in your controller, and convert it in your beforeSave() method.

Try reading this: http://www.yiiframework.com/wiki/167/understanding-virtual-attributes-and-get-set-methods/

I found the answer, but still want to thank you for your help.




	public function rules()

	{

		return array(

			#......

			array('repayment_date_text', 'length', 'max'=>10),

			#......

		);

	}