several buttons in a form
Helle,
Do you know a solution to manage a form with several buttons.
My form is managed in the back end and is for a front end page.
I wish to distinguish a valid button from a cancel one .
In fact, the element name is not defined by TL.
and the value is not set for reading it in a validateFormField hook.
A solution could be to create another form with another button , but it is a bit not very nice.
Has some one an idea ?
Thanks a lot
Re: several buttons in a form
In the form you can define the buttons as:
Code:
<input name="cancelbutton" type="submit" value="Cancel" />
<input name="okbutton" type="submit" value="OK" />
And then in PHP check as:
Code:
if (isset($_POST['cancelbutton'])) {
// do this
} else
if (isset($_POST['okbutton'])) {
// do that
}
Re: several buttons in a form
Since the web works on a positive action method. I understand that a cancel button really serves little` purpose. If you were developing your own BE application, then its also not needed, as TL includes this. In the FE, and working on many websites since the start of the web, I have almost never used a cancel button for a client page. Usually, they just navigate to another page. Cancel buttons, don't really do anything.
You can add the button using HTML in the form if you have to.
Re: several buttons in a form
Thanks Acenes,
The more simple solution arent' not so visible in a first row ....
This way of managing Cancel/OK buttons suits me very well
Nevertheless, I have another little question about it
I put the buttons in a html field at the end of my form
but
this field is placed at the top of the form in the FE
Is there a way to display it at the very end of the form ?
Thanks
Thanks Thyon for replying me once more
I agree with you for a positive way about Human interface web.
I take that for my own way of web developpment
Have a good day !
Re: several buttons in a form
Another relatively easy way would be to COPY the FFL (form field) called submit and make it a Form Field and allow you to add it to the form field types. Then you can modify the standard submit and make it do something else. You'd need to do some development for this.
Re: several buttons in a form
Thanks thyon,
I have found another solution a little more lazy :
The validate button field is replaced by an explanation field
In this field I insert a file tag : {{file::myfile.inc.php?arg_cancel="Cancel"&arg_OK= "OK"}}
in the myfile.inc.php :
echo "<p style=\"text-align: center;\">
<input name=\"cancelbutton\" type=\"submit\" value=".$_GET['arg_cancel']."/>
<input name=\"okbutton\" type=\"submit\" value=".$_GET['arg_OK']."/></p>
That suits me for this time
But in a "second time", I 'll build a form template ...
Re: several buttons in a form
so what does your cancel buttons do? because you can just add TWO submit buttons and then you'll have the same situation? you can name them and label them differently??
Re: several buttons in a form
Hello thyon ,
This previous phase was important for me and it 's not the same situation than before :
because now I can test upon button name, you are right.
Without the item name I wasn't able to switch something.
The item widget in the validateFormField hook didn't give me anything :
value, name, label or id item between which one of the buttons was pressed,
but now, with the item name, it is possible
good TL day!
Re: several buttons in a form
Since TL's form generator allows you to specify the ID, you could have used that instead, but I see that the submit doesn't allow the setting of the name at all..
Re: several buttons in a form
To reopen this topic in one form, why does Contao remove the possibility of using the name attribute of a submit button?
Ie this code:
Code:
// Submit buttons do not use the name attribute
if ($objFields->type == 'submit')
{
$arrData['name'] = '';
}
I'm very curious to the exact reason.
Re: several buttons in a form
TL includes this. In the FE, and working on many websites since the start of the web, I have almost never used a cancel button for a client page. Usually, they just navigate to another page. Cancel buttons, don't really do anything.
Re: several buttons in a form
I understand of that intention, however the situation gets to become a limitation when you have two submit options on a form.
Ie if you have some logic to put into processFormData and it behaves differently depending which submit button the user selects. This behavior is used in the BE for save, saveNClose.
The two ways round the Contao implementation are to
1) Override the Submit widget and set the name variable using the id attribute.
2) Use JS to on submit fill a hidden variable with the submit name.
What I'm trying to understand is the reason for the restriction as there are clear use cases for forms to have multiple submits which aren't clear.