S
S
Sergey2015-04-27 00:57:21
Yii
Sergey, 2015-04-27 00:57:21

Why fields that match validation rules don't pass it in yii2? What am I doing wrong?

There is a form, most of the fields which are generated on the frontend. This form contains a questionnaire generator. People write a question, choose the type of question (input or radio buttons / checkboxes), if the second / third, then there are answer options that people can add as many as they like. The questions themselves can also be added as many as you like.
I do this with jquery.
I generate form fields as an array, and I plan to process them the same way.

echo $form->field($model, 'answer[]')->label('Answer:');

But here I ran into a problem. For some reason the validation fails. Although the data received is correct. Here is the result of var_dump($model), part of it, which I conditionally printed to the screen if the validation failed:

["question"]=>
array(1) {
[0]=>
string(9) "Question?"
}
["q_type"]=>
array(1) {
[0]=>
string(1) "2"
}
["answer"]=>
array(2) {
[0]=>
string(3) "One"
[1]=>
string(3) "Two"
}
["_errors":"yii\base\Model":private]=>
array(4) {
["captcha"]=>
array(1) {
[0] =>
string(35) "The verification code is incorrect."
}
["question"]=>
}
["q_type"]=>
array(1) {
[0]=>
string(26) "Q Type must be an integer."
}
["answer"]=>
array(1) {
[0]=>
string(41) "Answers must to have from 0 to 64 symbols"
}
}

Those. it can be seen that the data has been received, and it fits the validation rules, however, an error was returned and not one.
And here is the model itself, also part of it, the rules for validating these fields in rules():
['question', 'string', 'length' => [0,128], 'message' => 'Question must to have from 0 to 128 symbols'],
      ['q_type', 'integer'],
      ['answer', 'string', 'length' => [0,64], 'message' => 'Answers must to have from 0 to 64 symbols'],
      ['if_question', 'boolean'],

Why does such an error occur? At the same time, this happens even for the only question that exists initially, i.e. without generating additional ones on the frontend. It turns out that my js is not to blame. Then what is wrong with me and why?
PS I changed q_type to string - still an error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2015-04-27
@butteff

Better show the form view, and what goes to the server. (Screen firebug', for example).
In var_dump you have a curse on the wrong captcha and the wrong length of the question and answer.
It's not clear why you're checking the response as a string, because it should be an array, judging by how you generate the field.
And you keep in mind that when generating a question on the frontend, it is also necessary to generate questions as an array:
question[0][answer][0]
question[0][answer][1]
question[1][answer][0]
question[ 1][answer][1]
And then process accordingly? :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question