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.
Related
Here is my problem :
I have a checkout page with multiple forms, the first one is for add people to the order, and the second one is the checkout form. I need to get the people from the first form in my second form, the problem is that these two forms are created when the page is loaded, so at the beggining the second form doesn't know who are the people in the first form, so I can't use form_alter (or only if I refresh the page).
I think the thing to do is add them when the first form is submitted, but how to alter fields of another form ?
Edit:
Yes same page, so i really can't use the form_alter, I try this thing now : ajax_command_replace('#commerce-checkout-form', drupal_render(drupal_get_form('commerce-checkout-form', $order, $payment_page)));
but the drupal_render returns me an empty form (I check with dsm) and it's not replaced in the page
I also try to set a callback on my checkbox field and do a form_rebuild on the callback but doesn't work too... I really don't know how to do that
Do you have any idea of how to make this works ?
I'm using verify.js to validate my forms.
https://github.com/jpillora/verifyjs/issues
Some forms allow users to dynamically add in elements and then remove them. My problem is, if they add in a new form element, try to submit it and the validation fails, then delete the element from the DOM, then submit again, verify.js is still trying to validate the removed elements.
Anyone had any experience of this?
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 have a very simple form with a single autocomplete widget. No submit button. I would like the form to act in such a way that it submits the form when the user selects a suggestion from the autocomplete, but does not submit otherwise. The problem is, the form automatically submits, filled in or not, whenever I press enter. However, if I add a hidden text input box, it resolves the issue, and I can only submit the form by selecting a suggestion from the autocomplete (submission via this mechanism is handled by some jQuery). Is there a more 'graceful' way of turning off the submit-on-return feature? Adding a hidden text input that I don't actually need definitely does not seem like the 'proper' way to do this and is probably a browser-dependent fix anyways (I'm using Chrome).
The "submit on enter" is a browser specific implementation. So I don't think there is anything we can do from JS to turn it off.
You might be able to force the issue by listening to "keypress" event in jQuery, but that seems heavy handed.
Another way you could possibly approach this (in theory, never done this) is using HTML5 Data attributes. i.e. on your form, have
<form data-ready="false">
</form>
Then set that attribute to "true" when you've selected your suggestion item.
In your Form submit handler, check for that attribute before deciding to allow form submission, or use .preventDefault() to stop it from submitting to server.
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.