Zend_Form: Subform Validation

View: New views
7 Messages — Rating Filter:   Alert me  

Zend_Form: Subform Validation

by Steven-81 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi all,

Nice easy question (I hope) but I havent been able to figure it out
from the code or the documentation.

How would you go about displaying a custom error message when a sub
form fails validation? i.e. I've overloaded the subform's isValid()
method to add some custom validation thats reliant on a combination of
all the fields in the subform, if this fails I'd like to pass an error
message to surround the subform as with individual form elements.
Output and decoration issues aside, I can't even see how to log the
error anywhere I'm afraid.

Thoughts?

Cheers,

Steve

Re: Zend_Form: Subform Validation

by Matthew Weier O'Phinney-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-- Steven <gatecrasher1981@...> wrote
(on Thursday, 08 May 2008, 04:42 PM +0100):

> Nice easy question (I hope) but I havent been able to figure it out
> from the code or the documentation.
>
> How would you go about displaying a custom error message when a sub
> form fails validation? i.e. I've overloaded the subform's isValid()
> method to add some custom validation thats reliant on a combination of
> all the fields in the subform, if this fails I'd like to pass an error
> message to surround the subform as with individual form elements.
> Output and decoration issues aside, I can't even see how to log the
> error anywhere I'm afraid.
>
> Thoughts?

Use the sub form's description:

    $subForm->setDescription($errorMessage);
    $subForm->setDecorators(array(
        'FormElements',
        array('Description', array('placement', 'prepend')),
        'Fieldset',
        'DtDdWrapper',
    ));

The above will place the error message above the items in the subform.

--
Matthew Weier O'Phinney
Software Architect       | matthew@...
Zend - The PHP Company   | http://www.zend.com/

Re: Zend_Form: Subform Validation

by Steven-81 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>> How would you go about displaying a custom error message when a sub
>> form fails validation? i.e. I've overloaded the subform's isValid()
>> method to add some custom validation thats reliant on a combination of
>> all the fields in the subform, if this fails I'd like to pass an error
>> message to surround the subform as with individual form elements.

>
> Use the sub form's description:
>
>    $subForm->setDescription($errorMessage);
>    $subForm->setDecorators(array(
>        'FormElements',
>        array('Description', array('placement', 'prepend')),
>        'Fieldset',
>        'DtDdWrapper',
>    ));
>

Excellent thanks!

Although this is only my first attempt at using Zend_Form maybe future
releases would benefit from some kind of mechanism for grouping fields
together with a particular mind to validation? Almost like extending
the display group functionality? Please correct me if I'm missing some
kind of functionality, the flexibility in the component is a little
overwhelming :-)

Cheers,

Steve

Re: Zend_Form: Subform Validation

by Matthew Weier O'Phinney-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-- Steven <gatecrasher1981@...> wrote
(on Thursday, 08 May 2008, 06:04 PM +0100):

> > > How would you go about displaying a custom error message when a sub
> > > form fails validation? i.e. I've overloaded the subform's isValid()
> > > method to add some custom validation thats reliant on a combination of
> > > all the fields in the subform, if this fails I'd like to pass an error
> > > message to surround the subform as with individual form elements.
> >
> > Use the sub form's description:
> >
> >    $subForm->setDescription($errorMessage);
> >    $subForm->setDecorators(array(
> >        'FormElements',
> >        array('Description', array('placement', 'prepend')),
> >        'Fieldset',
> >        'DtDdWrapper',
> >    ));
> >
>
> Excellent thanks!
>
> Although this is only my first attempt at using Zend_Form maybe future
> releases would benefit from some kind of mechanism for grouping fields
> together with a particular mind to validation? Almost like extending
> the display group functionality?

Extending the "sub form" functionality makes more sense, as they are
supposed to be used for logical groupings (validation falls under this
category).

Unfortunately, I have yet to see a use case that can be generalized for
this; in most cases, extending Zend_Form_SubForm and overriding
isValid() appears to make the most sense, as it allows the developer to
customize for the form's particular needs. I am, however, open to adding
such capability if the community can present a good, common use case.

> Please correct me if I'm missing some kind of functionality, the
> flexibility in the component is a little overwhelming :-)

It's a complex component - many areas of responsibility. I'm happy that
you're mostly discovering what you need to do, however.

--
Matthew Weier O'Phinney
Software Architect       | matthew@...
Zend - The PHP Company   | http://www.zend.com/

