I am building a directory listing type site and trying to keep all my listings in one node type. There are different options for the types of listings available, and I am using some javascript to show/hide forms on the node/add page. I am also doing quite a bit of validation from hook_nodeapi, of individual field formatting, and so on. One of the things I'd like to be able to do, is upon validation of the node, throw out any form values that do not match the type of listing they have chosen (so that the values won't be shown on the node preview, for example). What is the best way to go about this? I see that in my hook_nodeapi under the validate op, I am getting the node, and the $form itself, not $form_state or $form_values. The submitted data that I wish to discard is in both the $node object, and the $form array under #post among other places. Drupal 6.x btw. -Mike __________________ Michael Prasuhn mike@mikeyp.net http://mikeyp.net
On 20-Oct-08, at 11:05 PM, Michael Prasuhn wrote:
I am building a directory listing type site and trying to keep all my listings in one node type.
I'm curious as to why CCK with a bunch of optional fields doesn't work for this. Could you not do a bit of custom PHP to validate fields within CCK?
One of the things I'd like to be able to do, is upon validation of the node, throw out any form values that do not match the type of listing they have chosen (so that the values won't be shown on the node preview, for example).
I don't think that's really the purpose of the validation hook. Typically, you should throw errors with form_set_error() to indicate why the data submitted is invalid. Even if you could make it work, I would think you'd have some pretty confused users wondering where their data went, thinking that the preview or the site was broken.
What is the best way to go about this?
Perhaps presave, or prepare hooks could do it? But based on the information you've given, I really think this is a solution for multiple content types + CCK. HTH, --Andrew
On 21 Oct 2008, at 6:29 AM, Andrew Berry wrote:
I'm curious as to why CCK with a bunch of optional fields doesn't work for this. Could you not do a bit of custom PHP to validate fields within CCK? because sometimes there is a lot of overhead that comes with cck that you don't really need.
and you spend more time working against what cck gives you by default than you do just writing it yourself and the dependencies for your module explode making upgrades a lot messier Anyway, to answer the question, i know in d4.7/d5 you could do form_set_value('field][field', 'value') to set fields in the validate functions, i don't know if in D6 you can just modify the $form_state.
On Oct 20, 2008, at 9:29 PM, Andrew Berry wrote:
I'm curious as to why CCK with a bunch of optional fields doesn't work for this. Could you not do a bit of custom PHP to validate fields within CCK?
I am using CCK fields. This application requires the use of conditionally required/available fields. In order to make this work reliably, I am discarding data submitted for unavailable fields on validation, instead of trying to validate a field that doesn't correspond to the listing type. On Oct 20, 2008, at 11:21 PM, Derek Wright wrote:
That's because of this critical bug in the D6 API:
http://drupal.org/node/241364 "hook_validate() doesn't get $form_state passed to it"
Sadly, since that'd be a pretty major API change in a supposedly stable release series of core, it's probably not going to be fixed. : ( But, it makes hook_nodeapi() quite useless in many cases, and has caused (and will continue to cause) all sorts of grief for a variety of modules. Tragically, no one noticed until after 6.0 was out. The current work-around is to form_alter() and add your own #validate handler, which *does* get a copy of $form_state as nature intended. Arguably, that's cleaner than using hook_nodeapi() in the first place, but then what's the point of having hook_nodeapi('validate') at all?
I suppose whether or not $form_state should be in hook_nodeapi('validate') is debatable. I can see the utility of it here, yet I can see the argument that it is a misuse of hook_nodeapi, as it really has nothing to do with the node object itself, but the form. Thanks for the suggestion with the custom validation function. On Oct 20, 2008, at 11:13 PM, Adrian Rossouw wrote:
Anyway, to answer the question, i know in d4.7/d5 you could do form_set_value('field][field', 'value') to set fields in the validate functions, i don't know if in D6 you can just modify the $form_state.
This would be ideal, however in D6 the $form_state is a required parameter of form_set_value() and the absence of $form_state in hook_nodeapi('validate') is the problem I was running into. Derek's suggestion of adding my own validation with hook_form_alter() makes the most sense, as it will pass $form_state to my function. -Mike __________________ Michael Prasuhn mike@mikeyp.net http://mikeyp.net
On Oct 20, 2008, at 8:05 PM, Michael Prasuhn wrote:
I see that in my hook_nodeapi under the validate op, I am getting the node, and the $form itself, not $form_state or $form_values.
That's because of this critical bug in the D6 API: http://drupal.org/node/241364 "hook_validate() doesn't get $form_state passed to it" Sadly, since that'd be a pretty major API change in a supposedly stable release series of core, it's probably not going to be fixed. : ( But, it makes hook_nodeapi() quite useless in many cases, and has caused (and will continue to cause) all sorts of grief for a variety of modules. Tragically, no one noticed until after 6.0 was out. The current work-around is to form_alter() and add your own #validate handler, which *does* get a copy of $form_state as nature intended. Arguably, that's cleaner than using hook_nodeapi() in the first place, but then what's the point of having hook_nodeapi('validate') at all? This is yet another good anecdote to encourage contrib maintainers to make heavy use of the UNSTABLE-N tags that webchick is making from D7 core, so that we flesh out broken parts of the API before it's too late to fix them. Meanwhile, it'd be nice if Gabor and/or Dries would weigh in on #241364 and indicate if it's a D7 only issue, or if we should attempt to make a solution for D6, too. So far, none of the core maintainers (or major contributors, for that matter) have participated in that issue. Cheers, -Derek (dww)
Seems like the lack of functionality of hook_nodeapi('validate') should be documented in the D6 and FAPI docs in BIG BOLD LETTERS.
On Tue, Oct 21, 2008 at 6:06 AM, Chris Johnson <cxjohnson@gmail.com> wrote:
Seems like the lack of functionality of hook_nodeapi('validate') should be documented in the D6 and FAPI docs in BIG BOLD LETTERS.
I think you meant to say the problem with hook_validate(). I'll also throw out the reminder that anyone with a CVS account can make corrections to the docs. andrew
participants (6)
-
Adrian Rossouw -
Andrew Berry -
andrew morton -
Chris Johnson -
Derek Wright -
Michael Prasuhn