0

I'm currently trying to set up a custom validation script to prevent spam on a comment form with hidden elements. The problem involves setting up the conditional for the submitted form values. I'm currently using the hook_form_FORM_ID_alter. Here is my existing code.

function STARTERKIT_form_comment_node_contact_page_form_alter(&$form, &$form_state) {
    $form['#validate'][] = 'comment_validation_function';
}

function comment_validation_function($form, &$form_state)
{
    if ($form_state['values']['field_website'] != 'http://') {
        form_set_error('field_website', t('Please do not change the value of this field.'));
    }
}

The problem is that $form_state['values']['field_website'] isn't returning the value inside the text field. I've looked at so many pages showing a script very similar to this, yet I haven't been able to get this even close to working.

The field id is "field_website".

0

Assuming the field is a text type (and your site is monolingual) you'll find the value here:

$form_state['values']['field_website'][LANGUAGE_NONE][0]['value']

There's an explanation here.

Instead of doing this, though, you might want to have a look at the Field Validation module, which will let you add the same validation rule through the UI.

  • Thanks for the quick response. That ended up doing the trick. I had previously looked at the Field Validation module, yet it seems to only attach validation to any content created by it's sibling Webform module. – digitalclover Apr 19 '12 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.