How to set values displayed after submit using PEAR QuickForm?

2.1k Views Asked by At

Let's say I've got form like this:

$form = new HTML_QuickForm('Novinky');  
$defaults = array('text' => '');
$form->setDefaults($defaults);
$elements['text'] = $form->addElement('textarea', 'text', 'Text', array('cols'=>55, 'rows'=>10, 'id'=>'text'));
$form->addElement('submit','save','Save');
if (isset($_POST[save])) {
    if ($form->validate()) {            
        $form->process(array($this,'writeDB'));
    }
}

After submit I want the default value to be shown instead of the value entered by user. Does Quickform have some functionality to achieve that or do I have to use something clumsy like:

$elements['text']->setValue( $defaults['text']);

.. in which case the setDefaults method seems bit useless to me...

2

There are 2 best solutions below

1
On

you could use

$form->exportValue('text');
0
On

setDefaults method is thought to "pre-fill" the content of the form.

After a submit the values set into the forms are the ones coming from the method $form->getSubmitValues.

If you want / need to change that behaviour then you have no other option than setting the value manually:

code (to be used after you define your element "text" of course):

$text=$form->getElement('text');                    
$text->setValue('your value');