Re: [fw-general] Zend Form - Setting element required based on a condition

2009-02-19 Thread jkendall



danielito24 wrote:
> 
> . . . if you really implement the preValidation-method - I don't see any
> reason left to implementing the CustomValidator 'FieldDepends' at all. 
> 
> You could implement the whole dependency-issue in the preValidation method
> too, because the general usage is lost anyways. Or am I missing something?
> 

I don't think you're missing anything.  The 'FieldDepends' functionality can
be implemented in the preValidation method, and it's a lot simpler than
putting together a custom validator.  I think it would depend on your use
case.  If you need to add additional validators to a field that depends on
another, you might consider leaving out the 'FieldDepends' validator and
putting all of the functionality into a custom method like preValidation. 
If you don't have any additional validation to add, and there isn't a need
for preValidation(), why create a custom method when you can use a custom
validator?


-
Jeremy Kendall --  http://www.jeremykendall.net http://www.jeremykendall.net 
-- 
View this message in context: 
http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p22099819.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Zend Form - Setting element required based on a condition

2009-02-19 Thread danielito24


jkendall wrote:
> 
> I had some of the same reservations about adding a preValidation() call to
> my controller, but in the end I decided the explicit call was the best way
> to go. 
Hm. I really spend some time dealing with this issue and came to the
conclusion, that - if you really implement the preValidation-method - I
don't see any reason left to implementing the CustomValidator 'FieldDepends'
at all. 

You could implement the whole dependency-issue in the preValidation method
too, because the general usage is lost anyways. Or am I missing something?



-- 
View this message in context: 
http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p22098848.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Zend Form - Setting element required based on a condition

2009-01-31 Thread Bertil Wergelius
This solved my (as yet unanswered) question:
http://www.nabble.com/Zend_Form-validation-when-2-forms-td21747325.html
Which actually was about radiobuttons and conditional required field validation.

A heartfelt thank you Jeremey Kendall
/Bertil Wergelius

2009/1/22 jkendall :
>
>
> Martin Martinov-2 wrote:
>>
>> However, I believe that the recommended way of doing this is by
>> creating a custom validator and utilizing the $context parameter.
>>
>
> I second Martin's suggestion.
>
> I wrote up how I dealt with this at
> http://www.jeremykendall.net/2008/12/24/conditional-form-validation-with-zend_form/
>
> Good luck.
>
>
> -
> Jeremy Kendall --  http://www.jeremykendall.net http://www.jeremykendall.net
> --
> View this message in context: 
> http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p21608829.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>


Re: [fw-general] Zend Form - Setting element required based on a condition

2009-01-22 Thread jkendall



Colin J wrote:
> 
>  I'm on a mission at the moment to keep the controller as clean as
> possible, so I was trying to find a way of not having to call anything but
> $form->isValid from the controller.  If I could somehow hook your
> $form->preValidation call into any calls that a form isValid that would
> suit me better.  Maybe that's just about me creating a custom object that
> extends Zend_Form and adds a preValidation call automatically.
> 

I had some of the same reservations about adding a preValidation() call to
my controller, but in the end I decided the explicit call was the best way
to go.  Not every form will require a preValidation() method, so I'd have to
use a different Zend_Form subclass for those that did.  When I look back at
my controllers in six weeks, I won't remember which form used
preValidation() and which didn't.  Having that call in the controller helps
me to remember and makes debugging and adding features a lot easier.  YMMV.


-
Jeremy Kendall --  http://www.jeremykendall.net http://www.jeremykendall.net 
-- 
View this message in context: 
http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p21609466.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Zend Form - Setting element required based on a condition

2009-01-22 Thread Colin J


jkendall wrote:
> 
> I second Martin's suggestion.
> 
> I wrote up how I dealt with this at
> http://www.jeremykendall.net/2008/12/24/conditional-form-validation-with-zend_form/
> 
> Good luck.
> 
> 

Jeremy, I read your post, it was very useful, thank you.  I'm on a mission
at the moment to keep the controller as clean as possible, so I was trying
to find a way of not having to call anything but $form->isValid from the
controller.  If I could somehow hook your $form->preValidation call into any
calls that a form isValid that would suit me better.  Maybe that's just
about me creating a custom object that extends Zend_Form and adds a
preValidation call automatically.
-- 
View this message in context: 
http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p21609167.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Zend Form - Setting element required based on a condition

2009-01-22 Thread Colin J


Martin Martinov-2 wrote:
> 
> 
> I don't see anything wrong with your approach but I'm not an expert too
> ;-)
> However, I believe that the recommended way of doing this is by
> creating a custom validator and utilizing the $context parameter.
> Take a look at this page in the manual
> http://framework.zend.com/manual/en/zend.form.elements.html and search
> for the note on "Validation Context".
> 
> -- 
> Regards,
> Martin Martinov
> http://mmartinov.com/
> 
> 
Thanks for the pointers Martin

