By wardazo on
Hi
In the user registration form the default type is "container". However, according to the manual the "container" form elements cannot take "#ajax":
http://api.drupal.org/api/drupal/developer%21topics%21forms_api_referenc...
Could someone explain how I can make one element dependent on another without using states? For example something like this and email textfield is dependent on the age of the user:
function custom_form_alter(&$form, &$form_state, $form_id){
if($form_id == 'user_register_form'){
$form['field_birthdate']['und'][0]['#ajax'] = array(
'callback' => 'custom_fecha',
'wrapper' => 'fechax',
);
$form['field_parents_email']['#prefix'] = '<div id="fechax">';
$form['field_parents_email']['#suffix'] = '</div>';
}
}
function custom_fecha($form, $form_state){
$ageok = custom_agecheck($form_state['values']['field_birthdate']['und'][0]['value']);
if($ageok){
$form['field_parents_email']['und'][0]['value']['#type'] = "markup";
}
else{
$form['field_parents_email']['und'][0]['value']['#type'] = "textfield";
}
$f['field_parents_email'] = $form['field_parents_email'];
return $f;
}
Comments
First off, you cannot make
First off, you cannot make changes in your ajax callback, all changes have to be in the form definition. Any changes made in the ajax callback will not persist to your submit function, meaning the submitted values will not be available.
In your form definition, you will have access to the existing values in $form_state['values']. You can get the value of Field A there, and use it to determine Field B.
Contact me to contract me for D7 -> D10/11 migrations.
Thanks
I had totally misinterpreted the root the problem. Very clear answer.
Thank you.