NewbieCoder
NewbieCoder

Reputation: 706

Yii, changing original posted attributes

I'd like to ask, is it possible to change the original posted attributes in actionCreate()?

For example I have 3 attributes: name, phNumber, address

In the _form.php, it automatically posts these 3 attributes. BUT what if I want to change the posted name attribute to all Uppercases? Do I need to create my own method of creating a record just to change how the name will be recorded OR is there something that I can do in actionCreate() so that it only changes the name attribute?

For example, user types in

adam michael

for the name textbox, and I want to change only this attribute to

ADAM MICHAEL

to be recorded in the database instead of having to create another method.

Code below:

public function actionCreate() {
    $model = new Masseuse;

    if (isset($_POST['Masseuse'])) {
        $model->setAttributes($_POST['Masseuse']);          

        if ($model->save()) {
            if (Yii::app()->getRequest()->getIsAjaxRequest())
                Yii::app()->end();
            else
                $this->redirect(array('servicemasseuse/create', 'mid' => $model->id));
        }  
    }

    $this->render('create', array( 'model' => $model));
}

Upvotes: 2

Views: 387

Answers (2)

crafter
crafter

Reputation: 6296

You must alter the user input prior to saving the data. You do this by creating an overwritten function in your model.

class Masseuse extends CActiveRecord {

  // ...

   public function beforeSave()
   {
      $this->name = strtoupper($this->name)
   }
 }

Upvotes: 1

NewbieCoder
NewbieCoder

Reputation: 706

Just simply do a $model->name=strtoupper($model->name);

Refer here

Upvotes: 2

Related Questions