2

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));
}
NewbieCoder
  • 676
  • 1
  • 9
  • 32

2 Answers2

2

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

Refer here

Community
  • 1
  • 1
NewbieCoder
  • 676
  • 1
  • 9
  • 32
1

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)
   }
 }
crafter
  • 6,246
  • 1
  • 34
  • 46