-- fab2008 <[EMAIL PROTECTED]> wrote
(on Wednesday, 12 March 2008, 06:22 PM -0700):
> Hi to everyone, i've just started a new project with Zend Framework (very
> cool), i want to submit a question about Zend_Form usage.
>
> I created a custom subclass of Zend_Form to enclose all common code shared
> by different forms and then for every form I need, I subclass this custom
> class adding elements and validators. Now I have two problems, one with
> filters and one very frustrating with validators. This is the constructor of
> first form (the parent form of every other):
>
> public function __construct($options = null) {
> parent::__construct($options);
> // set class for this form
> $this->setAttrib('class', 'zenitspotform');
> // set the form to be post
> $this->setMethod('post');
> // trim all values provided in form
> $this->setElementFilters(array ('StringTrim'));
The above is going to likely cause issues, but it appears you found that
out... I'll explain more below...
> // set translator
> $this->setTranslator(
> Zend_Registry::get('translator')->getAdapter());
> }
>
> and this is an example form constructor (with only one field but in the
> others, with more fields, the behaviour is the same)
>
> public function __construct($options = null) {
> parent::__construct($options);
>
> $this->addElement('text', 'login',
> array ('label' => 'Username (e-mail)'));
> $this->login->setRequired(true);
> $this->login->addValidator('Date', true);
> $this->login->addValidator('Digits', true);
> $this->login->addValidator('Alpha', true);
> /* this method add a submit button */
> $this->addSubmitButton('Send >>');
> }
>
> If I insert the value ' qwerty ' (without quotes, please note the blanks at
> the begin and at the end of the string) I obtain these errors:
>
> * ' qwerty ' is not of the format YYYY-MM-DD
> * ' qwerty ' contains not only digit characters
> * ' qwerty ' has not only alphabetic characters
>
> Now the problems:
>
> 1) Why StringTrim filter is not applied to submitted value? I expect as
> output the value 'qwerty', but blanks remains in the result...
This is because setElementFilters() applies the filter to all
*currently* registered elements -- it does not apply to elements
registered after the fact.
I'd suggest in your original subclass, you do the following:
abstract class My_Form extends Zend_Form
{
public function __construct($options = null)
{
parent::__construct($options);
// set class for this form
$this->setAttrib('class', 'zenitspotform');
// set the form to be post
$this->setMethod('post');
// set translator
$this->setTranslator(
Zend_Registry::get('translator')->getAdapter());
// custom initializations...
$this->init();
// trim all values provided in form
$this->setElementFilters(array ('StringTrim'));
}
abstract public function init();
}
Then, in the classes subclassing the above, you would *not* override the
constructor, but instead implement the init() method for your
initialization tasks. This would ensure that your StringTrim gets added
to all elements.
> 2) Why all validators are executed???? I passed true as second parameter of
> every addValidator call, so I expect only the date validator should be
> processed.
>
> Two other strange thing, if I remove the setRequired line, only the first
> validator is being executed and the other two don't show their error
> message. As a other solution If I add a NotEmpty validator, I obtain the
> same result...
There was a bug in RC1 with how required elements created the NotEmpty
validator that is prepended to the validation chain -- the
'breakChainOnFailure' flag was getting reset on already instantiated
validators. I fixed this in svn yesterday, and it's available in both
the trunk and the 1.5 release branch.
--
Matthew Weier O'Phinney
PHP Developer | [EMAIL PROTECTED]
Zend - The PHP Company | http://www.zend.com/