By Apfel007 on
Hi there,
need some help with a cancel button.
I like to add cancel button to node/add form. the problem are the required fields. Could some one give me an hint, how to solve this?
Cheers
Hi there,
need some help with a cancel button.
I like to add cancel button to node/add form. the problem are the required fields. Could some one give me an hint, how to solve this?
Cheers
Comments
When you say "the problem are
When you say "the problem are the required fields", do you mean that clicking the cancel button gives an error if the required fields are not filled?
What I've done with similar cases where I needed a Back button is put a bit into the top of the form validation function that that tests if the Back button was pushed and if so clears out the validation errors thrown by core validation and returns true.
Another option is to take
Another option is to take advantage of button specific validate and submit values for submit buttons:
This also works if you are altering the node add/edit form.
Dang! That's a lot nicer
Dang! That's a lot nicer than what I had. I'll definitely have to add that trick to my repertoire. Thanks.
Thank you both great I will
Thank you both great I will try it the second one I had nearly.... validate.. that should be the trick
Maybe checkout
Maybe checkout http://drupal.org/project/more_node_buttons - currently D7 only, but there is a request for a D6 version here: #784936: This looks great - any chance for a Drupal 6 backport?
--
Tom
www.systemseed.com - drupal development. drupal training. drupal support.
Hello guys! Here is an
Hello guys!
Here is an example of real cancellation of required field validation. Without redirecting user to another page. You can skip validation and perform additional actions when Cancel button is clicked.
http://devengineering.com/best-practices/drupal/cancellation-required-va...
None of the solutions here
None of the solutions here work for the /node/add/page form. I've tried adding .click functions to my cancel button to make it redirect, tried emptying the validation and submit arrays in the form, tried adding my button in an after_build function and specifying there's no validation and writing its own submit handler, I've tried adding the button into the form with javascript rather than the forms api, absolutely NOTHING works. Please if somebody thinks otherwise post and contradict me, I would love to get this working properly. In the end what I had to do was add a the cancel LINK rather than button/input into the div where my other buttons were using jquery. It is ugly and horrible and means I have to keep track of custom button text in javascript and in my custom module which is very upsetting, I'm not at all happy with it.
A simple solution
The Leprechaun says this is a really easy task :)
First, you need to implement hook_form_alter() to alter the standard node form. In your hook_form_alter you need to add your custom submit handler as the the first element in the array of submit handers.
Next implement your custom submit handler.
If you look at the html source of the node form, you will notice the buttons all had a "name" attribute of "op". Therefore, you distinguish which button was clicked by inspecting the "value" attribute submitted:
Again, the trick is to make sure you rearrange the submit handler array so that your handler is called first. In the example above, the Leprechaun redirects the user to the page who's path is mapped to "magically/delicious".
The Leprechaun says he hope this is useful.
Hello Leprechaun! Thank you
Hello Leprechaun! Thank you for your help. What is the array of submit handlers in the form, is it the 'buttons' array? If so then I think I did the equivalent of what you're saying in my post below, and it doesn't work :( Thank you for taking the time to try and help me though.
There are two different
There are two different options that you can do to avoid using jquery. If you're fine with a link (which IMO is better because it is the only easy way to get around required field validation):
This way will create a submit button, but it doesn't get around required field validation. There are ways to get around it (you have to remove all fields from being required - it's really ugly to say the least):
Both of these were tested against Drupal 6 just now and worked fine.
I've tried the second way
I've tried the second way (because I must get around the required field validation), but it still doesn't work for me. This is my code:
This makes the button appear correctly, but the validation for only TWO fields still seem to be triggered. one is the Title field which I can understand but the other is a CCK field. Is there something obviously wrong that I'm just not spotting? Thank you for your help so far.
Why can't you use the link?
Why can't you use the link? The link will skip the required field validation always as well as any other validation. The submit button will not because of the way that drupal handles required fields - those handlers are executed before anything else - always. The solution is really ugly and I didn't put the solution to it - it would involve stepping through each field and removing the required element, and then if the person does click preview or submit then you have to go through and make sure the required fields are there in your own custom code. To say the least it's a hack and I would USE THE LINK.
I was feeling lazy about
I was feeling lazy about CSSing the link so it looks like the rest of my buttons. In the end what I did was create a submit button like this:
and then in a javascript file I added:
And it works fine. Thanks for your help :)
cheers for this sample
cheers for this sample [j_ten_man]. was one of those little tricks what was making me crazy
Hello. Did you check the link
Hello. Did you check the link i've posted? It's step by step tutorial there. You need to define function and assign it to form #after_build property, like this
in this function you need to iterate through all form elements and recursively unset property $element['#needs_validation']
these actions will skip required validation and you will be able to perform your button submit handler. Until you unset '#needs_validation' property and form #validate array, your submit button handler won't be called.
then in submit handler you can do whatever you want, for example redirect to another page with drupal_goto function.
jQuery solution
Is it possible to remove what ever form elements that are required using jQuery.
I can hide() the elements with jQuery but if any are required, then my form fails.
Simple cancel button
For a simple cancel button that does nothing but go back to the previous page, see the comment I added to drupal.org/node/133861
Use drupal custom module:
Use drupal custom module: https://www.drupal.org/project/cancel_button It works great