Community & Support

How to implement extra validation

Hi all,

- I'm using Drupal 5.1.
- I have a content type, say "my_content_type".
- On entering contents, users have to fill in a CCK textfield, say "user_info".
- How can I make sure that the user only fills in text which, among other things, contains a certain string, e.g. "Brussels"? Only when this string is part of the user input, the submitted content item would be accepted.

I suppose I have to write some sort of validation routine. I think I can handle the PHP aspect, but I don't have a clue on the "drupalisation" of this routine. Where would I write that routine??? It would be hugely appreciated if someone could give me a "skeleton" of what to do. I am starting to realize the power of Drupal's hook system, but I don't have the required fluency with it yet. So, some sort of working example would really set me on the right track.

Any kind soul out here willing to help me out?

Thanks,

Ludo

Comments

have a look at this

I can't answer your question right away, but if you know php, you could have a look at the email cck module (http://drupal.org/project/email). It does the same, but with a different set of validation criteria. I think it wouldn't be too hard to convert it into something which suits your needs.
Marc

Thanks, Marcvangend, I'll

Thanks, Marcvangend, I'll take a look at that email cck. Still, I was wondering: isn't there anywhere a good and accessible explanation on how to implement input validation? I would assume that's a rather basic requirement of most any website. Any pointers? Any "skeleton"? Any working examples?

Ludo

Still hoping for a workable

Still hoping for a workable explanation on how to validate textfield input...

Thanks,

Ludo

The nodeapi hook is the easy way

For node based content the easy way is to use the nodeapi hook and the case where $op is 'validate', The following is a working example, under your modules directory add a directory call 'node_validator'. Create the two files below (with the content show). The module file includes the nodeapi hook and once you enable the module every time you save content it will display the node type and the node (these are for debugging). The example validation code works on a content type called 'junk' with a text field called 'value'.

File node_validator.info

name = Node Validation
description = Skeleton for adding addition node validation

File node_validator.module

<?php
function node_validator_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
if ( $op == 'validate' ) {
// The next two lines are meant to you find the information you want to validate
drupal_set_message("Validating node type $node->type");
drupal_set_message("<pre>" . print_r($node, TRUE) . '<pre>');

// Example code for validation (focus on CCK generated types)
// First is this a content type we care about?
switch ( $node->type ) {
case 'junk':
// Check the value is validate
// Here you need to now how to access the value(s)
// Here since we are using CCK we allow for multiple values

// For example we are going to make sure the value field
// is in the range 20 to 200 (including 20 and 200

foreach ( (array)$node->field_value as $instance ) {
// Make sure the value is actually set
if ( isset($instance['value']) ) {
$value = $instance['value'];
if ( is_numeric($value) ) {
// It's a number, is it in range?
if ( $value < 20 || $value > 200 ) {
form_set_error('field_type', "Value must be a number in the range 20 to 200");
}
}
else {
// Not a number, generate an error
form_set_error('field_type', "Value must be a number in the range 20 to 200");
}
}
}
break;
}
}
}

Thanks, nevets, that was

Thanks, nevets, that was Exactly what I was looking for, a good explanation and a bit of working code!!! Thanks So Much, you made my day! Your code does the job, and it opens up the whole validation world to me.

My problem is solved, so I don't want to waste your time any more, but if you or someone else could fill me in on a more theoretical level: where in the Drupal universe does it say that you can solve my validation problem the way you did it? I mean, is there some kind of manual page where it says: "Validation goes like this..."?? I'm asking, because very often my problem with Drupal is that I know exactly what kind of PHP code is needed to solve my problem, but I don't have the faintest idea where to put that code. This to me appears to be of such a basic level that I wonder how I haven't come across some (workable and decently explained) information in this respect, not even after 6 months of drupaling?

Anyway, I'm thrilled that my actual problem has been solved. Thanks again, nevets!

Ludo

Some places to start reading

I tend to read alot, so when I was first learning Drupal I spent a lot of time wandering the site and reading the code. A couple of good places to start are the Module developer's guide and the Drupal APIs (The link is for Drupal 5 APIs). Under the APIs see How to extend existing content types and more generally Module system (Drupal hooks).

Hope this helps.

Also read