[development] How to validate a $node?

Jim Riggs drupal-lists at jimandlissa.com
Wed Jun 7 12:02:35 UTC 2006


On 7 Jun, 2006, at 3:24, Robrecht Jacques wrote:

>> On 6/7/06, Jim Riggs <drupal-lists at jimandlissa.com> wrote:
>> You can take a look at my quotes.module.  It can import several
>> quotes through a single node submission form.  Although the concept
>> is a bit different from what you are doing, it does have to
>> individually validate and submit each quote.  You might be able to
>> get some hints from it...or give me some hints if you see problems
>> with my code!  ;-)
>
> The problem with your importing code is exactly what I was pointing  
> out. Suppose I attach a required taxonomy to the "quotes" content  
> type. You don't set it. You don't validate it. Still the node is  
> created.
> Or if I use hook_form_alter() to add a "#required" to the title  
> textfield so it must be non-empty... you'll happily save the node  
> without the title set.

No, I don't set the required taxonomy, but it will be set if  
required.  The user has to set it.  They are presented with a  
complete node/add form, including taxonomy selection.  It's just that  
in my case they can import many items via the body textarea.  So, the  
required taxonomy is indeed set and validated when node_validate() is  
called for each imported item in quotes_submit().  Same for the  
title.  If it's required, it will have to be set, because it is  
validated before any submit() call can take place.

Again, conceptually what I am doing may be different from what you  
are doing.  My users are presented with the node/add form.  I use the  
"template" node object that was submitted as a base for each of the  
individual nodes I create, so my import gets most of the validation  
and node-submission stuff for free.  The key is in the foreach loop  
in quotes_submit() where I take this template node that has been  
created and set the body, teaser, author, etc., for this individual  
node, validate it, submit it, and save it.  This may be similar to  
what you  need to do;  maybe not.


> You also bail out creating quotes from the first error you  
> encounter while I would like to import the next rows too.

Yeah.  That was just a design decision, though.  I could have easily  
continued the import.





More information about the development mailing list