You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Malin Ljungh <ma...@kvadrat.se> on 2006/12/02 22:46:34 UTC

Custom validator not called when value is empty?

Good evening!

I have written my own validator which works excellent - as long as a value
is submitted in the field to be validated. Have I missed something here?
What should I do?

To add a required validator may seem to be the obvious solution but the
thing is that the field is not always required - it depends.

Cheers
Malin

Re: Custom validator not called when value is empty?

Posted by fdegrassi <fr...@emaze.net>.
Well, ok, i expressed myself rather poorly.
I did not mean that it is not possible to do cross-field validation with 
validators, but rather that it is not the more "traditional" approach.
I do not like it very much, because it blurs the separation (and 
location) of the two kinds of validation rules. Nonetheless i can 
clearly see that a similar IdentityValidator can come quite handy.
Thanks for the tip

Francesco

andyhot wrote:
> fdegrassi wrote:
>   
>> AFAIK, validators do just that, validate the syntax of values
>> submitted. (field level validation)
>> Validation logic that involves multiple fields (form level validation)
>> should go into the form/submit listener.
>>     
>
> Not quite true...
> Take a look at
> http://issues.apache.org/jira/browse/TAPESTRY-410
> to get some ideas of what can be achieved.
>
>   
>> Regards
>>
>> Francesco Degrassi
>>
>>
>> Malin Ljungh wrote:
>>     
>>> Good evening!
>>>
>>> I have written my own validator which works excellent - as long as a
>>> value
>>> is submitted in the field to be validated. Have I missed something here?
>>> What should I do?
>>>
>>> To add a required validator may seem to be the obvious solution but the
>>> thing is that the field is not always required - it depends.
>>>
>>> Cheers
>>> Malin
>>>
>>>       
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>>     
>
>
>   


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Custom validator not called when value is empty?

Posted by andyhot <an...@di.uoa.gr>.
fdegrassi wrote:
> AFAIK, validators do just that, validate the syntax of values
> submitted. (field level validation)
> Validation logic that involves multiple fields (form level validation)
> should go into the form/submit listener.

Not quite true...
Take a look at
http://issues.apache.org/jira/browse/TAPESTRY-410
to get some ideas of what can be achieved.

>
> Regards
>
> Francesco Degrassi
>
>
> Malin Ljungh wrote:
>> Good evening!
>>
>> I have written my own validator which works excellent - as long as a
>> value
>> is submitted in the field to be validated. Have I missed something here?
>> What should I do?
>>
>> To add a required validator may seem to be the obvious solution but the
>> thing is that the field is not always required - it depends.
>>
>> Cheers
>> Malin
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>


-- 
Andreas Andreou - andyhot@apache.org - http://andyhot.di.uoa.gr
Tapestry / Tacos developer
Open Source / J2EE Consulting 


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Custom validator not called when value is empty?

Posted by fdegrassi <fr...@emaze.net>.
AFAIK, validators do just that, validate the syntax of values submitted. 
(field level validation)
Validation logic that involves multiple fields (form level validation) 
should go into the form/submit listener.

Regards

Francesco Degrassi


Malin Ljungh wrote:
> Good evening!
>
> I have written my own validator which works excellent - as long as a 
> value
> is submitted in the field to be validated. Have I missed something here?
> What should I do?
>
> To add a required validator may seem to be the obvious solution but the
> thing is that the field is not always required - it depends.
>
> Cheers
> Malin
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Re: Re: Custom validator not called when value is empty?

Posted by Sam Gendler <sg...@ideasculptor.com>.
Yeah, you should inject a ValidationDelegate into your page and pass
it to your form as the delegate.  Then, you can stick any arbitrary
error messages in the delegate, attached to particular fields or not.
So long as your error handling in the template renders every error,
rather than just the first one, you should see it, and if you attached
the error to a field, the field will be marked as being in error (the
default is to slap a couple of asterisks after it and place the field
in a div marked with a "red" class, if I remember correctly).  Any
messages from validation or translation errors are also automatically
added to the delegate. There are some nice examples of this in the
freely available chapters (chapter 3, specifically) of the "Enjoy Web
Development with Tapestry" e-book.

--sam

