Hello to all,
I have the following problem, I implemented a module custo I make a validation for the presence of value in the fields, (and until everything is right).
In my form there is a checkbox (boolean) in which the user makes choices, in this case if the boolean is = 1
$form_state ['values'] ['field_ulteriori_passeggeri'] ['und'] [0] ['value'] == 1
I would like to set to null, the following variables in the form submission so as not to populate the database:

unset($name);
unset($name);
unset($data);

But you can not, you have tips?

/**
 * Implementation of hook_form_alter().
 */
function validate_passeggeri_node_registration_form_alter(&$form, &$form_state, $form_id) {
  
  if ($form_id == 'node_registration_form') {
$form['#validate'][] = 'validate_passeggeri_node_registration_form_validate';
  }

}

function validate_passeggeri_node_registration_form_validate($form, &$form_state) {
if ($form_state['values']['field_ulteriori_passeggeri']['und'][0]['value'] == 1) {
$cognome = $form_state['input']['field_cognome_passeggero']['und'][0]['value'];
$nome = $form_state['input']['field_nome_passeggero']['und'][0]['value'];
$data = $form_state['input']['field_data_nascita_passeggero']['und'][0]['value'];
if (!empty($cognome) && !empty($nome) && !empty($data)) {
} else {
form_set_error('field_cognome_passeggero', t('Controlla i campi del Passeggero 1 nella sezione Equipaggio: I campi diventano tutti obbligatori se almeno uno e\' selezionato '));
form_set_error('field_nome_passeggero');
form_set_error('field_data_nascita_passeggero');
}
} else {
$form_state['input']['field_cognome_passeggero']['und'][0]['value'] = ' ';
}
}

Comments

xlin’s picture

Have you tried $form['#states'] to unset the values when you checkbox is checked/unchecked?

See following examples.
https://api.drupal.org/api/examples/form_example!form_example_states.inc...

lucavox’s picture

I can not see like $form['my_field']['# states'] in dpm

Ayesh’s picture

You are changing the form_state input array. But Drupal uses the $form_state['values'] array to store the sanitized form values and use them after.

unset($form_state['values'][FIELD NAME HERE]);

Above should unset all values for that field if you have any required fields, they will trigger a validation error now though.