Webscaped.com

Custom field validation in Drupal

Thursday March 12th, 2009
Comment Icon 2 comments

So, here we go. A simple helper module that will:

  • Check the title field against a validation rule
  • Output an error to the screen, and provide focus on the incorrect field

Modules required:

To keep this simple, I’ll be validating against the story content type and the title field. The steps are the same if you have a custom content type, or are using CCK.

First step is to use the devel module to find out some information about the form we’re trying to validate.

Go to “Administer” -> “Modules”.

Enable the Devel module, and the Theme Developer sub module. After enabling, you should see a little box appear in the bottom left, which we’ll use in a minute.

Now head to the “Create Story” page in the content section.

We’ll be validating this form, but we need to know what the form, and fields are actually called.

Now, click the “Theme Info” helper located in the bottom left of the screen. This will bring up a page inspecting utility, that will tell more about what’s on the page. It’s useful for theme developing, as it can quickly tell you which .tpl file you need to create to theme a screen element.

Try it by clicking on the form somewhere. We need to find information about the form-id.

form-id

Clicking the parent object in the devel pop-up (Usually something like array…blah blah) will result in more options, with a structured tree view. Look around until you find form-id, in here you’ll find the #value key, which should say something like “story_node_form”.

Ok, so now we know this form is called “story_node_form”, we’ll use that in our validation module.

Next, we need to find the id of the field we want to validate.

field-id

Click on the title field, browse through the devel pop-up until you find #name….which is “title”. This will differ greatly, especially for CCK fields.

Ok, so we have the form name “story_node_form”, and the field “title”.

Lets start the module.

Open up a file browser and browse to your “modules” directory. Inside this create a directory called “test_helper”. It would be best to replace “test” with your site name. I.E webscaped_helper.

In that directory, create two files. Test_helper.module and Test_helper.info.

Put the following into test_helper.info:

;$Id$
name = "Helper Validation"
description = "Site specific validation"
version = "6.x"
core = "6.x"
package = "Test Site"

In test_helper.module, add in:

<?php
// $Id$
/**
* @file
* Simple module to run some custom validation on a story node
*/
 
/*
* Implementation of hook_form_alter()
*
*/
function test_helper_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'story_node_form') {
$form['#validate'] = array('_test_helper_story_node_form_validate');
}
return $form;
}
 
/*
* Validates the story node form, and throws an error to the user if validation fails
*
*/
function _test_helper_story_node_form_validate($form, &$form_state) {
form_set_error('title', t('Out to lunch'));
}

0.O No php closing tag? Yes, its considered bad, in Drupal, its considered normal. Something about any errors won’t cause your site to explode…or something.

As you can see, we used the previous form-id in the test_helper_form_alter function. This form_alter hook is called by Drupal, it checks to see if the current form matches your form-id, and adds a validation function to it.

Inside the validation function, we call a Drupal function (form_set_error) that causes an error to appear to the user. The first argument is the field to focus on, and the second is the error message shown to the user.

So some fairly basic files. Save them.

Go back into your Drupal admin area and enable your new module.

Now go back and try to submit a story. It should stop you with an error.

error-1

Ok, so inside the validation function, you can put your own PHP based validation scripts.

Go back into your test_helper.module file, and replace the validation function with this:

function _test_helper_story_node_form_validate($form, &$form_state) {
if($form_state['values']['title'] == "Testing")
{
form_set_error('title', t('No testing allowed!'));
}
 
}

Here, we are using the “title” field we found the name of using the Devel module. If somebody enters “Testing”, an error will display, but the form will submit correctly if they want anything else.

So, save that change and have a go.

Sprinkle in your own PHP validation, and you’re one step closer to stopping users from submitting rubbish!

Related posts:

2 comments

  1. Rimu

    In drupal 6, hook_nodeapi is cleaner

  2. John

    Thanks for this tutorial. I did the steps above in Drupal 7 without any problems, works great.
    One minor problem though:
    1. Create a story
    2. Edit the story and try to change it to “testing”, validation will stop you from saving
    3. Now try to delete.
    The validation hook is triggered on the delete as well. By calling form_set_error delete will not be allowed.
    Strange huh? Of course there is a workaround for the end-user, but what is the best-practice? Test the clicked-button in $formstate?

Leave a Reply

Copyright © Michael Adams     powered by WordPress

    RSS Feed HTML 4.01 Valid CSS Valid