On 12/6/06, Malin Ljungh <ma...@kvadrat.se> wrote:
> Guys - you're great!
>
> Acually Sam was right - by overriding the getAcceptsNull in my validator and
> returning true did the trick.
> I also tried overriding the isRequired method but this method never gets
> called it seems (?)
>
> Though - the other solution from Francesco is very intresting also!
> But I don't really get it - I don't have the "getDelegate" method in my
> page... is there some injection missing here, or how do I do it?
>
>
> Malin
>
>
> On 12/5/06, Sam Gendler <sg...@ideasculptor.com> wrote:
> >
> > Yes, even if you can't get your validator itself to work (and you
> > should be able to), you should definitely just add an error message to
> > your existing validation delegate, rather than making a custom method
> > for each field you want to validate.  That will allow your standard
> > error handling mechanism in the view to render the error rather than
> > requiring special conditionals within your template.
> >
> > --sam
> >
> >
> > On 12/4/06, fdegrassi <fr...@emaze.net> wrote:
> > > I think you can add  custom validation messages to your
> > > ValidationDelegate and display them along with your validators errors.
> > > It should work more or less like this.
> > >
> > > public void doRegister() {
> > >     if (getStart() > getStop()) {
> > >         getDelegate().record(getStartField(), "Start should be less than
> > > Stop");
> > >         return;
> > >     }
> > >     if (getDelegate().getHasErrors())
> > >         return;
> > >
> > >     // to register stuff ....
> > >     // ......
> > > }
> > >
> > > Good luck
> > >
> > > Francesco Degrassi
> > >
> > >
> > > Malin Ljungh wrote:
> > > > Thank you guys! You've been very helpful.
> > > >
> > > > I now make the validation in submit but it's not pretty; in the submit
> > > > listener I have this:
> > > >
> > > > if(!validateFieldX()) {
> > > >   setFieldXValid(false);
> > > >   return null;
> > > > }
> > > >
> > > > and in the html I have an @If to display validation message if
> > validation
> > > > has failed.
> > > > Is there a neater way or is this "how it is done"?
> > > >
> > > > Malin
> > > >
> > > > On 12/3/06, Sam Gendler <sg...@ideasculptor.com> wrote:
> > > >>
> > > >> Why not just build the set of validators for the field dynamically?
> > > >> Assuming you can determine whether the field should be required by
> > the
> > > >> time the field rewinds (rather than being dependant on something not
> > > >> yet rewound), then you can include the required validator in the list
> > > >> only if it is actually required.  However, since the 'required'
> > > >> validator is obviously called whether the field has a value or not, I
> > > >> would suggest taking a look at the source for that validator to
> > > >> determine where you have gone wrong with yours.  It is clearly
> > > >> possible. Without seeing your source, that's about the best I can
> > > >> suggest.
> > > >>
> > > >> --sam
> > > >>
> > > >>
> > > >> On 12/2/06, Malin Ljungh <ma...@kvadrat.se> wrote:
> > > >> > Good evening!
> > > >> >
> > > >> > I have written my own validator which works excellent - as long as
> > a
> > > >> value
> > > >> > is submitted in the field to be validated. Have I missed something
> > > >> here?
> > > >> > What should I do?
> > > >> >
> > > >> > To add a required validator may seem to be the obvious solution but
> > > >> the
> > > >> > thing is that the field is not always required - it depends.
> > > >> >
> > > >> > Cheers
> > > >> > Malin
> > > >> >
> > > >> >
> > > >>
> > > >> ---------------------------------------------------------------------
> > > >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > > >> For additional commands, e-mail: users-help@tapestry.apache.org
> > > >>
> > > >>
> > > >
> > >
> > >
> > > ---------------------------------------------------------------------
> > > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > > For additional commands, e-mail: users-help@tapestry.apache.org
> > >
> > >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Re: Custom validator not called when value is empty?

Posted by Malin Ljungh <ma...@kvadrat.se>.
Guys - you're great!

Acually Sam was right - by overriding the getAcceptsNull in my validator and
returning true did the trick.
I also tried overriding the isRequired method but this method never gets
called it seems (?)

Though - the other solution from Francesco is very intresting also!
But I don't really get it - I don't have the "getDelegate" method in my
page... is there some injection missing here, or how do I do it?


Malin


On 12/5/06, Sam Gendler <sg...@ideasculptor.com> wrote:
>
> Yes, even if you can't get your validator itself to work (and you
> should be able to), you should definitely just add an error message to
> your existing validation delegate, rather than making a custom method
> for each field you want to validate.  That will allow your standard
> error handling mechanism in the view to render the error rather than
> requiring special conditionals within your template.
>
> --sam
>
>
> On 12/4/06, fdegrassi <fr...@emaze.net> wrote:
> > I think you can add  custom validation messages to your
> > ValidationDelegate and display them along with your validators errors.
> > It should work more or less like this.
> >
> > public void doRegister() {
> >     if (getStart() > getStop()) {
> >         getDelegate().record(getStartField(), "Start should be less than
> > Stop");
> >         return;
> >     }
> >     if (getDelegate().getHasErrors())
> >         return;
> >
> >     // to register stuff ....
> >     // ......
> > }
> >
> > Good luck
> >
> > Francesco Degrassi
> >
> >
> > Malin Ljungh wrote:
> > > Thank you guys! You've been very helpful.
> > >
> > > I now make the validation in submit but it's not pretty; in the submit
> > > listener I have this:
> > >
> > > if(!validateFieldX()) {
> > >   setFieldXValid(false);
> > >   return null;
> > > }
> > >
> > > and in the html I have an @If to display validation message if
> validation
> > > has failed.
> > > Is there a neater way or is this "how it is done"?
> > >
> > > Malin
> > >
> > > On 12/3/06, Sam Gendler <sg...@ideasculptor.com> wrote:
> > >>
> > >> Why not just build the set of validators for the field dynamically?
> > >> Assuming you can determine whether the field should be required by
> the
> > >> time the field rewinds (rather than being dependant on something not
> > >> yet rewound), then you can include the required validator in the list
> > >> only if it is actually required.  However, since the 'required'
> > >> validator is obviously called whether the field has a value or not, I
> > >> would suggest taking a look at the source for that validator to
> > >> determine where you have gone wrong with yours.  It is clearly
> > >> possible. Without seeing your source, that's about the best I can
> > >> suggest.
> > >>
> > >> --sam
> > >>
> > >>
> > >> On 12/2/06, Malin Ljungh <ma...@kvadrat.se> wrote:
> > >> > Good evening!
> > >> >
> > >> > I have written my own validator which works excellent - as long as
> a
> > >> value
> > >> > is submitted in the field to be validated. Have I missed something
> > >> here?
> > >> > What should I do?
> > >> >
> > >> > To add a required validator may seem to be the obvious solution but
> > >> the
> > >> > thing is that the field is not always required - it depends.
> > >> >
> > >> > Cheers
> > >> > Malin
> > >> >
> > >> >
> > >>
> > >> ---------------------------------------------------------------------
> > >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > >> For additional commands, e-mail: users-help@tapestry.apache.org
> > >>
> > >>
> > >
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> > For additional commands, e-mail: users-help@tapestry.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Re: Custom validator not called when value is empty?

Posted by Sam Gendler <sg...@ideasculptor.com>.
Yes, even if you can't get your validator itself to work (and you
should be able to), you should definitely just add an error message to
your existing validation delegate, rather than making a custom method
for each field you want to validate.  That will allow your standard
error handling mechanism in the view to render the error rather than
requiring special conditionals within your template.