Yes, I looked at creating a custom validator against the emailAddress field
to check the value of the contactBy field in the $context but ran into a
couple of issues.

(a)  I could not find a way of using the standard
Zend_Validate_NotEmpty::IS_EMPTY message (which may or may not be
translated).

(b) I had issues when attaching another validator to the emailAddress field
(e.g. Zend Validate EmailAddress) because to fire the custom validator when
the field is empty you need 'allowEmpty' => false but this, in turn, causes
the EmailAddress validator to run even when the field is empty :-(

My alternative approach to override the isValid method on Zend_Form did seem
simpler (I'm lazy, I suppose, but that's if it's OK to do that in OO terms!)

Thanks again



-- 
View this message in context: 
http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p21608927.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Zend Form - Setting element required based on a condition

2009-01-22 Thread jkendall


Martin Martinov-2 wrote:
> 
> However, I believe that the recommended way of doing this is by
> creating a custom validator and utilizing the $context parameter.
> 

I second Martin's suggestion.

I wrote up how I dealt with this at
http://www.jeremykendall.net/2008/12/24/conditional-form-validation-with-zend_form/

Good luck.


-
Jeremy Kendall --  http://www.jeremykendall.net http://www.jeremykendall.net 
-- 
View this message in context: 
http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p21608829.html
Sent from the Zend Framework mailing list archive at Nabble.com.



Re: [fw-general] Zend Form - Setting element required based on a condition

2009-01-22 Thread Martin Martinov
2009/1/22 Colin J :
>
> Hi
>
> Just wanted to check whether this was being done the 'right' way and welcome
> any feedback.  I would like to set a field to be required based on a certain
> condition, for example, if a form has a 'How would you like to be
> contacted?' dropdown (of Post or Email) and the user selects 'Email' then I
> want an Email Address field to be required.
>
> To do this, I have created the form like this:
>
> class MyForm extends Zend_Form
> {
>public function init()
>{
>$this->addElement(
>'select',
>'contactBy',
>array(
>'label' => 'How would you like to be 
> contacted?',
>'multiOptions' => array(
>'' => '-- Please Select --',
>'post' => 'Post',
>'email' => 'Email'
>)
>'required' => true,
>)
>);
>
>$this->addElement(
>'text',
>'emailAddress',
>array(
>'label' => 'Email Address'
>)
>);
>
>}
>
>/**
>/* override the isValid function of Zend_Form so that I can
>/* set a required field based on a condition
>*/
>
>public function isValid($value)
>{
>if (array_key_exists('contactBy', $value) && 
> $value['contactBy'] ==
> 'email') {
>$this->emailAddress->setRequired(true);
>}
>
>parent::isValid($value);
>}
> }
>
> It is the overriding of isValid() and the call parent::isValid($value) that
> I particularly wanted to ask is a suitable approach in OO terms (not being
> an expert!)?
>
> Thanks, in advance, for any feedback
>
> Colin
> --
> View this message in context: 
> http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p21605109.html
> Sent from the Zend Framework mailing list archive at Nabble.com.
>
>

I don't see anything wrong with your approach but I'm not an expert too ;-)
However, I believe that the recommended way of doing this is by
creating a custom validator and utilizing the $context parameter.
Take a look at this page in the manual
http://framework.zend.com/manual/en/zend.form.elements.html and search
for the note on "Validation Context".

-- 
Regards,
Martin Martinov
http://mmartinov.com/


[fw-general] Zend Form - Setting element required based on a condition

2009-01-22 Thread Colin J

Hi

Just wanted to check whether this was being done the 'right' way and welcome
any feedback.  I would like to set a field to be required based on a certain
condition, for example, if a form has a 'How would you like to be
contacted?' dropdown (of Post or Email) and the user selects 'Email' then I
want an Email Address field to be required.

To do this, I have created the form like this:

class MyForm extends Zend_Form
{
public function init()
{
$this->addElement(
'select',
'contactBy', 
array(
'label' => 'How would you like to be 
contacted?',
'multiOptions' => array(
'' => '-- Please Select --',
'post' => 'Post',
'email' => 'Email'
)
'required' => true,
)
);

$this->addElement(
'text',
'emailAddress',
array(
'label' => 'Email Address'
)
);

}

/**
/* override the isValid function of Zend_Form so that I can
/* set a required field based on a condition
*/

public function isValid($value)
{
if (array_key_exists('contactBy', $value) && 
$value['contactBy'] ==
'email') {
$this->emailAddress->setRequired(true);
}

parent::isValid($value);
}
}   

It is the overriding of isValid() and the call parent::isValid($value) that
I particularly wanted to ask is a suitable approach in OO terms (not being
an expert!)?

Thanks, in advance, for any feedback

Colin
-- 
View this message in context: 
http://www.nabble.com/Zend-Form---Setting-element-required-based-on-a-condition-tp21605109p21605109.html
Sent from the Zend Framework mailing list archive at Nabble.com.