[support] how to preserve current checkbox values if validation fails

Michel Morelli michel at ziobuddalabs.it
Tue Apr 20 07:00:21 UTC 2010


Daniel Caillibaud ha scritto:
> Hi,
>
> In a custom nodetype, I have a fieldset $form['keywords'] with stuff filled by ajax calls, e.g.
>
> $form['keywords'][$vid][$tid]['#type'] = 'checkbox';
> $form['keywords'][$vid][$tid]['#value'] = $tid;
> $form['keywords'][$vid][$tid]['#name'] = 'taxonomy[' .$vid .'][' .$tid .']';
>
> All works quite fine, but, if validation fails (title not set for example), my own form_alter, validate or submit function
> aren't called.
>   
Strange, have you tried to add a line like

drupal_set_message("In my form_alter");

to you form_alter function ? Try in _validate and _submit, too.


> If the user $form['taxonomy'] chexboxes are correctly set after reloading the form with the error displayed, it's not the case
> with my $form['keywords']
> (I guess it's because values of my $form['keywords'] are in $form_state['values']['taxonomy']).
>
> So I tried to do this (set default_value depending on $_POST values) in my after_build function, but all the changes done in it
> aren't displayed (I dump the form so I'm sure changes are done in $form in this function).
>
> function my_after_build_callback(&$form, &$form_state) {
>   // check if it's necessary to do something and what
>   // ...
>     $form['keywords'][$vid][$tid]['#value']  = $toset;
>     $form['keywords'][$vid][$tid]['#default_value']  = $toset;
>     $form_state['values']['taxonomy'][$vid][$tid] = $toset;
>   // ...
>   // drupal_set_message(dump(etc. => changes OK)
>   return $form;
> }
>
> But then drupal_render seems just ignoring my changes, just lik if it took the previous cache version :-/
>   
Because you need to say that the menu need to be rebuild 
($form_state['rebuild'] = true;) (but yuo need to make this in _validate 
function).

However I have see that in your code (the first) there is not set the 
"#default_value" in $form['keywords'][$vid][$tid] item.

M.

-- 
Michel 'ZioBudda' Morelli                       michel at ziobuddalabs.it
Sviluppo applicazioni CMS DRUPAL e web dinamiche (LAMP+Ajax)
Telefono: 0200619074
Telefono Cell: +39-3939890025 --  Fax: +39-0291390660

http://www.ziobudda.net                         ICQ: 58351764  
http://www.ziobuddalabs.it                      Skype: zio_budda
http://www.ziodrupal.net       			MSN: michel at ziobuddalabs.it                   
						JABBER: michel at ziobuddalabs.it



More information about the support mailing list