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
A little disappointed in the community...
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:
Hope this helps someone with my same question.
Chuck
----------------------------------------
Chuck Crandall
WebChuck Web Development
chuck at webchuckweb dot com
----------------------------------------
It is...
...very helpful, thanks. If I can follow what you've done with my basic PHP knowledge.
Thanks for the post
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.
to riyengar8: please help on
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!
yep...did it! it's great! so
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...
dprint_r
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....
great
great job !!!
another way to do this
replace [content_type] with your content type.
a_module.info
a_module.module
instead of custom module, put in template.php ?
It may be possible to put the custom function in your custom theme instead of a custom module?
http://drupal.org/node/55126
8.x: #52051: Validation
8.x: #52051: Validation API/UI
Here is how you add validation on existing form
You need to implement hook_form_alter like on link below.
http://www.learn-drupal.in/forums/topic/how-to-add-custom-form-validatio...