Yii Framework Forum: About Setter Validation Question - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

About Setter Validation Question Rate Topic: -----

#1 User is offline   spzgy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 21-April 13

Posted 26 April 2013 - 08:36 PM

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.
0

#2 User is offline   artificial 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 96
  • Joined: 11-December 10
  • Location:Sydney, Australia

Posted 26 April 2013 - 10:41 PM

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.yiiframew...et-set-methods/
Alex Berriman
www.fishvision.com
0

#3 User is offline   spzgy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 5
  • Joined: 21-April 13

Posted 27 April 2013 - 11:03 AM

View PostArtificial, on 26 April 2013 - 10:41 PM, said:

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.yiiframew...et-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),
			#......
		);
	}

0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users