This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

fujitsuDev
fujitsuDev

Hi,

I've a form with dynamic questions, like this:

{{ form_ajax('onSend') }}
{% for question in chapter.questions %}
  [fieldset]
    [legend]{{ question.question }}[/legend]
        {% for answer in question.answers %}
          [label]
            [input type="radio" name="question-{{ question.id }}" value="{{ answer.id }}"] {{ answer.answer }}
          [/label]
        {% endfor %}
  [/fieldset]
{% endfor %}
[button type="submit"]Done[/button]
{{ form_close() }}

I've one chapter, contains its questions (not same according to the chapters), each question contains many answers (display as button inputs).

How I can get in my component class the dynamics inputs question-{{ question.id }}? I would like return an 'required' error message if no answer.

I tried...

$questions = Input::all();
    foreach ($questions as $question => $answer) {
      $validator = Validator::make(
        [$question => $answer],
        [$question => 'required'],
        ['required' => 'You must have select an answer!']
      );
      if ($validator->fails()) {
        return Redirect::back()->withErrors($validator)->withInput();
      }
    }

How I can do that? Thanks.

Last updated

daftspunky
daftspunky

The problem here in the code is that you lose the ID of the question with the validation response. Otherwise, the code looks like it should work. It's hard to say what else is needed.

1-2 of 2

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.