Answer the question
In order to leave comments, you need to log in
Why isn't all form data passed to the model in Yii?
There was such a problem, after submitting the form, not all form data is assigned to the model.
Form view code snippet:
echo $form->textArea($model,'description', array('class'=>"input_text"));
echo $form->textField($model,'url', array('class'=>"input_text"));
public function actionCreateLink() {
$model=new Links;
if(isset($_POST['Links'])) {
$model->attributes=$_POST['Links'];
echo '<pre>';
print_r($_POST['Links']);
print_r($model->attributes);
echo '</pre>';
}
}
Array (
[url] => yandex.ru
[name] => Яндекс
[id_category] => 9
[description] => Поисковая система
)
Array (
[url] => yandex.ru
[name] => Яндекс
[description] =>
[id_category] => 9
)
public function rules() {
return array(
array('url, name', 'required', 'message'=>'Обязательно для заполнения'),
array('id_category', 'required', 'message' => 'Выберите категорию'),
array('id, url, name, id_category, description', 'safe', 'on'=>'search'),
);
}
Answer the question
In order to leave comments, you need to log in
all attributes for which you make a mass assignment must be specified in the rules
Here in Links::rules() there is no description field, so this property is marked as unsafe attribute - look at the logs, you will see the corresponding error there.
the easiest way to addarray('description', 'safe'),
I think that validation has nothing to do with it, since the assignment of form attributes to the model goes before the validation check.
Added array('description', 'required', 'message' => 'write something'), everything worked.
Hence another question, how to make the field not validated? Does that mean it can be empty?
They wrote to you that
safe does not validate anything, but only reports that this data can be safely assigned.
or if you don't want to write it in rules() do this:
public function actionCreateLink() {
$model=new Links;
if(isset($_POST['Links'])) {
$model->setAttributes($_POST['Links'], false);
echo '<pre>';
print_r($_POST['Links']);
print_r($model->attributes);
echo '</pre>';
}
}
Links rules: (added description to save array)
public function rules() {
return array(
array('url, name', 'required', 'message'=>'Обязательно для заполнения'),
array('id_category', 'required', 'message' => 'Выберите категорию'),
array('description', 'safe'),
array('id, url, name, id_category', 'safe', 'on'=>'search'),
);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question