Be sure to check out the API it will give you good information on how to use form_set_error(). You should use it in your validate function and can and should set it to highlight the proper field and be sure to use the t() function.
Thanks for the replies. I haven't messed around with it yet but I thought using validation and submit functions were kind of specific to node forms? I'm technically making nodes initially, but I'm storing data associated with them in a custom table. This form is just to be able to insert into the custom table. It's a multi-part form also if that makes a difference.
No, you can use validate and submit functions on any form you'd like. They can be added to other forms with hook_form_alter() or added to your own administrative forms.
Just add:
to the form you're editing with hook_form_alter()
...or
function my_form_validate($form, &$form_state) {
if (empty($form_state['values']['this_field'])) {
form_set_error('this_field', t('You must enter a value.'));
}
}
to your own form.
you can also use placeholders (! % @) to use variables in the form_set_error message.
I did this from my iPad! Good thing computers aren't like this...
Edit: updated the validation function to show form_set_error()
Comments
If you set form_set_error()
If you set form_set_error() in your form's _validate() function, it will prevent the form submission from proceeding to the _submit() function.
Contact me to contract me for D7 -> D10/11 migrations.
Be sure to check out the API
Be sure to check out the API it will give you good information on how to use form_set_error(). You should use it in your validate function and can and should set it to highlight the proper field and be sure to use the t() function.
Thanks for the replies. I
Thanks for the replies. I haven't messed around with it yet but I thought using validation and submit functions were kind of specific to node forms? I'm technically making nodes initially, but I'm storing data associated with them in a custom table. This form is just to be able to insert into the custom table. It's a multi-part form also if that makes a difference.
No, you can use validate and
No, you can use validate and submit functions on any form you'd like. They can be added to other forms with hook_form_alter() or added to your own administrative forms.
Just add:
Or...
to the form you're editing with hook_form_alter()
...or
to your own form.
you can also use placeholders (! % @) to use variables in the form_set_error message.
I did this from my iPad! Good thing computers aren't like this...
Edit: updated the validation function to show form_set_error()