Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Fabrice_ on
Hello,
I'm developing a module that uses a form.
The module reads the two textbox value (defined in _form), validates the data (_form_validation) and inserted in the db (_form_submit).
I have done without ajax submit and it works fine.
Now i need to this using AJAX submit.
I tried to follow the examples (eg AJAX example) but I found problems with data validation:
_form_validation hook is called but if the procedure find a validation error it call anyway _form_submit hook... this is wrong!
Can anyone help me to fix?
This is a piece of code
function example_form($_data=NULL)
{
$form['username'] = array(
'#type' => 'textfield',
'#title' => t('User`s username'),
'#required' => TRUE,
);
$form['email'] = array(
'#type' => 'textfield',
'#title' => t('E-mail'),
'#required' => TRUE,
);
$form['sub']['insert'] = array(
'#type' => 'button',
'#value' => t('Save record'),
'#ajax' => array(
'callback' => 'example_form_submit',
'wrapper' => 'example-form',
),
);
return $form;
}
function example_form_validate($form, &$form_state)
{
if (!valid_email_address($form_state['values']['email'])) {
form_set_error('email', t('That e-mail address is not valid.'));
}
function example_form_submit($form, &$form_state)
{
<insert data into db>
}
Comments
no one can help me?
no one can help me?
The error lies in the submit button
Off the top of my head I would say instead of type => button it should be type => submit.
http://api.drupal.org/api/drupal/developer--topics--forms_api_reference....
I don't think it's the submit
I don't think it's the submit thats the problem, it's more likely the names you have for your wrapper.
I had major issues trying to get my errors to show up to. But after several hours got something working.. so to take your code:
I'm not sure if this is best practice, as I got here with a lot of guess work. If I've done something blackhat, please reply back. Also I haven't tested the code exactly above, but it's basically a toned down version of my fix. Cheers.
alexkb have you get the text error displayed?
Hi alexkb, thanks for your code. It helps me to submit with ajax and validation my form, and now those fields with validation errors change their color to red, but I can't get the text error displayed on my form.
Have you a solved that issue?
And I can't resubmit the form after validation errors
If I set some wrong values on my form and try to submit it, It doesn't get submited, but after fixed the values with valid ones, it still doesn't get submited.
I have fixed my code :)
My problem was related to the wrapper id used, now it is working fine showing also the errors just before the form.
I was attaching a form to a node with this code on _node_view hook:
So I was getting an html code like this:
When the form was submited it used the wrapper div to show the result, so It replaced the form tag with the response without any form tag:
I fixed it setting the prefix and suffix inside the form:
Thanks to all for your ideas!
Greets.
thanks to all
thanks to all
Heres another solution for
Heres another solution for this problem, without using prefix and suffixes. This is good when you want to replace the form with a message once it has been submitted. It displays the validation messages correctly.
Note that you need to get the id of the form element from the form's HTML output.
Also important to note that you should not give your ajax callback function a _submit name (ie. example_ahah_form_submit) - that will cause the function to be triggered twice whenever the ajax form is submitted.
Thanks for the tip with
Thanks for the tip with form_get_errors() !!!
This is awesome! I searched
This is awesome! I searched the web forever and this is the best example on ajax form submitting!
Thank you thank you!
John