--sam


On 12/4/06, fdegrassi <fr...@emaze.net> wrote:
> I think you can add  custom validation messages to your
> ValidationDelegate and display them along with your validators errors.
> It should work more or less like this.
>
> public void doRegister() {
>     if (getStart() > getStop()) {
>         getDelegate().record(getStartField(), "Start should be less than
> Stop");
>         return;
>     }
>     if (getDelegate().getHasErrors())
>         return;
>
>     // to register stuff ....
>     // ......
> }
>
> Good luck
>
> Francesco Degrassi
>
>
> Malin Ljungh wrote:
> > Thank you guys! You've been very helpful.
> >
> > I now make the validation in submit but it's not pretty; in the submit
> > listener I have this:
> >
> > if(!validateFieldX()) {
> >   setFieldXValid(false);
> >   return null;
> > }
> >
> > and in the html I have an @If to display validation message if validation
> > has failed.
> > Is there a neater way or is this "how it is done"?
> >
> > Malin
> >
> > On 12/3/06, Sam Gendler <sg...@ideasculptor.com> wrote:
> >>
> >> Why not just build the set of validators for the field dynamically?
> >> Assuming you can determine whether the field should be required by the
> >> time the field rewinds (rather than being dependant on something not
> >> yet rewound), then you can include the required validator in the list
> >> only if it is actually required.  However, since the 'required'
> >> validator is obviously called whether the field has a value or not, I
> >> would suggest taking a look at the source for that validator to
> >> determine where you have gone wrong with yours.  It is clearly
> >> possible. Without seeing your source, that's about the best I can
> >> suggest.
> >>
> >> --sam
> >>
> >>
> >> On 12/2/06, Malin Ljungh <ma...@kvadrat.se> wrote:
> >> > Good evening!
> >> >
> >> > I have written my own validator which works excellent - as long as a
> >> value
> >> > is submitted in the field to be validated. Have I missed something
> >> here?
> >> > What should I do?
> >> >
> >> > To add a required validator may seem to be the obvious solution but
> >> the
> >> > thing is that the field is not always required - it depends.
> >> >
> >> > Cheers
> >> > Malin
> >> >
> >> >
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> >> For additional commands, e-mail: users-help@tapestry.apache.org
> >>
> >>
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Custom validator not called when value is empty?

Posted by fdegrassi <fr...@emaze.net>.
I think you can add  custom validation messages to your 
ValidationDelegate and display them along with your validators errors.
It should work more or less like this.

public void doRegister() {
    if (getStart() > getStop()) {
        getDelegate().record(getStartField(), "Start should be less than 
Stop");
        return;
    }
    if (getDelegate().getHasErrors())
        return;

    // to register stuff ....
    // ......
}

Good luck

Francesco Degrassi


Malin Ljungh wrote:
> Thank you guys! You've been very helpful.
>
> I now make the validation in submit but it's not pretty; in the submit
> listener I have this:
>
> if(!validateFieldX()) {
>   setFieldXValid(false);
>   return null;
> }
>
> and in the html I have an @If to display validation message if validation
> has failed.
> Is there a neater way or is this "how it is done"?
>
> Malin
>
> On 12/3/06, Sam Gendler <sg...@ideasculptor.com> wrote:
>>
>> Why not just build the set of validators for the field dynamically?
>> Assuming you can determine whether the field should be required by the
>> time the field rewinds (rather than being dependant on something not
>> yet rewound), then you can include the required validator in the list
>> only if it is actually required.  However, since the 'required'
>> validator is obviously called whether the field has a value or not, I
>> would suggest taking a look at the source for that validator to
>> determine where you have gone wrong with yours.  It is clearly
>> possible. Without seeing your source, that's about the best I can
>> suggest.
>>
>> --sam
>>
>>
>> On 12/2/06, Malin Ljungh <ma...@kvadrat.se> wrote:
>> > Good evening!
>> >
>> > I have written my own validator which works excellent - as long as a
>> value
>> > is submitted in the field to be validated. Have I missed something 
>> here?
>> > What should I do?
>> >
>> > To add a required validator may seem to be the obvious solution but 
>> the
>> > thing is that the field is not always required - it depends.
>> >
>> > Cheers
>> > Malin
>> >
>> >
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
>> For additional commands, e-mail: users-help@tapestry.apache.org
>>
>>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org


Re: Custom validator not called when value is empty?

Posted by Malin Ljungh <ma...@kvadrat.se>.
Thank you guys! You've been very helpful.

I now make the validation in submit but it's not pretty; in the submit
listener I have this:

if(!validateFieldX()) {
   setFieldXValid(false);
   return null;
}

and in the html I have an @If to display validation message if validation
has failed.
Is there a neater way or is this "how it is done"?

Malin

On 12/3/06, Sam Gendler <sg...@ideasculptor.com> wrote:
>
> Why not just build the set of validators for the field dynamically?
> Assuming you can determine whether the field should be required by the
> time the field rewinds (rather than being dependant on something not
> yet rewound), then you can include the required validator in the list
> only if it is actually required.  However, since the 'required'
> validator is obviously called whether the field has a value or not, I
> would suggest taking a look at the source for that validator to
> determine where you have gone wrong with yours.  It is clearly
> possible. Without seeing your source, that's about the best I can
> suggest.
>
> --sam
>
>
> On 12/2/06, Malin Ljungh <ma...@kvadrat.se> wrote:
> > Good evening!
> >
> > I have written my own validator which works excellent - as long as a
> value
> > is submitted in the field to be validated. Have I missed something here?
> > What should I do?
> >
> > To add a required validator may seem to be the obvious solution but the
> > thing is that the field is not always required - it depends.
> >
> > Cheers
> > Malin
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>

Re: Custom validator not called when value is empty?

Posted by Sam Gendler <sg...@ideasculptor.com>.
Why not just build the set of validators for the field dynamically?
Assuming you can determine whether the field should be required by the
time the field rewinds (rather than being dependant on something not
yet rewound), then you can include the required validator in the list
only if it is actually required.  However, since the 'required'
validator is obviously called whether the field has a value or not, I
would suggest taking a look at the source for that validator to
determine where you have gone wrong with yours.  It is clearly
possible. Without seeing your source, that's about the best I can
suggest.

--sam


On 12/2/06, Malin Ljungh <ma...@kvadrat.se> wrote:
> Good evening!
>
> I have written my own validator which works excellent - as long as a value
> is submitted in the field to be validated. Have I missed something here?
> What should I do?
>
> To add a required validator may seem to be the obvious solution but the
> thing is that the field is not always required - it depends.
>
> Cheers
> Malin
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
For additional commands, e-mail: users-help@tapestry.apache.org