I have a couple of different forms on one page; each has a 'name' field.
Sensibly, on submission drupal processes the submitted form, but in what seems a completely barmy way of doing things, form_set_error() just takes a field name, and appears to have no knowledge of which form was submitted.
So if the name field is invalid on the submitted form, it gets marked as invalid on all the other forms that use a field called 'name'.
Does this mean that every field needs a unique field name, e.g. name-somemodule1 and name-somemod2? This will break user convenience (browser probably knows what to pre-fill for 'name').
Sure I've missed something, but have read a lot of FAPI pages and code comments and am still confused.
In the screenshot below, you'll see 2 forms. The one on the left is the submitted one, with 'invalid-name' in a field called 'name'. The one on the right is not submitted (can only physically click one submit button at once!) and yet it's 'name' field (with 'admin' in it) is also marked invalid.
These are entirely separate forms.
Here's my modules' code (email field omitted, some attrs omitted just to reduce post size)
function mymod_contact_form() {
$form['comment'] = array(
'#title' => t('Enter query'),
'#default_value' => '',
'#type' => 'textarea',
);
$form['name'] = array(
'#title' => t('Enter name'),
'#default_value' => '',
'#type' => 'textfield',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Send',
);
return $form;
}
function mymod_contact_form_validate($form, &$form_state) {
$name = trim($form_state['values']['name']);
if (strpos($name,' ') === FALSE) {
form_set_error('name', t('Please enter your full name'));
}
}
function mymod_contact_form_submit($form, &$form_state) {
$vals = $form_state['values'];
// …
}