You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by "Marc A. Donis" <ma...@runbox.com> on 2007/09/29 18:51:56 UTC

T5 form field validation, component injection

Hi all,

I am trying to understand how to implement custom validation of form fields. 
Here's what I've got so far:

CreateAccount.java:

public class CreateAccount {
    @InjectComponent
    private Form createAccountForm;

    @OnEvent(value = "validate", component = "createAccountForm")
    void validate() throws ValidationException {
        log.debug("validate");
        if (password == null || !password.equals(password2)) {
            createAccountForm.getDefaultTracker().getErrors().add("passwords 
don't match");
        }
    }
}

and CreateAccount.html:

 <t:form t:id="createAccountForm">
  <t:errors/>
     <t:passwordfield t:id="password" t:validate="required,minLength=4"/>
     <t:passwordfield t:id="password2" t:validate="required"/>
... etc


This is failing because Tapestry is trying to cast CreateAccount to Form... 
but why?  Am I going about this all wrong?  What am I missing here?

I feel a bit like I am stumbling around in the dark.  Does anybody know of 
some T5 examples I can look at, just to get me going?

tia,
Marc


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


Re: T5 form field validation, component injection

Posted by "Marc A. Donis" <ma...@runbox.com>.
Thanks for that fantastic reply.  Yes, in fact, the Component annotation is 
exactly what I was missing.  Don't know why I didn't see it before...
And that Input Validate page you refered me to is quite a help.  Reading 
avidly.

Thanks!

Marc

----- Original Message ----- 
From: "lasitha" <la...@gmail.com>
To: "Tapestry users" <us...@tapestry.apache.org>
Sent: Saturday, September 29, 2007 19:40
Subject: Re: T5 form field validation, component injection


> Hello Marc,
>
> I think you want the @Component annotation instead of
> @InjectComponent.  The latter is "Used exclusively inside a mixin to
> connect the mixin to the component to which it is attached."[1]
>
> I haven't tested whether this will solve your problem, but it seems 
> likely.
>
> A good starting point for basic form validation is probably the 'Input
> Validation' page of the online documentation[2].  It comes with a
> simple example and a lot of useful info.
>
> As an aside, you can use Form.recordError() as a convenience around
> getDefaultTracker().getErrors().add().
>
> HTH.  Cheers,
> lasitha.
>
> [1] 
> http://tapestry.apache.org/tapestry5/tapestry-core/apidocs/org/apache/tapestry/annotations/InjectComponent.html
> [2]
> http://tapestry.apache.org/tapestry5/tapestry-core/guide/validation.html
>
>
> On 9/29/07, Marc A. Donis <ma...@runbox.com> wrote:
>> Hi all,
>>
>> I am trying to understand how to implement custom validation of form 
>> fields.
>> Here's what I've got so far:
>>
>> CreateAccount.java:
>>
>> public class CreateAccount {
>>     @InjectComponent
>>     private Form createAccountForm;
>>
>>     @OnEvent(value = "validate", component = "createAccountForm")
>>     void validate() throws ValidationException {
>>         log.debug("validate");
>>         if (password == null || !password.equals(password2)) {
>> 
>> createAccountForm.getDefaultTracker().getErrors().add("passwords
>> don't match");
>>         }
>>     }
>> }
>>
>> and CreateAccount.html:
>>
>>  <t:form t:id="createAccountForm">
>>   <t:errors/>
>>      <t:passwordfield t:id="password" t:validate="required,minLength=4"/>
>>      <t:passwordfield t:id="password2" t:validate="required"/>
>> ... etc
>>
>>
>> This is failing because Tapestry is trying to cast CreateAccount to 
>> Form...
>> but why?  Am I going about this all wrong?  What am I missing here?
>>
>> I feel a bit like I am stumbling around in the dark.  Does anybody know 
>> of
>> some T5 examples I can look at, just to get me going?
>>
>> tia,
>> Marc
>>
>>
>> ---------------------------------------------------------------------
>> 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: T5 form field validation, component injection

Posted by lasitha <la...@gmail.com>.
Hello Marc,

I think you want the @Component annotation instead of
@InjectComponent.  The latter is "Used exclusively inside a mixin to
connect the mixin to the component to which it is attached."[1]

I haven't tested whether this will solve your problem, but it seems likely.

A good starting point for basic form validation is probably the 'Input
Validation' page of the online documentation[2].  It comes with a
simple example and a lot of useful info.

As an aside, you can use Form.recordError() as a convenience around
getDefaultTracker().getErrors().add().

HTH.  Cheers,
lasitha.

[1] http://tapestry.apache.org/tapestry5/tapestry-core/apidocs/org/apache/tapestry/annotations/InjectComponent.html
[2]
http://tapestry.apache.org/tapestry5/tapestry-core/guide/validation.html


On 9/29/07, Marc A. Donis <ma...@runbox.com> wrote:
> Hi all,
>
> I am trying to understand how to implement custom validation of form fields.
> Here's what I've got so far:
>
> CreateAccount.java:
>
> public class CreateAccount {
>     @InjectComponent
>     private Form createAccountForm;
>
>     @OnEvent(value = "validate", component = "createAccountForm")
>     void validate() throws ValidationException {
>         log.debug("validate");
>         if (password == null || !password.equals(password2)) {
>             createAccountForm.getDefaultTracker().getErrors().add("passwords
> don't match");
>         }
>     }
> }
>
> and CreateAccount.html:
>
>  <t:form t:id="createAccountForm">
>   <t:errors/>
>      <t:passwordfield t:id="password" t:validate="required,minLength=4"/>
>      <t:passwordfield t:id="password2" t:validate="required"/>
> ... etc
>
>
> This is failing because Tapestry is trying to cast CreateAccount to Form...
> but why?  Am I going about this all wrong?  What am I missing here?
>
> I feel a bit like I am stumbling around in the dark.  Does anybody know of
> some T5 examples I can look at, just to get me going?
>
> tia,
> Marc
>
>
> ---------------------------------------------------------------------
> 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