0

form_set_error supposedly takes the name of the offending form element as the first param.

I have a form element that is named "field_reviewer_terms_of_service[value]" (as taken from firebug). My code that sets the error is:

    form_set_error('field_reviewer_terms_of_service[value]', t('You must agree to the Terms of Service.'));

The error shows up in the messages, but doesn't highlight the form element itself. What am I doing wrong?

Edited with HTML of form element:

<div id="edit-field-applicant-terms-of-service-value-wrapper" class="form-item">
 <label for="edit-field-applicant-terms-of-service-value" class="option"><input type="checkbox" class="form-checkbox required error" value="1" id="edit-field-applicant-terms-of-service-value" name="field_applicant_terms_of_service[value]"> I agree to the <a href="/terms_of_service" target="_blank">Terms of Service</a></label>
</div>
1

Don't put the [value] inside, it should work with only

form_set_error('field_reviewer_terms_of_service', t('You must agree to the Terms of Service.'));

See the documentation for form_set_error().
You may have to use some [value inside your field if it is multivalued, see this comment but we need to see the HTML ouput for your field.

| improve this answer | |
  • I tried it without the [value] but it still didn't work. I edited the question with the HTMLs. – Joren Dec 15 '11 at 20:21
  • Hum I'm not really sure why is this happening, I set up a single on/off checkbox (as you appear to have) and it works fine with just the fieldname. However with multiple checkbox it doesn't work but there's a module to correct this: checkbox validate. Have you looked at modules like legal or terms of use ? – tostinni Dec 16 '11 at 21:17
  • I may have to fall back on those modules. I didn't think I'd need a module just to add a required checkbox. :P – Joren Dec 16 '11 at 21:22

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.