Re: Zend_Form: Subform Validation

by Steven-81 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

>> Although this is only my first attempt at using Zend_Form maybe future
>> releases would benefit from some kind of mechanism for grouping fields
>> together with a particular mind to validation? Almost like extending
>> the display group functionality?
>
> Extending the "sub form" functionality makes more sense, as they are
> supposed to be used for logical groupings (validation falls under this
> category).
>
> Unfortunately, I have yet to see a use case that can be generalized for
> this; in most cases, extending Zend_Form_SubForm and overriding
> isValid() appears to make the most sense, as it allows the developer to
> customize for the form's particular needs. I am, however, open to adding
> such capability if the community can present a good, common use case.

Mathew,

Yeah I guess a subform is the correct construct when you consider it a
logical grouping of elements, maybe adding additional error message
functionality rather than relying on setDescription() is appropriate?

My use case relates to taking a date as input, I elected to use 3
fields to capture the elements, obviously all 3 fields require
consideration to establish a valid date (in my instance I was
validating that the date was within a boundry also). I elected to go
with a subform rather than using the $context paramater to validate as
that felt too much like a kludge and the error message would relate to
a specific field rather than a group of fields. As another example,
maybe 2 fields detailing colour and size of clothes, individually the
fields may contain valid data, however it is the combinations that
need to be able to generate out of stock messages.

Incidently, is there a convenience method to reset a form once it's
been populated?

Steve

Re: Zend_Form: Subform Validation

by Matthew Weier O'Phinney-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

-- Steven <gatecrasher1981@...> wrote
(on Friday, 09 May 2008, 09:53 AM +0100):

> >> Although this is only my first attempt at using Zend_Form maybe future
> >> releases would benefit from some kind of mechanism for grouping fields
> >> together with a particular mind to validation? Almost like extending
> >> the display group functionality?
> >
> > Extending the "sub form" functionality makes more sense, as they are
> > supposed to be used for logical groupings (validation falls under this
> > category).
> >
> > Unfortunately, I have yet to see a use case that can be generalized for
> > this; in most cases, extending Zend_Form_SubForm and overriding
> > isValid() appears to make the most sense, as it allows the developer to
> > customize for the form's particular needs. I am, however, open to adding
> > such capability if the community can present a good, common use case.
>
> Yeah I guess a subform is the correct construct when you consider it a
> logical grouping of elements, maybe adding additional error message
> functionality rather than relying on setDescription() is appropriate?
>
> My use case relates to taking a date as input, I elected to use 3
> fields to capture the elements, obviously all 3 fields require
> consideration to establish a valid date (in my instance I was
> validating that the date was within a boundry also). I elected to go
> with a subform rather than using the $context paramater to validate as
> that felt too much like a kludge and the error message would relate to
> a specific field rather than a group of fields.

I'd actually suggest that you create a composite element that renders
three input fields using array notation in this situation; that way you
can have error messages that apply to the entity, and it looks logically
like a single element in the form; getValue() could even return the
composite date.

> As another example, maybe 2 fields detailing colour and size of
> clothes, individually the fields may contain valid data, however it is
> the combinations that need to be able to generate out of stock
> messages.

Again, this is a good example for a composite element. I'm thinking I
may need to write a tutorial on this approach...

> Incidently, is there a convenience method to reset a form once it's
> been populated?

No! We should have one, though. Could you put an issue in the tracker
for this?

--
Matthew Weier O'Phinney
Software Architect       | matthew@...
Zend - The PHP Company   | http://www.zend.com/

Re: Zend_Form: Subform Validation

by Steven-81 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

> I'd actually suggest that you create a composite element that renders
> three input fields using array notation in this situation; that way you
> can have error messages that apply to the entity, and it looks logically
> like a single element in the form; getValue() could even return the
> composite date.
>
>> As another example, maybe 2 fields detailing colour and size of
>> clothes, individually the fields may contain valid data, however it is
>> the combinations that need to be able to generate out of stock
>> messages.
>
> Again, this is a good example for a composite element. I'm thinking I
> may need to write a tutorial on this approach...

I thought about this option, but I didnt have a clue how to implement
it as it covered both element rendering and the 'model' side. I must
admit you've kind of lost me with the array notation comment - a
tutorial would be great to see :-)

>> Incidently, is there a convenience method to reset a form once it's
>> been populated?
>
> No! We should have one, though. Could you put an issue in the tracker
> for this?

Will do.

Cheers,

Steve