I have a simple form with several checkboxes. Now I need to prevent users to proceed with this form if more then 3 checkboxes are selected. How should this be done?
Prevent the user from proceeding in what fashion?
On general way would be to simply keep track of the number of check boxes checked within your checkbox check methods. In each check method, check your checkbox count variable and you can proceed from there how you want to prevent the user from interacting with your form.
Related
I have a long form that an user has to fill. After the user has clicked submit, I have to show a page with the information the user has filled in the form so that he can check that everything is correct before doing any further processing (i.e., he has to press another submit button or a back button if he detects an error).
Is there an easy way to do this? Is there a better way than creating a new form with hidden values equal to the values submitted in the previous form?
Would it be okay if I just store the information in a session variable and then process it if the user confirms?
You could skin that cat many ways, here's a couple of ideas:
Add a non-mapped confirm input (like a checkbox) to your form. On initial form render with no POST, hide the input. When you get a POST, show the checkbox input & some additional information (please review your data & check to confirm all is OK etc). Don't validate the form or save your data until the confirm input has been checked. You don't need to mess about with hidden fields, the one form will do the job for you.
Save the entity and give it a property e.g. isApproved, that way you can show the data easily & if the user needs to edit, re-use your form. When the user approves the information set isApproved to true. Don't perform any actions on any entities that have isApproved == false. Cons are you then have to manage an isApproved state for an entity.
Using CakePHP I have a page where I have multiple forms. Each form updates a single field on a record. I'm trying to implement a way to submit all the forms through a single "Submit All" button. However, all my solutions so far have been lest than successful.
My first attempt was to create a separate action called editAll in the controller that took an array, but I cannot figure out how to send the data from all the forms to that action without having a hidden form that saves all that data. The second idea was to create some kind of Javascript function that iterated over all the forms to create an array to be sent to the controller's editAll action.
The first implementation did not work, and I couldn't find a reasonable way to implement the second idea.
Basically, I was hoping someone could point me in the direction to submitting multiple forms (or at least the data from multiple forms) at once from a single page.
I assume you don't ALWAYS want to submit them all - if you do, then just make one form. If you're hoping to be able to submit some of them individually, but also be able to submit them all, then you could do something along the lines of this:
Keep all the fields in one form. For each field have a 'submitted' value (1 or 0). If they click the Submit next to an individual field, turn all of the submitted values to 0 except that one, then submit the form.
If they click Submit All, then turn them all to '1' and submit the form.
Then, when you process the data, just strip anything that doesn't have 'submitted' value of 1.
It would still take some work, and you're submitting more data than is necessary, but.... it's an idea.
Is it really necessary to also filter or clean a form's select element since the input is not actually entered by the user but rather the user selects from an already entered options?
Note form submission method is POST. Thanks for the reply.
Yes. You're assuming that the user can't alter it, when in reality they can. Any DOM inspector would allow a user to simply go in and change the values of your options, or even add new options. Always, always validate user input on the server-side, for everything.
I needed to show some preexisting data from a table and but needed to disable them to prevent user from editing them. So i disabled them
$form -> getElement("elementname") -> setAttrib("disable", true);
When I submit the form, I found out, that the form element does not get submitted at all, just because it was disabled. I confirmed this when I tested removing the disable options.
What is happening? Am i doing something wrong? How to solve this?
This is by design, disabled elements do not get submitted with the form.
What you are doing is actually a null practice, no matter what you do to that form in put it will be editable by the end user. You simply cannot trust form input - even hidden fields - to not be tampered with.
Your best bet is to just display the information to the user and load it again after the form has been submitted; at worst store it in a session.
This worked like a charm for me.
It prevents the element from being edited and will pass it through the post.
$this->username->setAttrib('readonly', 'true');
I handle these type of scenarios using hidden elements. Add a hidden element with the same content that is there in your disabled element. When the form is posted, use the value from the hidden element.
But be cautious that the use can modify the value of the hidden element using Firebug or other tools before submitting the form. Always check the form values again before processing.
I have working user registration form.
It consist of Zend Form Elements.
Now I wonder what is the best way to modify this form in order to implement a "switch" in registration form. In other words user should be able to choose which fields to fill (there should be fields as for individual user and company; only few elements should change their labels and "required" state of a selected registration type) and accordingly of made choice validate appropriate fields.
So which is the best and not hard way to make it?
Maybe there are some tutorial or examples?
Thank you!
I think that the best place to change required" states and validators of your form elements is just before validating your form, i.e. $yourForm->isValid($_POST). In this place you would have to change the validators and/or required states of some fields. Which fields would depend on the value of registration type input form element.
As far as showing/hiding specific form elements goes, you could do it on the front-end side using JavaScript.