Answer the question
In order to leave comments, you need to log in
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:');
["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"
}
}
['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'],
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question