Disabling POST data persistense
Permalink
Hey guys
I have a form with two text fields. When the form is validated and submitted successfully, I want to clear my two text fields (so that the entered values are not displayed). If there are validation errors, the values should preferably still be displayed. I make my text fields with the text helper, like so:
echo $form->text('txtName', '', array(
'placeholder' => t('Name of the subscriber'),
'maxlength' => 75
));
I have tried to give a value as parameter, but it seems as if the value I provide is overwritten by the data persistence in concrete5.
How do I accomplish this? If possible, I still want to show values when there are validation errors, but if not possible, then it would be OK to remove data persistence completely from my form.
Thanks in advance!
I have a form with two text fields. When the form is validated and submitted successfully, I want to clear my two text fields (so that the entered values are not displayed). If there are validation errors, the values should preferably still be displayed. I make my text fields with the text helper, like so:
echo $form->text('txtName', '', array(
'placeholder' => t('Name of the subscriber'),
'maxlength' => 75
));
I have tried to give a value as parameter, but it seems as if the value I provide is overwritten by the data persistence in concrete5.
How do I accomplish this? If possible, I still want to show values when there are validation errors, but if not possible, then it would be OK to remove data persistence completely from my form.
Thanks in advance!
These are just meant to be convenience methods. If they're convenient, don't use them. Just use a regular HTML form.
Additionally, it's usually a good idea if a POST succeeds to redirect back to the page. This will clear out the form (and keep your form from re-submitting if you refresh the page.)
Additionally, it's usually a good idea if a POST succeeds to redirect back to the page. This will clear out the form (and keep your form from re-submitting if you refresh the page.)
Thanks guys. I ended up doing a redirect. I was hoping to do it in a nicer way because I also had to preserve state (display a success message). But I did that by having an optional parameter in my controller method; if set, display the message.
You may find this snippet for redirecting to one of (many) intermediaries of use. It only works with fixed text messages. If a t() string has a %s in it, you need something like your extra parameter, or to pass it through the $_SESSION variable. Either way, it can keep view() cleaner:
public function save() { // Usual save processing // expand if or switch as needed and match to tiny functions below if ($error_msg){ $this->redirect('......', 'error_mesage'); } else if ($other_message){ $this->redirect('......', 'other_mesage'); } else { $this->redirect('......', 'view'); } } public function error_mesage() { $this->set('error', t('Something went wrong.')); return $this->view(); }
Viewing 15 lines of 19 lines. View entire code block.
An alternative that I have been using widely is a hacked version of the form helper with an option to turn off the persistence. It also has an option to turn off the id generation (avoiding illegal html). I wanted to get it into the core as an update to the form helper (its backward compatible), but the form helper is already such a spaghetti that it was turned down due to risk side effects.
Feel free to use as you want and feedback any further hacks. Its not on github or anything like that.