AJAX change field is required 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, &amp;$form_state) {

      $form['ask_required'] = array(
        '#type' =&gt; 'checkbox',

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

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

  }else{

       $form['vin'] = array(
        '#type' =&gt; 'textfield',

        '#title' =&gt; t('VIN'),
        '#default_value' =&gt; "",
          '#prefix' =&gt; '&lt;div id="replace_reqired_div"&gt;',
        '#suffix' =&gt; '&lt;/div&gt;',
    );
  }


    $form['next'] = array(
        '#type' =&gt; 'submit',
        '#value' =&gt; 'Dalej &gt;&gt;',
        '#submit' =&gt; 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(&amp;$form, &amp;$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' =&gt; 'checkbox',
            '#title' =&gt; t('required'),
            '#default_value' =&gt; "",
            '#ajax' =&gt; array(
                'callback' =&gt; 'ajax_required_callback',
                'wrapper' =&gt; 'replace_reqired_div',
            ),
        );

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

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


why?
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' =&gt; 'textfield',
  '#title' =&gt; t('VIN'),
  '#default_value' =&gt; '',
  '#states' =&gt; array(
    'required' =&gt; array(
      ':input[name="ask_required"]' =&gt; array('checked' =&gt; TRUE),
    ),
  ),
);