0

I've created a custom module for populating a database and now I need to apply form validation to this form.

I want a few different validation things to happen.

  1. I want to limit specific entries on the client side IE: only allowing 6 characters being entered etc.
  2. On submit I want to run through validation rules to make sure what was entered is correct. IE: only positive #s etc.

Are there any drupal 7 FAPI fields to validate specifics like this?

I found something like '#element_validate' => array('element_validate_integer_positive'),

Is there a list anywhere of element validations like this? I'm unable to find documentation anywhere :(

0

I assume you are using Drupal's Form API.

  1. I want to limit specific entries on the client side IE: only allowing 6 characters being entered etc.

In your element definition, you need to add the HTML attribute maxlength. For example:

$form['your_field_name']['#attributes'] = array('maxlength' => array('6'));

Regarding:

  1. On submit I want to run through validation rules to make sure what was entered is correct. IE: only positive #s etc.

You can create your own validation function for the form or the element. For example, in your form definition, in the element you want to validate, you need to enter:

$form['your_field_name']['#element_validate'] = array('your_field_name_validate');

In this case, your_field_name_validate is the name of the function that will be invoked when the form is submitted. The function needs to be in the same module as the form definition, or in an include file. See an example here. Pasting:

function my_field_name_validate($element, &$form_state, $form) {
   if (empty($element['#value'])) {
     form_error($element, t('This field is required.'));
   }
}
  • Do you know where to find a list of the #attributes that I can use? Or are these the same as all HTML5 attributes that I can apply? So I would need to create custom validation functions for each set of validation rules I would like to apply? What if I want to validate a date making sure it's in the future and making sure that date is after another date entered into the form? For example: My birthday must be in the past. My birthdate must also be AFTER my parent's entered birth date. Would I have to create a function that handles BOTH of these in one function or can I call multiple function – Exziled Jan 3 '16 at 20:07
  • Yes, the attributes you can change with '#attributes' are the same as the HTML attributes for input tags, such as class, maxlength, etc. The only thing you cannot change is the type attribute. This attribute is defined in the "#type" attribute by Form API. – argiepiano Jan 4 '16 at 0:30
  • As for validation, there are some basic validation included in the different #type. You can also use #validate for the whole form, as opposed to #element_validate for each element, and create one function that validates some or all the elements of the form at once. An element can also have more than one validating function - you would define them as an array of functions ['#element_validate'][0] , ['#element_validate'][1] etc. I suggest you really look at the examples of Form API. A LOT of these questions are answered there. – argiepiano Jan 4 '16 at 0:34
  • See also api.drupal.org/api/drupal/includes%21common.inc/group/… for a couple pre-made validation functions – argiepiano Jan 4 '16 at 0:38
  • And see this - look for functions that contain the word 'validate' for other pre-made validation functions. api.drupal.org/api/drupal/includes%21form.inc/group/form_api/7 – argiepiano Jan 4 '16 at 0:41

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.