[support] is it possible to have validation using CCK fields?
Larry Garfield
larry at garfieldtech.com
Wed Jun 6 04:34:36 UTC 2007
Every site of consequence I've ever done has had a site-specific module to
hold a form alter hook, a one or two entry menu hook, maybe a nodeapi hook,
etc. That's very common.
You can use form_alter to modify *any* form. You just need to know how to
identify which form you're after. For a node edit form, I believe the usual
code is something like this:
if (isset($form['type']) && 'page_node_form' == $form_id) {
...
}
That will let you modify just the add/edit form for page nodes. You can do
pretty much anything you want to it; add validate and submit hooks, add
prefix and suffix attributes, change default values, move things around,
mutate a textfield to a select box (my favorite), etc. The only thing you
need to be careful of is that the form's structure vis a vis what element
names return what value doesn't change, or else the other submit and validate
callbacks that are defined on it will get confused. It doesn't sound like
that's an issue in your case.
The nodeapi validate op is for validating the *node*. You want to validate
the *form*. The difference is rather subtle and, honestly, mostly due to
legacy issues at this point. :-) You can try both ways and see which works
better in this case. It will vary depending on your use case.
On Tuesday 05 June 2007, btully at about.com wrote:
> thanks a bunch Larry.
>
> so I can create a module that only includes one function - an instance of
> hook_form_alter?
>
> also how would I specify which form it is I want to validate, i.e. how
> does Drupal know?
>
> Can hook_form_alter also modify the form fields that have been created
> within CCK, such as adding extra attributes not visible through CCK's add
> form UI? If so this would be super as I would like to add #prefix and
> #suffix tags to several form elements but the CCK UI does not allow for
> this.
>
> also, I found this link a few hours ago <http://drupal.org/node/134289>
> which suggests creating a module that uses hook_nodeapi and specifying
> code within the 'validate' case. Can this work as well?
>
> > No, CCK does not.
> >
> > However, FAPI does. :-) You can use hook_form_alter() in your own module
> > somewhere to add another validation function to the form. In that
> > validation
> > function, just set a form error if you don't like what the user's doing.
> > FAPI will take care of rejecting the form and showing it back to the user
> > with your message. That the form is built with CCK is irrelevant to this
> > task.
> >
> > Cheers.
> >
> > On Tuesday 05 June 2007, Brian Tully wrote:
> >> hey there -
> >>
> >> I'm looking to use CCK fields for defining if an article is exclusive to
> >> a role of users along with a specified timeframe.
> >>
> >> Is it possible to utilize some sort of validation whereby if a role is
> >> specified then a startdate and enddate must be specified?
> >>
> >> The trick is that if they specify a role, I need them to also specify a
> >> startdate and enddate.
> >>
> >> Does the CCK module allow for hooks to alter the form and/or specify
> >> more advanced validation rules?
> >>
> >> i've read about node_alter_form and hook_alter_form, but how does one
> >> use them with CCK fields?
> >>
> >> Any advice would be greatly appreciated.
> >>
> >>
> >> regards,
> >> brian
> >
> > --
> > Larry Garfield AIM: LOLG42
> > larry at garfieldtech.com ICQ: 6817012
> >
> > "If nature has made any one thing less susceptible than all others of
> > exclusive property, it is the action of the thinking power called an
> > idea, which an individual may exclusively possess as long as he keeps it
> > to himself; but the moment it is divulged, it forces itself into the
> > possession
> > of every one, and the receiver cannot dispossess himself of it." --
> > Thomas
> > Jefferson
> > --
> > [ Drupal support list | http://lists.drupal.org/ ]
--
Larry Garfield AIM: LOLG42
larry at garfieldtech.com ICQ: 6817012
"If nature has made any one thing less susceptible than all others of
exclusive property, it is the action of the thinking power called an idea,
which an individual may exclusively possess as long as he keeps it to
himself; but the moment it is divulged, it forces itself into the possession
of every one, and the receiver cannot dispossess himself of it." -- Thomas
Jefferson
More information about the support
mailing list