Do object attributes not save unless it has a validation rule applied to it? I am trying to save a form with no validations applied to it and it does not save, if I apply a validation rule to one of the fields then it does save.
Do object attributes not save unless it has a validation rule applied to it? I am trying to save a form with no validations applied to it and it does not save, if I apply a validation rule to one of the fields then it does save.
You should read the documents… here is the part for the attribute assignements - http://www.yiiframework.com/doc/guide/1.1/en/form.model#securing-attribute-assignments
Yeah I have done that, but it still does not work.
Its not really what I am asking. But thanks for your help.
Let me explain …
Here is the code for my controller action: -
public function actionCreate() {
$client = new Client;
if(isset($_POST["Client"])) {
$client->attributes = $_POST["Client"];
if($client->validate()) {
$client->save();
$this->redirect(array("client/index"));
}
}
$view->client = $client;
$this->render("Create", array("view" => $view));
}
As you can see I have used $client->attributes = $_POST["Client"]; already, however it still not saving the fields. Why is this?
Well I have tried with validation rules applied and without, it only saves the fields with the validation rules applied. So I am assuming Yii only saves attributes with validation rules applied, is this true?
You should read about “safe” attributes
You have to define all attributes as "safe" in this case:
public function rules()
{
return array(
array('a, b, c, d', 'safe'),
);
}
There are many topics about safe attributes on the forum. Check the search result
Thanks for your help, I will apply the safe attributes.
The link I gave you is just about what you need… as you wrote… seems to me that you have read it… but you haven’t understood it!
In your code you are using a "massive assignement" like:
$client->attributes = $_POST["Client"];
To decide what attributes can be assigned by using this massive assignement… in Yii we use "safe" attributes…
NOTE: if some attribute has a validation rule defined it becomes automaticaly safe (Yii 1.1.x)…
so try to read again that part of the documentation… I hope it will be more clear now…
Ahh I see what you are saying now, thanks for clearing it up.