Clientside Validation works great, and I'm using FAPI Validation for some extra rules.
However, my form also uses an ajax callback to run some stuff and then output the results under the form. This callback is always run, even when validation fails. e.g.:
function example_form($form, &$form_state) {
$form['zip'] = array(
'#type' => 'textfield',
'#title' => t('of this ZIP Code'),
'#required' => TRUE,
'#rules' => array(
'numeric',
),
);
$form['submit'] = array(
'#type' => 'submit',
'#value'=>t('Submit'),
'#ajax' => array(
// #ajax has two required keys: callback and wrapper.
// 'callback' is a function that will be called when this element changes.
//'callback' => 'ajax_example_simplest_callback',
'callback' => 'example_form_submit',
// 'wrapper' is the HTML id of the page element that will be replaced.
'wrapper' => 'results',
'method' => 'html',
),
);
return $form;
}
function example_form_submit($form, &$form_state) {
$values = $form_state['values'];
$html = "<p>zip: ". $values['zip'] . "<br>";
return array("#markup" => $html);
}
"zip: ", for example, always shows up, even when a valid value hasn't been passed. Is there anyway of preventing submission/working out in example_form_submit whether validation has been passed?
Comments
Comment #1
attiks commentedThe problem is that your code is attached to the click event of the button, our code is attached to the submit event of the form, form api apparently does not support attaching to the submit event of the form (feel free to create an issue in http://drupal.org/project/issues/drupal).
Possible solutions:
Comment #2
attiks commentedClosing this, if you need more assistance, feel free to re-open.
Comment #3
vdsh commentedI had the same issue and I struggled a bit with it (my log-in form is using an ajax request), so here is what I did: I altered the #ajax element from the form in my hook form alter
And in the js:
Hope it helps a bit!