[development] How to validate a $node?
Earl Dunovant
prometheus6 at gmail.com
Wed Jun 7 10:42:26 UTC 2006
Got it. In my case I'm sure those fields are there. But that also tells you
the interim (pre-FAPI 2) solution.
On 6/7/06, Robrecht Jacques <robrechtj+drupal at gmail.com> wrote:
>
> Despite what the name implies, node_validate() does not validate the node
> at all. Or at least not completely.
>
> Try to submit a story node using the UI form. If you don't enter a title,
> you'll get an error (Title is required).
>
> But programmatically :
>
> $node->type = 'story';
> $node->uid = 1;
> $node->title = ''; // invalid empty title!
> $node->body = ''; // invalid empty body!
>
> // node_validate() does not check the emptiness of title or body
> // like the UI does, so this validation will work... but shouldn't.
> node_validate($node);
>
> if (form_get_errors()) {
> // I would expect to arrive here as the node was not valid (no
> // non-empty title or body set).
> } else {
> // But instead, I arrive here and the node is saved without
> // trouble. If I go to the edit page of the new node, I'll get
> // the form_errors again.
> node_save($node);
> }
>
> This may be called a bug. Or at least rename node_validate() to
> node_doesn't_validate().
>
> Robrecht
>
>
>
>
> On 6/7/06, Earl Dunovant <prometheus6 at gmail.com> wrote:
> >
> > Question: since node_validate() really does validate your new node
> > (being a standard story node), why can't you use it?
> >
> >
> > On 6/7/06, Robrecht Jacques < robrechtj+drupal at gmail.com> wrote:
> > >
> > > On 6/7/06, Earl Dunovant <prometheus6 at gmail.com> wrote:
> > >
> > > >
> > > > With no claim that it's the Right Way(c)
> > > >
> > >
> > > Again, no validation at all.
> > >
> > > I know how to create a node programmatically. But this way, you can't
> > > be sure it is valid.
> > >
> > > Thanks anyway,
> > > Robrecht
> > >
> > > function syndicator_item_to_node($item_id = NULL) {
> > > > > global $user;
> > > > > if (is_null($item_id)) {
> > > > > $iid = arg(2);
> > > > > if (isset($iid)) {
> > > > > $item =
> > > > > db_fetch_object(db_query(SYNDICATOR_PROMOTE_TO_NODE_QUERY, $iid));
> > > > > }
> > > > > }
> > > > > else if (is_numeric($item_id)) {
> > > > > $item =
> > > > > db_fetch_object(db_query(SYNDICATOR_PROMOTE_TO_NODE_QUERY, $item_id));
> > > > > }
> > > > > if (isset($item)) {
> > > > > $node = new stdClass();
> > > > > $node->is_new = true;
> > > > > $item->description = check_markup($item->description,
> > > > > $node->filter);
> > > > > $node->created = time();
> > > > > $node->date = format_date($item->timestamp, 'custom', 'Y-m-d
> > > > > H:i:s O');
> > > > > $node->type = variable_get('syndicator_publish_node_type',
> > > > > 'none');
> > > > > $node->title = $item->title;
> > > > > $node->filter = variable_get('filter_default_format',
> > > > > FILTER_FORMAT_DEFAULT);
> > > > > $node->body = theme('syndicator_published_content', $item);
> > > > > $node->teaser =
> > > > > node_teaser(theme('syndicator_published_content', $item),
> > > > > variable_get('filter_default_format',
> > > > > FILTER_FORMAT_DEFAULT)); // might be unnecessary
> > > > >
> > > > > $node_options = variable_get('node_options_' . $node->type,
> > > > > array());
> > > > > $node->comment = variable_get('comment_' . $node->type, 2);
> > > > > unset($node->nid);
> > > > > $node = node_submit($node);
> > > > > $node->uid = $user->uid ? $user->uid : 1;
> > > > > module_invoke('node', 'save', $node);
> > > > > }
> > > > > }
> > > > >
> > > > >
> > > > >
> > > > On 6/6/06, Robrecht Jacques <robrechtj+drupal at gmail.com> wrote:
> > > > >
> > > > > - and the real question: how does the following sequence of 4.6translate into
> > > > > 4.7:
> > > > >
> > > > > $node->type = 'story';
> > > > > $node->uid = 1;
> > > > > $node->title = ''; // invalid empty title!
> > > > > $node->body = ''; // invalid empty body!
> > > > >
> > > > > $node = node_validate($node); // if life was only that
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > // that easy in 4.7...
> > > > >
> > > > > if (form_get_errors()) {
> > > > > // we detected the error... do something
> > > > > unset($GLOBALS['form']);
> > > > >
> > > > > // note: we unset $GLOBALS['form'] so the next time we
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > // try this trick, form_get_errors() return the errors
> > > > > // of the last run and not of the previous.
> > > > > // If something like this would be possible in 4.7 it
> > > > > // would be great, but form_set_error() in
> > > > >
> > > > >
> > > > >
> > > > > 4.7 saves the
> > > > >
> > > > > // errors in a static variable inside form_set_error()
> > > > > // instead of in $GLOBALS['form']. I see no way to unset
> > > > > // that...
> > > > > $output .= node_view($node);
> > > > > }
> > > > > else {
> > > > > // no errors? hmm, great, save it... quickly before
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > // "they" introduce something else that breaks
> > > > > // "common sense". Really: why call something
> > > > > // foobar_validate() if it doesn't validate anything
> > > > > // at all... Am I alone in this? I guess so.
> > > > >
> > > > >
> > > > >
> > > > >
> > > > >
> > > > > node_save($node);
> > > > > }
> > > > >
> > > > >
> > > >
> > >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.drupal.org/pipermail/development/attachments/20060607/dc543fa5/attachment-0001.htm
More information about the development
mailing list