I'm creating a simple contact form and it works flawlessly, besides the validation.
When I try to exercise the validate function, no error messages are shown. The text in the correspondent field turns red, so that works, but where is the messages?
If I fill in the form correctly and the mail gets sent, messages are shown just like they should.
Here's my code:
function mymodule_buysell_contact_form($form, &$form_state) {
// The form
$form['contact'] = array(
'#title' => t('Skicka ett meddelande till säljaren'),
'#type' => 'fieldset'
);
$form['contact']['message'] = array(
'#type' => 'textarea',
'#title' => t('Meddelande:'),
'#cols' => 35,
'#rows' => 6,
'#resizable' => FALSE
);
$form['contact']['name'] = array(
'#type' => 'textfield',
'#title' => t('Ditt Namn:'),
'#size' => 35,
'#maxlength' => 30
);
$form['contact']['compname'] = array(
'#type' => 'textfield',
'#title' => t('Företag:'),
'#size' => 35,
'#maxlength' => 30
);
$form['contact']['phone'] = array(
'#type' => 'textfield',
'#title' => t('Telefon:'),
'#size' => 35,
'#maxlength' => 30
);
$form['contact']['cellphone'] = array(
'#type' => 'textfield',
'#title' => t('Mobiltelefon:'),
'#size' => 35,
'#maxlength' => 30
);
$form['contact']['mail'] = array(
'#type' => 'textfield',
'#title' => t('Din E-mail:'),
'#size' => 35,
'#maxlength' => 30
);
$form['contact']['captcha'] = array (
'#type' => 'captcha',
'#captcha_type' => 'image_captcha/Image',
);
$form['contact']['submit'] = array(
'#type' => 'submit',
'#value' => t('Skicka')
);
return $form;
}
function mymodule_buysell_contact_form_validate($form, &$form_state) {
// Validate message
$valid_message = $form_state['values']['message'];
if (empty($valid_message)) {
form_set_error('message', 'Du har inte skrivit något meddelande');
}
// Validate if a name is submitted
$valid_name = $form_state['values']['name'];
if (empty($valid_name)) {
form_set_error('name', 'Du har inte fyllt i ett namn');
}
// Validate if a company name is submitted
$valid_compname = $form_state['values']['compname'];
if (empty($valid_compname)) {
form_set_error('compname', 'Du har inte fyllt i ditt företagsnamn');
}
// Validate phone by look for numbers
$valid_phone = $form_state['values']['phone'];
if (!is_numeric($valid_phone)) {
form_set_error('phone', 'Ditt nummer verkar inte vara korrekt ifyllt (endast siffror)');
}
// Validate cellphone by look for numbers
$valid_cellphone = $form_state['values']['cellphone'];
if (!is_numeric($valid_cellphone)) {
form_set_error('cellphone', 'Ditt mobilnummer verkar inte vara korrekt ifyllt (endast siffror)');
}
// Validate the mail, using Drupals own function
$valid_email = $form_state['values']['mail'];
if (!valid_email_address($valid_email)) {
form_set_error('mail', 'Din Epost verkar inte vara korrekt ifylld');
}
}
TRUE
.t()
is only used for translating from English to the language set for the Drupal site; the string passed tot()
needs then to be in English. If you want to only to show the user interface's strings in a specific language, then you should not callt()
.