You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by ja...@w9z.org on 2011/05/24 17:30:51 UTC

dynamic validation for required / not required

I know I can make a field required like this:
<t:textfield value="myTextField" validate="required"/>

But how can I make it programmatically determine whether or not the field is required?

I have tried:

<t:textfield value="myTextField" validate="${validationText}"/>

public String getValidationText() {
   if (fieldRequired) { return "required" }
   else { return ""}


But that doesn't seem to work.  Tapestry says it can't coerce a string into a Validator.

Am I overlooking something simple or is there a better approach to accomplish this?

Jack


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


Re: dynamic validation for required / not required

Posted by Josh Canfield <jo...@gmail.com>.
> Thats helpful, but my problem is that I don't know the field id either, so I can't inject the field.  The fields are determined at run time (attributes
> that the user can add or remove) so it has to be flexible.

I'm going to assume that you are not dynamically generating your .tml
files and that you have a <t:textfield..> in a <t:loop> rendering your
attributes. In that case you have only one field, and can know the id.

> I supposed I could put every TextField in an IF statement where the IF has "required" and the ELSE doesn't, but that seems like I'm missing a better way to do it.
If you can wrap the TextField in an If, then surely you can use a
getDynamicConstraints() type solution and switch the return value
based on whatever you'd put in the if.

public FieldValidator getDynamicConstraints() {
        if (currentAttributeIsRequired()) {
            constraint = "required";
        }
        return source.createValidators(valueField, constraint);
    }


Josh

On Tue, May 24, 2011 at 11:47 AM,  <ja...@w9z.org> wrote:
> Thats helpful, but my problem is that I don't know the field id either, so I can't inject the field.  The fields are determined at run time (attributes that the user can add or remove) so it has to be flexible.
>
> I supposed I could put every TextField in an IF statement where the IF has "required" and the ELSE doesn't, but that seems like I'm missing a better way to do it.
>
> Jack
>
> -----Original Message-----
> From: "Thiago H. de Paula Figueiredo" <th...@gmail.com>
>
> @Inject FieldValidatorSource and use its FieldValidator
> createValidators(Field field, String specification) method to generate a
> FieldValidator. Then create a FieldValidator getValidator() method using
> createValidators() and add t:validate="prop:validator" to your TextField.
>
> Example (taken from
> http://tapestry.1045711.n5.nabble.com/Validation-based-on-condition-td2435565.html):
>
> @Inject
> private FieldValidatorSource source;
>
> @InjectComponent
> private Field yourField;
>
> public FieldValidator getDynamicConstraints() {
>        return source.createValidators(yourField, "required", null);
> }
>
>
>
> ---------------------------------------------------------------------
> 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: dynamic validation for required / not required

Posted by George Christman <gc...@cardaddy.com>.
First time I've stumped you :) 

Anyhow, I found a work around. Just had to use single quoates in the
parameter to pass back my string value, then I could handle setting up the
Field on the backend.

    t:validator="prop:getAuthorizerValidator('authorizer')"

    public FieldValidator getAuthorizerValidator(String clientId) { 
        String validationString = "none";
        Field f = null; 

        if(formValidations.contains(clientId) &&
formValidationAction.equals(action)) {
            validationString = "required";
        }        
        
        f = (Field)componentResources.getEmbeddedComponent(clientId); 
        
        return source.createValidator(f, validationString, null);
    } 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/dynamic-validation-for-required-not-required-tp4422440p5712122.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Fri, 18 May 2012 16:40:43 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> Hi Thiago, do you have any suggestions on my previous question?

If I had, I'd have posted it, I guarantee. :)

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by George Christman <gc...@cardaddy.com>.
Hi Thiago, do you have any suggestions on my previous question?

--
View this message in context: http://tapestry.1045711.n5.nabble.com/dynamic-validation-for-required-not-required-tp4422440p5712101.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: dynamic validation for required / not required

Posted by George Christman <gc...@cardaddy.com>.
it now produces the following exception. 

java.lang.RuntimeException
Error parsing property expression
'getContactNameValidator(component:contactName)': line 1:23 no viable
alternative at input '('.
java.lang.RuntimeException
line 1:23 no viable alternative at input '('

--
View this message in context: http://tapestry.1045711.n5.nabble.com/dynamic-validation-for-required-not-required-tp4422440p5710856.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Wed, 16 May 2012 10:05:47 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> Thiago, I'm getting the following exception while trying to pass back the
> parameter.

Oops, it should be  
t:validate="prop:getContactNameValidator(component:contactName)". I forgot  
the default binding for the validate parameter is 'validator'.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by George Christman <gc...@cardaddy.com>.
Thiago, I'm getting the following exception while trying to pass back the
parameter. 

org.apache.tapestry5.ioc.internal.util.TapestryException
Failure reading parameter 'validate' of component
Purchase_Request:contactname: Unexpected character '(' at position 24 of
input string: getContactNameValidator(component:contactName)

--
View this message in context: http://tapestry.1045711.n5.nabble.com/dynamic-validation-for-required-not-required-tp4422440p5710848.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 15 May 2012 17:33:06 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> lol I spoke to soon, I don't remember how to pass it back as a  
> parameter....
> Is it something like this?
> t:validate="prop:contactNameValidator(literal:contactName)"
> public FieldValidator getContactNameValidator(String clientId) {

More like this:

t:validate="getContactNameValidator(component:contactName)"

public FieldValidator getContactNameValidator(Field field) {

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by George Christman <gc...@cardaddy.com>.
lol I spoke to soon, I don't remember how to pass it back as a parameter.... 

Is it something like this?

t:validate="prop:contactNameValidator(literal:contactName)"

public FieldValidator getContactNameValidator(String clientId) {

--
View this message in context: http://tapestry.1045711.n5.nabble.com/dynamic-validation-for-required-not-required-tp4422440p5710385.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: dynamic validation for required / not required

Posted by George Christman <gc...@cardaddy.com>.
Ah yes, I forgot about that solution. Good idea, Thanks 

--
View this message in context: http://tapestry.1045711.n5.nabble.com/dynamic-validation-for-required-not-required-tp4422440p5710378.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 15 May 2012 16:22:50 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> So you can't dynamically get the field id's like you can in mixens with
> ClientElement.getClientId

Yes, but if you're not in a mixin, how do you know which field we're  
dealing? ;-) Maybe I just didn't understand your question, of course.

> and use a single method?

Yes if you pass the field as a parameter to the getFieldValidator()  
method. Use the component prefix binding.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by George Christman <gc...@cardaddy.com>.
So you can't dynamically get the field id's like you can in mixens with
ClientElement.getClientId and use a single method?

--
View this message in context: http://tapestry.1045711.n5.nabble.com/dynamic-validation-for-required-not-required-tp4422440p5710318.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 15 May 2012 15:01:57 -0300, George Christman  
<gc...@cardaddy.com> wrote:

> How could I use one method with multiple fields. Not sure how the method
> detects the client id.

Use different methods for different fields with different logics.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by George Christman <gc...@cardaddy.com>.
I know this is a pretty old post, but had a question. 

How could I use one method with multiple fields. Not sure how the method
detects the client id. 

    @Inject
    private FieldValidatorSource source;
    
    @InjectComponent
    private Field contactName; 
    
    @InjectComponent
    private Field contactEmail; 

    public FieldValidator getFieldValidator() {
        String validationString = "none";
        if(attributeDefinition.isRequired()) {
            validationString = "required";
        }
        //source is FieldValidatorSource
        return source.createValidator(contactEmail, validationString, null);

    } 

<t:TextField t:id="contactEmail" t:validate="prop:fieldValidator"

<t:TextField t:id="contactName" t:validate="prop:fieldValidator"

--
View this message in context: http://tapestry.1045711.n5.nabble.com/dynamic-validation-for-required-not-required-tp4422440p5710274.html
Sent from the Tapestry - User mailing list archive at Nabble.com.

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


Re: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Wed, 25 May 2011 00:41:48 -0300, <ja...@w9z.org> wrote:

> Thanks Josh and Thiago!

:D

> Here is what I ended up with in case it helps anyone else:
>
>     public FieldValidator getFieldValidator() {
>         String validationString = "none";
>         if(attributeDefinition.isRequired()) {
>             validationString = "required";
>         }
>         Field f = null;
>         //isUseTextField just tests to see if the attribute is defined  
> to be
>         // over 50 chars or not
>         if(isUseTextField()) {
>             f =  
> (Field)componentResources.getEmbeddedComponent("attributetextfield");
>         } else {
>             f =  
> (Field)componentResources.getEmbeddedComponent("attributetextarea");
>         }
>         //source is FieldValidatorSource
>         return source.createValidator(f, validationString, null);
>
>     }

Have you tried using @InjectComponent instead of getEmbeddedComponent()?  
Your solution is correct, but using @InjectComponent would be a little  
easier and more elegant.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by ja...@w9z.org.
Thanks Josh and Thiago!  Here is what I ended up with in case it helps anyone else:

    public FieldValidator getFieldValidator() {
        String validationString = "none";
        if(attributeDefinition.isRequired()) {
            validationString = "required";
        }
        Field f = null;
        //isUseTextField just tests to see if the attribute is defined to be
        // over 50 chars or not
        if(isUseTextField()) {
            f = (Field)componentResources.getEmbeddedComponent("attributetextfield"); 
        } else {
            f = (Field)componentResources.getEmbeddedComponent("attributetextarea");
        }
        //source is FieldValidatorSource
        return source.createValidator(f, validationString, null);

    }

Thanks again!

-----Original Message-----
From: "Thiago H. de Paula Figueiredo" <th...@gmail.com>
Sent: Tuesday, May 24, 2011 9:52pm
To: "Tapestry users" <us...@tapestry.apache.org>
Subject: Re: dynamic validation for required / not required

On Tue, 24 May 2011 19:13:31 -0300, <ja...@w9z.org> wrote:

>> How do you generate your fields?
>
> Each event has an ordered list of attribute definitions.  When someone  
> registers for the event, the page loops through the list of attributes.  
> If the attribute definition says it is to be a short field, it renders  
> as a textField. If it is long, it renders as a text area. Multiple  
> people can be registered at the same time, so this loop is in another  
> loop that executes once for each attendee.

In this case, you have exactly one TextArea instance and one TextField  
instance, regardless of how many times you render them inside a loop. Give  
each one a t:id and use @InjectComponent to get them. All  
Tapestry-provided form field components are Field implementations, so you  
can use them in FormValidatorSource methods.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
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: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 24 May 2011 19:13:31 -0300, <ja...@w9z.org> wrote:

>> How do you generate your fields?
>
> Each event has an ordered list of attribute definitions.  When someone  
> registers for the event, the page loops through the list of attributes.  
> If the attribute definition says it is to be a short field, it renders  
> as a textField. If it is long, it renders as a text area. Multiple  
> people can be registered at the same time, so this loop is in another  
> loop that executes once for each attendee.

In this case, you have exactly one TextArea instance and one TextField  
instance, regardless of how many times you render them inside a loop. Give  
each one a t:id and use @InjectComponent to get them. All  
Tapestry-provided form field components are Field implementations, so you  
can use them in FormValidatorSource methods.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by ja...@w9z.org.
> How do you generate your fields?

The app manages attendees for training events.  When the event is defined, the administrator can specify additional attributes that need to be collected when someone registers.  So one event might need to keep track of attendee's first and last names and another might need to keep track of their name, company, & school.

Each event has an ordered list of attribute definitions.  When someone registers for the event, the page loops through the list of attributes. If the attribute definition says it is to be a short field, it renders as a textField. If it is long, it renders as a text area. Multiple people can be registered at the same time, so this loop is in another loop that executes once for each attendee.

> See the Component getEmbeddedComponent(String embeddedId)  
method in ComponentResources.

This looks like along the lines of what I'm looking for, but I need a field.  How do I convert the right component to a field so I can pass it to source.createValidator? (Sorry if that is a silly question.)

Also if there is a better approach, please let me know.  I'm not married to this approach.

Mark





-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

---------------------------------------------------------------------
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: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 24 May 2011 17:18:27 -0300, <ja...@w9z.org> wrote:

> OK I understand how to do that and it works as long as I know the name  
> of yourField so I can use:
>
>     @InjectComponent
>     private Field yourField;

Not necessarily. See the Component getEmbeddedComponent(String embeddedId)  
method in ComponentResources.

> In order to have yourField to create the Validator.  This would require  
> knowing the name and number of fields at compile time.  In my code, the  
> fields are generated from user specified attributes so I don't know what  
> they are called until the app is running.

How do you generate your fields?

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by ja...@w9z.org.
OK I understand how to do that and it works as long as I know the name of yourField so I can use:

    @InjectComponent
    private Field yourField;

In order to have yourField to create the Validator.  This would require knowing the name and number of fields at compile time.  In my code, the fields are generated from user specified attributes so I don't know what they are called until the app is running.

Maybe a better question is this:

Is there a way to get a Field at run time so I can pass it to the createValidator method?  Or, is there a way to create a Validator that figures out what field it is attached to based on where it is used in the template instead of needing to be told when it is created?

Thank you for  your help.

Jack




-----Original Message-----
From: "Thiago H. de Paula Figueiredo" <th...@gmail.com>
Sent: Tuesday, May 24, 2011 4:09pm
To: "Tapestry users" <us...@tapestry.apache.org>, jackkurosaki@w9z.org
Subject: Re: dynamic validation for required / not required

On Tue, 24 May 2011 16:50:46 -0300, <ja...@w9z.org> wrote:

> Is there a way to tell Tapestry to use:
> validate="${validationText}"
> so if getValidationText returns the string "required" it will act the  
> same way as if you wrote: validate="required"?

As already stated, you need to pass a FieldValidator instance to the  
validate parameter. FieldValidatorSource is the easiest way.

<... validate="prop:customValidation">

public FieldValidator getCustomValidation() {
	...
}

Tapestry doesn't have templates with dynamic structure, so you'll be able  
to get the Field instance to pass to the FieldValidatorSource methods.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br



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


Re: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 24 May 2011 16:50:46 -0300, <ja...@w9z.org> wrote:

> Is there a way to tell Tapestry to use:
> validate="${validationText}"
> so if getValidationText returns the string "required" it will act the  
> same way as if you wrote: validate="required"?

As already stated, you need to pass a FieldValidator instance to the  
validate parameter. FieldValidatorSource is the easiest way.

<... validate="prop:customValidation">

public FieldValidator getCustomValidation() {
	...
}

Tapestry doesn't have templates with dynamic structure, so you'll be able  
to get the Field instance to pass to the FieldValidatorSource methods.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by ja...@w9z.org.
Is there a way to tell Tapestry to use:
validate="${validationText}"

so if getValidationText returns the string "required" it will act the same way as if you wrote: validate="required"?



-----Original Message-----
From: jackkurosaki@w9z.org
Sent: Tuesday, May 24, 2011 2:47pm
To: "Tapestry users" <us...@tapestry.apache.org>
Subject: Re: dynamic validation for required / not required

Thats helpful, but my problem is that I don't know the field id either, so I can't inject the field.  The fields are determined at run time (attributes that the user can add or remove) so it has to be flexible.

I supposed I could put every TextField in an IF statement where the IF has "required" and the ELSE doesn't, but that seems like I'm missing a better way to do it.

Jack

-----Original Message-----
From: "Thiago H. de Paula Figueiredo" <th...@gmail.com>

@Inject FieldValidatorSource and use its FieldValidator  
createValidators(Field field, String specification) method to generate a  
FieldValidator. Then create a FieldValidator getValidator() method using  
createValidators() and add t:validate="prop:validator" to your TextField.

Example (taken from  
http://tapestry.1045711.n5.nabble.com/Validation-based-on-condition-td2435565.html):

@Inject
private FieldValidatorSource source;

@InjectComponent
private Field yourField;

public FieldValidator getDynamicConstraints() {
	return source.createValidators(yourField, "required", null);
}



---------------------------------------------------------------------
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: dynamic validation for required / not required

Posted by ja...@w9z.org.
Thats helpful, but my problem is that I don't know the field id either, so I can't inject the field.  The fields are determined at run time (attributes that the user can add or remove) so it has to be flexible.

I supposed I could put every TextField in an IF statement where the IF has "required" and the ELSE doesn't, but that seems like I'm missing a better way to do it.

Jack

-----Original Message-----
From: "Thiago H. de Paula Figueiredo" <th...@gmail.com>

@Inject FieldValidatorSource and use its FieldValidator  
createValidators(Field field, String specification) method to generate a  
FieldValidator. Then create a FieldValidator getValidator() method using  
createValidators() and add t:validate="prop:validator" to your TextField.

Example (taken from  
http://tapestry.1045711.n5.nabble.com/Validation-based-on-condition-td2435565.html):

@Inject
private FieldValidatorSource source;

@InjectComponent
private Field yourField;

public FieldValidator getDynamicConstraints() {
	return source.createValidators(yourField, "required", null);
}



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


Re: dynamic validation for required / not required

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
On Tue, 24 May 2011 13:33:48 -0300, <ja...@w9z.org> wrote:

> Hm... I've used this before when I ALWAYS want a field to be required,  
> but I didn't see a way to make a field required or not required based on  
> conditions that are only known when the page is rendered.


@Inject FieldValidatorSource and use its FieldValidator  
createValidators(Field field, String specification) method to generate a  
FieldValidator. Then create a FieldValidator getValidator() method using  
createValidators() and add t:validate="prop:validator" to your TextField.

Example (taken from  
http://tapestry.1045711.n5.nabble.com/Validation-based-on-condition-td2435565.html):

@Inject
private FieldValidatorSource source;

@InjectComponent
private Field yourField;

public FieldValidator getDynamicConstraints() {
	return source.createValidators(yourField, "required", null);
}

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, Ars Machina Tecnologia da Informação Ltda.
http://www.arsmachina.com.br

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


Re: dynamic validation for required / not required

Posted by ja...@w9z.org.
Hm... I've used this before when I ALWAYS want a field to be required, but I didn't see a way to make a field required or not required based on conditions that are only known when the page is rendered.

-----Original Message-----
From: "Nikola Milikic" <ni...@gmail.com>
Sent: Tuesday, May 24, 2011 11:44am
To: "Tapestry users" <us...@tapestry.apache.org>
Subject: Re: dynamic validation for required / not required

Hi Jack,

Maybe Bean Validation can help you here

http://tapestry.apache.org/bean-validation.html
 <http://tapestry.apache.org/bean-validation.html>
Best,
Nikola


On Tue, May 24, 2011 at 5:30 PM, <ja...@w9z.org> wrote:

> I know I can make a field required like this:
> <t:textfield value="myTextField" validate="required"/>
>
> But how can I make it programmatically determine whether or not the field
> is required?
>
> I have tried:
>
> <t:textfield value="myTextField" validate="${validationText}"/>
>
> public String getValidationText() {
>   if (fieldRequired) { return "required" }
>   else { return ""}
>
>
> But that doesn't seem to work.  Tapestry says it can't coerce a string into
> a Validator.
>
> Am I overlooking something simple or is there a better approach to
> accomplish this?
>
> Jack
>
>
> ---------------------------------------------------------------------
> 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: dynamic validation for required / not required

Posted by Nikola Milikic <ni...@gmail.com>.
Hi Jack,

Maybe Bean Validation can help you here

http://tapestry.apache.org/bean-validation.html
 <http://tapestry.apache.org/bean-validation.html>
Best,
Nikola


On Tue, May 24, 2011 at 5:30 PM, <ja...@w9z.org> wrote:

> I know I can make a field required like this:
> <t:textfield value="myTextField" validate="required"/>
>
> But how can I make it programmatically determine whether or not the field
> is required?
>
> I have tried:
>
> <t:textfield value="myTextField" validate="${validationText}"/>
>
> public String getValidationText() {
>   if (fieldRequired) { return "required" }
>   else { return ""}
>
>
> But that doesn't seem to work.  Tapestry says it can't coerce a string into
> a Validator.
>
> Am I overlooking something simple or is there a better approach to
> accomplish this?
>
> Jack
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tapestry.apache.org
> For additional commands, e-mail: users-help@tapestry.apache.org
>
>