From: Php Developer on
Hi,

Even if I added a rule, the user can still submit a form with an empty field. What would be the correct version of the following code:

require_once "HTML/QuickForm2.php";
$form = new HTML_QuickForm2('test');
$fieldSet = $form->addFieldset()->setLabel('test');
$title = $fieldSet->addText('title')->setLabel('Title:');
$title->setValue("mytitle");
$title->addRule('required', "Please provide title");
$coverLetter = $fieldSet->addElement(
'textarea', 'Letter',
array('style' => 'width: 300px;', 'cols' => 50, 'rows' => 7),
array('label' => 'Letter:')
);
$coverLetter->setValue("my letter");
$coverLetter->addRule('required', 'Please provide letter');
$fieldSet->addElement('submit', null, 'Submit!');
if (('POST' == $_SERVER['REQUEST_METHOD']) && ($form->validate())) {
$arr = $form->getValue();
echo "<pre>";print_r($arr);echo "</pre>";

}
else echo $form;

Thank you



From: Alexey Borzov on
Hi Anonymous,

On 25.05.2010 12:15, Php Developer wrote:
> Even if I added a rule, the user can still submit a form with an empty field. What would be the correct version of the following code:
>
> require_once "HTML/QuickForm2.php";
> $form = new HTML_QuickForm2('test');
> $fieldSet = $form->addFieldset()->setLabel('test');
> $title = $fieldSet->addText('title')->setLabel('Title:');
> $title->setValue("mytitle");

Here you override whatever value was submitted with your 'mytitle' value...

> $title->addRule('required', "Please provide title");

....and when this rule runs it checks 'mytitle' and finds it valid.

The correct way to set default values is

$form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
'title' => 'mytitle',
'Letter' => 'my letter'
)));
From: Alexey Borzov on
Hi Anonymous,

On 25.05.2010 12:15, Php Developer wrote:
> if (('POST' == $_SERVER['REQUEST_METHOD'])&& ($form->validate())) {

Also the REQUEST_METHOD check is redundant, validate() checks whether the form
was submitted and returns false if it wasn't.
From: Php Developer on
Hi,

Thank your for your answer. It is my first use of pear in general and the code that I adapted to QuickForm2 is a lot clearer and more compact now.

I'm wondering if there is a way to change dynamically the action of the post method. I had already an answer explaing how to do it in the constructor. But it would be a lot better if it is possible to do it for an already instantiated quickform2 object too. May be in the addDataSource function but in that case what would be the syntax?

Thank you very much



----- Original Message ----
From: Alexey Borzov <borz_off(a)cs.msu.su>
To: Php Developer <pdeveloper(a)rocketmail.com>
Cc: pear-general(a)lists.php.net
Sent: Tue, May 25, 2010 4:40:42 AM
Subject: Re: [PEAR] QuickForm2 validation

Hi Anonymous,

On 25.05.2010 12:15, Php Developer wrote:
> Even if I added a rule, the user can still submit a form with an empty field. What would be the correct version of the following code:
>
> require_once "HTML/QuickForm2.php";
> $form = new HTML_QuickForm2('test');
> $fieldSet = $form->addFieldset()->setLabel('test');
> $title = $fieldSet->addText('title')->setLabel('Title:');
> $title->setValue("mytitle");

Here you override whatever value was submitted with your 'mytitle' value...

> $title->addRule('required', "Please provide title");

....and when this rule runs it checks 'mytitle' and finds it valid.

The correct way to set default values is

$form->addDataSource(new HTML_QuickForm2_DataSource_Array(array(
'title' => 'mytitle',
'Letter' => 'my letter'
)));

-- PEAR General Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



From: Alexey Borzov on
Hi,

On 27.05.2010 0:09, Php Developer wrote:
> I'm wondering if there is a way to change dynamically the action of the post method. I had already an answer explaing how to do it in the constructor. But it would be a lot better if it is possible to do it for an already instantiated quickform2 object too. May be in the addDataSource function but in that case what would be the syntax?

If I understand correctly what you are trying to achieve:

$form->setAttribute('action', 'new value');

See the API docs for HTML_Common2 package:
http://pear.php.net/package/HTML_Common2/docs/2.0.0RC1/
 | 
Pages: 1
Prev: QuickForm2 (form action)
Next: Addrule not working