By sylvie_n on
I have found the code that I need to include in my node edit form, so that one of two fields is required on a form, from http://drupal.org/node/355264.
However, I need the code to function AFTER the node has been submitted. I.e. the message about "either x or y needs to be filled" to show up after the node has been submitted, along with the other messages about reuired fields.
Currently it shows up before.
Where do I put the code??
thanks
Comments
You want to add a validation
You want to add a validation callback not one on submit.
thanks for your response
thanks for your response nevets.
how would i do that?
any code i can copy or a place i can read up about it?
i'm not a programmer, so i just kind of muddle my way through...learning process :)
So far i have combined code
So far i have combined code from http://drupal.org/node/486340 and http://drupal.org/node/355264 and combined the two to come up with this:
I put the above in my template.php file, but no joy! :(
You say that i shouldn;t be using $form['#submit'][] though, right?
what should i use instead.
thanks
Two things 1) I would use
Two things
1) I would use '#validate' instead of '#submit', validate handlers if they post errors (form_set_error) keep the form from even submitting.
2) I think (not positive) the hook_form_alter() needs to be implemented in a module (not template.php)
Hi Nevets. Thanks for the
Hi Nevets.
Thanks for the help, but unfortunately still no luck.
I created a module, with the following:
But it just doesn't change a thing. A message saying "Description field is required." comes up, but nothing which says "E-mail Addresses or Phone Number must be given."
Does this seem ok as a module to you? I have created it as a weight of 10.
Thank you v.much for your help.
A couple things, your
A couple things, your function does not have the correct arguments defined and checks values incorrectly. This code comes from the Form API reference.
Notice the arguments and the use of $form_state['values']['name'] to check the value. (your code comes from a webform validation function).
Thanks for your reply
Thanks for your reply nevets.
I have tried a few variations of the following:
But, no luck. I have tried cutting out the first function, to see if thats the problem - but still doesn't work.
Am I at all on the right track...or just completely off it?!
Not sure if i used what you said in the right direction?!
At the start of
At the start of required_mysubmit add this line
and when you submit the form all the form field names (keys) should print in the message are
There will not be a 'submitted_tree' element as that is part of webforms. You fields will be something like 'email_address' or if CCK fields, something like 'field_email_address'. So assuming CCK fields, your code should look something like
Also, the field name passed to form_set_error should be a valid form field name.
hi nevets, yeah - thanks for
hi nevets,
yeah - thanks for that - i forgot to put in field.
However, still no luck. The problem lies in the fact that the second function isn't being called. When i put in
drupal_set_message('Form fields: ' . implode(', ', array_keys($form_state['values'])));
nothing is printed on the form.
Hence, either the module is not coming into effect, or the first section of the code isn’t working:
Many thanks for your help on this
In required_form_alter() you
In required_form_alter() you could add a call to drupal_set_message() inside the 'if' statement to make sure you are getting there.
I tried: <?php function
I tried:
and then:
in a module on their own, and they didn't work.
This means that the module itself isn't working, right?
well the module shows up under admin, i have ticked it and pressed save... any ideas why the module isn't coming into play?
many thanks
In required_form_alter()
In required_form_alter() before the 'if' statement add something like
drupal_set_message("required_form_alter(..., ..., $form_id)to verify you are using the correct form id. You also need to change required_form_alter to expect the correct arguments, the correct declaration isrequired_form_alter($form_id, &$form)Hey, It comes back with:
Hey,
It comes back with: required_form_alter(..., ..., Array
when i use drupal_set_message("required_form_alter(..., ..., $form_id");
Did you change the arguments
Did you change the arguments per the second part of the comment?
Yep: <?phpfunction
Yep:
My bad, you had it right,
My bad, you had it right, should be of the form: hook_form_alter(&$form, $form_state, $form_id)
Ok - so we're definately
Ok - so we're definately getting somewhere now. The second function is being called into play after submit.
When I use:
with the inclusion of ['submitted_tree'] in the third line, the message pops up after the submit button has been pressed.
However, it does this whether the phone number input field has been filled or not.
If I don't use ['submitted_tree'] and just use ['values'] then the message never comes up.
'submitted_tree' does not
'submitted_tree' does not apply to CCK fields (its an element of webforms).
What does the message say, what are the form fields it prints out?
ok. Thanks. It prints out
ok. Thanks.
It prints out field_email_address, field_phone_number
The message says "Description field is required."
But does not print out the message with regards to the email address field, whether it is filled out or not.
The code i am using:
hey nevets...got to the root of the problem
ok, heres the thing.
when i add:
it prints out 'Array' as the error message...
however, when i add:
it prints out the body of the node correctly.
Since, the array code is as follows:
[title] => lihli
[taxonomy] => Array
(
[2] => 5
)
[menu] => Array
(
[link_title] =>
[parent] => primary-links:0
[weight] => 0
)
[teaser_include] => 1
[body] => sdsd sd sd sd sd sd sd sd sd s ds d sd s d sd
[format] => 3
[changed] => 1252959082
[op] => Save
[form_build_id] => form-fa745ae804e17faa081ae4412b268b5e
[form_token] => 2777b3b516c5d704b053af340cc3dbd2
[form_id] => job_classified_node_form
[name] => bebecohete
[date] => 2009-09-08 15:34:53 -0400
[log] =>
[status] => 1
[path] =>
[comment] => 1
[field_email_address] => Array
(
[0] => Array
(
[value] => iuh
)
)
How do i print out the value within the email address array? I have tried
but that doesn't work...
Hi. I have resolved this
Hi.
I have resolved this issue.
The code I am using, which has been tested and works is: