G
G
GM2mars2014-02-15 18:43:29
Yii
GM2mars, 2014-02-15 18:43:29

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"));

Controller code:
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>';
  }
}

The result is:
Array (
    [url] => yandex.ru
    [name] => Яндекс
    [id_category] => 9
    [description] => Поисковая система
)

Array (
    [url] => yandex.ru
    [name] => Яндекс
    [description] =>
    [id_category] => 9
)

As you can see, the description field was not assigned to the model. What could be the problem?
Links rules: (added description to save array)
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

7 answer(s)
V
Vit, 2014-02-15
@GM2mars

all attributes for which you make a mass assignment must be specified in the rules

P
p4s8x, 2014-02-15
@p4s8x

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 add
array('description', 'safe'),

G
GM2mars, 2014-02-15
@GM2mars

I think that validation has nothing to do with it, since the assignment of form attributes to the model goes before the validation check.

G
GM2mars, 2014-02-15
@GM2mars

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?

E
Eugene, 2014-02-15
@Nc_Soft

Assign handles, the less magic the better.

C
Cage, 2014-04-15
@Cage

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>';
  }
}

but it's not safe!

C
Cage, 2014-04-15
@Cage

Links rules: (added description to save array)

After all , safe a not save
You added it for the " search " script, and for the newly created object, the " insert " script is used by default ,
add it as previously indicated on a separate line.
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 question

Ask a Question

731 491 924 answers to any question