1
$form['questionfield_form_fieldset'][$i]['answer_type'] = [
        '#type' => 'radios',
        '#title' => $this->t('Select Answer Type'),
        '#options' => array(
          'select' =>t('Drop Down'),
          'radios' =>t('Radio'),
          'rating' =>t('Rating'),
        ),
        '#description' => $this->t('Select the way answer will disply eg. in radio button or in selectbox(Dropdown)'),
        '#default_value' => $config->get($form_count . '.' . $drupal_form_id . '.' . 'answer_type'),
        '#required' => TRUE,
      ];



      $form['questionfield_form_fieldset'][$i]['answer_box_id'] = [
        '#type' => 'textarea',
        '#title' => $this->t('Answer for above question'),
        '#description' => $this->t('Add your answers for above question here.'),
        '#default_value' => $config->get($form_count . '.' . $drupal_form_id . '.' . 'answer_box_id'),
        '#required' => TRUE,
        '#states' => array(
      'visible' => array(
        array(
        array(":input[name='questionfield_form_fieldset[$i][answer_type]']" => array('value' => 'select')),
        'or',
        array(":input[name='questionfield_form_fieldset[$i][answer_type]']" => array('value' => 'radios')),
      ),
        ),
    ),
      ];

I want to make answer_box_id as not required when user click rating radio button .

4
  • Please be aware that we do not provide tutorials or step-by-step instructions as explained in the help center. Instead explain your attempt(s), and be as complete as possible. What have you tried, what did you expect, what actually happened? If you have any code to include in the question, please add it. The more complete you are, the better we can formulate an answer. – Neograph734 Jul 27 '17 at 11:09
  • I already mention what I did . I provide u full code – Kshitij Soni Jul 27 '17 at 11:25
  • The question shows code and describes what you want to achieve, but it doesn't say what you obtain from that code. Did you try that code, or did you simply find it on Internet and you want to know if it does what you want to achieve? Keep in mind that questions on Stack Exchange sites are thought to be useful also for future users, not just for the users asking the questions. – kiamlaluno Jul 27 '17 at 21:09
  • okay I will keep this in mind . – Kshitij Soni Jul 28 '17 at 7:15
2

You need to remove '#required' => TRUE from the render array.

States only control "soft" changes through javascript. They don't (and can't) have any effect on the backend validation; if they could, it would be trivial for anybody to bypass the server-side validation for a field just by setting some CSS styles on it. That would not be a good situation to get into.

5
  • 2
    I'd like to add to that that you can also use states to make a field required. But since the validation is done client-side, it can be circumvented and is not 100% reliable. – Neograph734 Jul 27 '17 at 11:19
  • AKA "100% unreliable" @Neograph734 :) Client side "validation" is for bells and whistles, it doesn't really deserve the suffix "validation" IMHO – Clive Jul 27 '17 at 11:21
  • True, though depending on the situation it might be useful to the OP. It does add a red asterisk to aid the user when filling in the form :). And with a small validation callback it can be checked server side if you need to have that 100%. – Neograph734 Jul 27 '17 at 11:24
  • can I manage this by using condition in "visible" field ? – Kshitij Soni Jul 27 '17 at 11:25
  • 1
    @KshitijSoni see this question: drupal.stackexchange.com/questions/14173/… – Neograph734 Jul 27 '17 at 11:27
0

Hello I solve as below..

$form['questionfield_form_fieldset'][$i]['answer_box_id'] = [
        '#type' => 'textarea',
        '#title' => $this->t('Answer for above question'),
        '#description' => $this->t('Add your answers for above question here.'),
        '#default_value' => $config->get($form_count . '.' . $drupal_form_id . '.' . 'answer_box_id'),
        //'#required' => TRUE,
        '#states' => array(
      'visible' => array(
        array(
        array(":input[name='questionfield_form_fieldset[$i][answer_type]']" => array('value' => 'select')),
        'or',
        array(":input[name='questionfield_form_fieldset[$i][answer_type]']" => array('value' => 'radios')),
      ),
        ),
      'required' => array(
        array(
        array(":input[name='questionfield_form_fieldset[$i][answer_type]']" => array('value' => 'select')),
        'or',
        array(":input[name='questionfield_form_fieldset[$i][answer_type]']" => array('value' => 'radios')),
      ),
        ),
    ),
      ];
2
  • Bear in mind that with this method the field is no longer required to people without javascript (or people who know how to work with the browser dev tools). Don't use this approach for anything critical – Clive Jul 27 '17 at 11:53
  • yes true . I will add custom validation – Kshitij Soni Jul 27 '17 at 12:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.