Validate Form Element Not In Model

Hello there.

I’m trying to validate a form wich has an element not present in the corresponding DB table.

So, I’ve added it in the model (public $myfield;) and in the validating rules.

But whatever I try, when doing $model->save(), I get an error: myfield is empty. And if I dump $model->attributes, I don’t see myfield.

What do I forget ?

u can check the that model class rules and attributes may be its help full for u and next time u post pls ill give clear?

Hi,

please see this link…

http://www.yiiframework.com/forum/index.php/topic/45890-ajax-validation-is-not-performed-for-second-model/page__p__216098__fromsearch__1#entry216098

hope it will be helpful…

My trouble is that the attribute I created is not appearing in the $model->attributes array.

So, when validating the form, it’s alway considered as empty.

The validation rules works fine, but this attibute is never seen in the model.

My model looks like:


/**

 * @property integer $id;

 * @property string $name;

 * @property string $email;

 */


class MyModel extends CActiveRecord

{

   public $myNewAttr;


   public function rules()

   {

      return array(

         array('name, email', 'required', 'on' => 'insert'),

         array('email, myNewAttr', 'required', 'on' => 'update'),

         array('id, name, email, myNewAttr', 'safe', 'on' => 'search'),

      );

   }

   ...

}

And in my controller:


public function actionUpdate($id)

{

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

   $model->setAttributes($_POST['User']);

   if ($model->save())

   {

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

   }

}

And when updating, I alway get the error telling that myNewAttr is empty.

I’ve tryed to force it with getter and setter, or explicite attribution ($model->setAttribute(‘myNewAttr’, $_POST[‘User’][‘myNewAttr’]), nothing works.

Hi,

try this…


$model->myNewAttr= $_POST['User']['myNewAttr'];

before setAttributes…

otherwise

add a line on rule function…


array('myNewAttr', 'default', 'setOnEmpty' => true, 'value' => null),

Ok, it was so simple…

I’d tried, but after the setAttributes and it failed.

Thanks a lot.

Hi,

cool :rolleyes:

can you post the code? so someone help this…

Yes :)

The only correction is in the controller:


public function actionUpdate($id)

{

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

   if (isset($_POST['User']) {

      $model->myNewAttr = $_POST['User']['myNewAttr']; // Forces the myNewAttr attribute

      $model->setAttributes($_POST['User']);

      if ($model->save())

      {

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

      }

   }

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

      'model' => $model,

   ));

}

Thanks… :rolleyes:

I’m curious, previously you did this:


$model->setAttribute('myNewAttr', $_POST['User']['myNewAttr']

and it didn’t work, and then you change to this:


$model->myNewAttr = $_POST['User']['myNewAttr'];

and it works now, what’s the different between setAttribute and direct assignment? I thought they’re the same.

Looking here http://www.yiiframework.com/doc/api/1.1/CActiveRecord#setAttribute-detail, it says that:

From what being said there, I conclude that setAttribute is the same as direct assignment, but not in this case?

I think the difference is not the way it’s done but when it’s done.

I already have this issue, but in another context, and using the setAttribute worked.

Hi


$model->setAttribute

  1. means you can set all attributes on DB but the reason is is if you want to add a any field after create the model so you want to write a code and defined the public property on model.

$model->myNewAttr = $_POST[‘User’][‘myNewAttr’];

2)second option is otherwise you can regenerate the model

Attributes are columns from the table. Since they’re not defined in the Active Record class they are read from the database schema when a model is instantiated.

Extra properties defined in the model are not added to the attribute list read from schema. That’s why you can’t set them using setAttributes() method.

Thanks for your good and clear explanation. I think my previous post made confusion between setAttribute and setAttributes.

Ok, I see. Good explanation, I’ll try to remember it :)