0

I have a simple form with a select menu on the node display page. Is there an easy way to validate the form in my callback function? By validation I don't mean anything advanced, just to check that the values actually existed in the form array. For example, without ajax, if my select menu has 3 items and I add a 4th item and try to submit the form, drupal will give an error saying something similar to "an illegal choice was made, please contact the admin."

With ajax this 4th item you created would get saved into the database. So do I have to write validation like

if ($select_item > 0 && $select_item <= 3) {
  //insert into db
}

Or is there an easier way that will check that the item actually existed in the form array? I'm hoping there is since without ajax, drupal will not submit the form if it was manipulated. Thanks.

EDIT: So I basically need this in my callback function?

$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);

To get $_POST['form_build_id'], I sent it as a data param, is that right? Where I use form_get_cache, looks like there is no data. Kind of lost now.

googletorp
  • 33,075
  • 15
  • 67
  • 82
Wade
  • 59
  • 3
  • 8
  • 1
    What is your goal exactly? It sounds like you are heading down the wrong track here. – googletorp Jan 25 '10 at 11:14
  • Sorry if I didn't explain it too well, my goal is simply to check that the option exists within the form select array and hasn't been manipulated. For example, let's say my select menu has 3 items, red, green, blue. Now someone attempts to enter bogus info to my database and adds a fourth option, black. If I submit this form without ajax, Drupal will know that black never existed in the array and will say an illegal choice was made. However, if I use ajax with a callback function, this basic validation does not occur so that value of black will be inserted. Hope that clears things up. – Wade Jan 25 '10 at 18:08

2 Answers2

0

Since you're already using AJAX, why not just write a bit of jQuery to only allow form submission if the choice is within the list of legal choices? This can be done within the custom module it already looks like you're working on (using drupal_add_js()).

Mike Crittenden
  • 5,779
  • 6
  • 47
  • 74
  • 1
    Well, if the user can manipulate the select menu, they could also manipulate the javascript, that's why I want to do it server side. So do I need to write out all the basic validation checks myself or is there a drupal function for that? Since without ajax they are able to automatically find out of the form was manipulated. – Wade Jan 26 '10 at 17:46
0

It is not especially 'easy', but the standard way to do it would be to use Drupals Forms API for the callback submission as well - that way, you'll get the same validation that would happen on a non js submit.

Take a look at Adding dynamic form elements using AHAH. While it does not match your scenario exactly (they rebuild the form on the callback to add new elements, not to save data), the explanation of the processing workflow is pretty helpful.

Then there are several modules that try to offer AJAX form submission in a generic way - you could check their code on how to do it (or maybe just use them ;)

Finally, there are efforts to put better support this functionality into core in Drupal 7 - the related discussions might also help.

Henrik Opel
  • 19,341
  • 1
  • 48
  • 64
  • Added comment to my original post, didn't know how to add the code in the comments... – Wade Jan 29 '10 at 20:46