[support] how to preserve current checkbox values if validation fails
Daniel Caillibaud
ml at lairdutemps.org
Tue Apr 20 12:47:25 UTC 2010
Le 20/04/10 à 11:49, Daniel Caillibaud <ml at lairdutemps.org> a écrit :
> (I'll look what happen with a standard nodetype as page to better understand fapi process)
Fapi process is quite clear http://drupal.org/node/165104, but if _validate functions are called, when a previous validation
fails (no title), changes made there on form or form_state are ignored then by drupal_render :-(
to test
function mymodule_form_alter(&$form, &$form_state, $form_id) {
...
else if($form_id == 'page_node_form') {
$form['#after_build'][] = '_test_after_build';
$form['#validate'][] = '_test_validate';
$form['#submit'][] = '_test_submit';
}
}
function _test_validate(&$form, &$form_state) {
drupal_set_message('_test_validate with build_id=' .$form['#build_id']);
$form['body']['#value'] = 'My new body modified in _test_validate on form';
$form_state['values']['body'] = 'My new body modified in _test_validate via form_state';
$form['title']['#default_value'] = 'My new modified title in _test_validate';
if ($errors = form_get_errors()) {
// this is executed when title validation fails
drupal_set_message(edulibre_get_dump($errors, 'errors already fired in _test_validate'));
}
drupal_set_message(edulibre_get_dump($form, 'form in _test_validate'));
drupal_set_message(edulibre_get_dump($form_state, 'form_state in _test_validate'));
return $form;
}
But then, in drupal_render (and of course on screen), we have the $form in its state before our _test_validate :-/
--
Daniel
PS: my custom dump function (MAXDUMPS is set to prevent too much krumo tree, if not javascript could become really slow)
function edulibre_get_dump($var, $str='', $krumo=TRUE) {
static $i = 0;
if ($i < MAXDUMPS) {
if ($krumo && function_exists('has_krumo') && has_krumo()) {
return 'Starting dumping (<strong style="color:#a00">'
.$str .'</strong>)<br />' .krumo_ob($var) ."\nEnd of dump ($str)<br />";
}
else {
// a more classical dump...
ob_start();
echo "Starting dumping ($str)<br /><pre>";
// var_export($var); // "Nesting level too deep" could be fired
var_dump($var);
echo "</pre>\nEnd of dump ($str)<br />";
$out = ob_get_contents();
ob_end_clean();
$i++;
return str_replace("=>\n", '=> ', $out); // to compact a little bit output
}
}
else {
return "Max dumps reached (" .MAXDUMPS .") : $str)";
}
}
More information about the support
mailing list