1

I have simple form, two fields one is checkbox and second is text. When checkbox is checked. Text field is required, and when checkbox is unchecked textfield shouldn't be required...

To perform this need with AJAX, i have my working example like below:

function mycustom_form($form, &$form_state) {

      $form['ask_required'] = array(
        '#type' => 'checkbox',

        '#title' => t('required'),
        '#default_value' => "",
           '#ajax' => array(
            'callback' => 'ajax_required_callback',
            'wrapper' => 'replace_reqired_div',
        ),
    );

       if (!empty($form_state['values']['ask_required']) && $form_state['values']['ask_required']) {
    $form['vin'] = array(
        '#type' => 'textfield',
    '#required' => TRUE,
        '#title' => t('VIN'),
        '#default_value' => "",
          '#prefix' => '<div id="replace_reqired_div">',
        '#suffix' => '</div>',
    );

  }else{

       $form['vin'] = array(
        '#type' => 'textfield',

        '#title' => t('VIN'),
        '#default_value' => "",
          '#prefix' => '<div id="replace_reqired_div">',
        '#suffix' => '</div>',
    );
  }


    $form['next'] = array(
        '#type' => 'submit',
        '#value' => 'Dalej >>',
        '#submit' => array('mail_form_submit'),

    );
    //    dpm($form);
    return $form;

}

function ajax_required_callback($form, $form_state) {


     return $form['vin'];
}

Is there any more elegant way e.g. to change attributes of field not whole VIN field?

Second thing is that when i place above code in ...form_alter, its not working..., there are fields there is ajax callback but required star is not changed

function module_form_alter(&$form, &$form_state, $form_id) {

    if ($form_id == 'user_register_form') {

        $form['account']['name']['#description'] = t('');
        $form['account']['mail']['#description'] = t('');

        $form['ask_required'] = array(
            '#type' => 'checkbox',
            '#title' => t('required'),
            '#default_value' => "",
            '#ajax' => array(
                'callback' => 'ajax_required_callback',
                'wrapper' => 'replace_reqired_div',
            ),
        );

        if (!empty($form_state['values']['ask_required']) && $form_state['values']['ask_required']) {
            $form['vin'] = array(
                '#type' => 'textfield',
                '#required' => TRUE,
                '#title' => t('VIN'),
                '#default_value' => "",
                '#prefix' => '<div id="replace_reqired_div">',
                '#suffix' => '</div>',
            );
        } else {

            $form['vin'] = array(
                '#type' => 'textfield',
                '#title' => t('VIN'),
                '#default_value' => "",
                '#prefix' => '<div id="replace_reqired_div">',
                '#suffix' => '</div>',
            );
        }
    }
}

why?

  • This is not directly related to your problem but why not use the drupal states api. Which will only show the textfield when checkbox is checked and will hide the textfield when checbox is unchecked. See here api.drupal.org/api/examples/… – MutantMahesh Jul 31 '14 at 13:36
2

You can use Form API #states attribute. This will allow you to trigger something based on another fields information. Below should be a working example.

$form['vin'] = array(
  '#type' => 'textfield',
  '#title' => t('VIN'),
  '#default_value' => '',
  '#states' => array(
    'required' => array(
      ':input[name="ask_required"]' => array('checked' => TRUE),
    ),
  ),
);
  • In addition ou will need custom validation of the form that checks that the textfield is not empty when checkbox is enabled. Drupal states do not perform any validation. – sanzante Jul 31 '14 at 15:17
  • tunic is correct. You will need to add your own validation. Thanks for catching that. – FatGuyLaughing Jul 31 '14 at 15:28
  • If my answer worked for you, please click the checkmark as I really need my rep points :) I can't comment on anything but my own posts. – FatGuyLaughing Aug 1 '14 at 14:44
  • Thank you for answer FatGuyLaughing, your solution also works on registration form :) I can't make your rep up, I have 2 :) – mbahojlo Aug 4 '14 at 11:11
  • No worries! I'm glad it worked for you :) – FatGuyLaughing Aug 4 '14 at 14:38

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.