Drupalers,

I am looking to add validation to an existing CCK node, and I'm a little shocked that I haven't found an easy way to do this. I understand that I could write my own custom field with validation, which might be an option if I didn't already have this field in production with hundreds of nodes currently being used.

So I need some way to validate a CCK node title. This is even more difficult than the other fields since the input ID is the same for every node, edit-title, so I can't figure out how to use javascript to validate.

I'm guessing there is something I missed. In this beautiful system with all its hooks, is there not a method to validate a CCK node title?

Thanks for any help.

Chuck

Comments

webchuck’s picture

I have to say, I'm a little disappointed in the Drupal community on this one. Up to this point, I've been impressed with the responsiveness on these forums. But this issue turned out to be a fairly simple one, and I didn't get a single comment to point me in the right direction. I guess this forum is just getting too big...

Anyway, here's the solution to validating a CCK node.

1. set up your own custom module. For each site I create a sitehelper.module which holds all functions specific to this site.
2. use the hook_nodeapi to hook into the node creation process.
3. use the node->type to work only on your specific CCK node.
4. define a 'validate' case for the $op variable
5. put your validation code under the validate case

Here's my code:

<?php
function sitehelper_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
if($node->type == 'content_client') {
switch ($op) {
case 'view':
break;

case 'validate':
$newtitle = clean_title($node->title);
if($newtitle != $node->title) {
// VALIDATION GOES HERE  
form_set_error('clientname', 'Client name has illegal characters.');
}
break;

case 'delete':
break;
}
}
}
?>

Hope this helps someone with my same question.

Chuck

----------------------------------------
Chuck Crandall
WebChuck Web Development
chuck at webchuckweb dot com
----------------------------------------

jockox3@drupal.org’s picture

...very helpful, thanks. If I can follow what you've done with my basic PHP knowledge.

riyengar8’s picture

I found this post after a lot of searching, and it turned out to be exactly what I needed. Thanks for documenting, Chuck!

Just in case there are any newbies like me floating around who want to test a field other than the title, you can dereference the $node array pointer in your _nodeapi function to get to the field you want. For instance, in my custom node I had an integer field named 'sku_' whose value I could test thusly:

$node->field_sku_[0][value];

If you are unsure what is in the $node array, you can print all the available members of the $node array with

print_r($node);

Like I said, I am relatively new to PHP and very new to Drupal, so I don't know if either of these techiques are "correct" or adviseable, but they worked for me.

omnyx’s picture

to riyengar8:
please help on this one, it's driving me crazy.
So, in CCK I created a field 'event_time' and a field 'place' (that is a datestamp). I looked at the node_field table in the database and Drupal (or the database) refers to them as 'field_event_time_value' and 'field_place_value'.

But how do I access it in hook_nodeapi()??
using $node->field_event_time_value doesn't seem to give me anything...neither is $node->field_place_value...
how can I access them?

also when you said print_r($node) where exactly can I put this code? Can, and if so how, I put that code within hook_nodeapi()??

many thanks!

omnyx’s picture

yep...did it! it's great!
so basically, priny_r($node) gives me appropriate field names....

but is it possible that those fields can change? i could swear that my date was in the form of
[date]=>array([0]=>array([value]=>1315489 - a unix timestamp

and now it seems that it's like
[date]=>array([0]=>array([day]=>....[month]=>,....[year]=>...)

it's weird that it would change...but i could swear it looked different earlier....i didn't make any changes simply because I have no idea how i could change something like that...

aangel’s picture

If you are unhappy squinting to read the results of print_r, install the Devel module, turn on the permission to use it/view its results and then use dprint_r. Much nicer....

ardi1983’s picture

great job !!!

yechuah’s picture

replace [content_type] with your content type.

a_module.info

name = A module
description = Extra validation to CCK content
core = 6.x

a_module.module

/**
 * Implement hook_form_alter
 * Add a validation callback to the form.
 */
function a_module_form_[content_type]_node_form_alter(&$form, &$form_state) {
  $form['#validate'][] = 'validation_function';
}

/**
 * Validation function
 */
function validation_function($form, &$form_state) {
  // for a cck field 'field_password'
  if (strlen($form_state['values']['field_password']) < 5) {
    form_set_error('field_password', t('Password should consist of 5 or more characters.'));
  }
}
webengr’s picture

It may be possible to put the custom function in your custom theme instead of a custom module?
http://drupal.org/node/55126

kenorb’s picture

sachinwable’s picture

You need to implement hook_form_alter like on link below.
http://www.learn-drupal.in/forums/topic/how-to-add-custom-form-validatio...