Validate annotation on field

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Validate annotation on field

Nathan Quirynen
Hi,

I want to use the @Validate annotation, but I cannot put it at the value
properties, because these properties are part of a DTO object that is
part of a library.
In the documentation
(http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beaneditor/Validate.html)
I found the following: " May be placed on any getter or setter method,
or on the matching field".

So I thought I could do the following:

@Validate("regexp="+SOME_REGEXP)
@InjectComponent
private Field someField;

But this does not seem to work?

Is there any other way to add validators to a field programatically when
you have no access to the property declarations?
I could do all validation myself in the onValidate event, but this does
not seem optimal.

Nathan


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Validate annotation on field

Nathan Quirynen
Hey,

I tried solving it the following way using the FieldValidatorSource service:

<t:textfield t:id="someField" t:value="someValue"
t:validate="prop:someFieldValidate" />

public FieldValidator<Object> getSomeFieldValidate() {

         StringBuilder validate = new StringBuilder()
                 .append("required")
                 .append(",")
                 .append("regexp="+SOME_REGEXP);

         return fieldValidatorSource.createValidators(someField,
validate.toString());
}


But now I have a problem when SOME_REGEXP contains a space character it
throws following error:


      java.lang.RuntimeException

*Unexpected character ']' at position 26 of input string:
required,regexp=^[A-ΓΏ'\- ]*$*

org.apache.tapestry5.internal.services.FieldValidatorSourceImpl
parseError() FieldValidatorSourceImpl.java 422
org.apache.tapestry5.internal.services.FieldValidatorSourceImpl parse()
        FieldValidatorSourceImpl.java 381
org.apache.tapestry5.internal.services.FieldValidatorSourceImpl
toValidatorSpecifications() FieldValidatorSourceImpl.java 199
org.apache.tapestry5.internal.services.FieldValidatorSourceImpl
createValidators() FieldValidatorSourceImpl.java 182

...


I used this same regexp value in the @Validate annotation before without
problems, but with my method above it gives the above parsing error.

Anyone know how I can solve the above problem?

Nathan

On 15/09/16 16:20, Nathan Quirynen wrote:

> Hi,
>
> I want to use the @Validate annotation, but I cannot put it at the
> value properties, because these properties are part of a DTO object
> that is part of a library.
> In the documentation
> (http://tapestry.apache.org/current/apidocs/org/apache/tapestry5/beaneditor/Validate.html)
> I found the following: " May be placed on any getter or setter method,
> or on the matching field".
>
> So I thought I could do the following:
>
> @Validate("regexp="+SOME_REGEXP)
> @InjectComponent
> private Field someField;
>
> But this does not seem to work?
>
> Is there any other way to add validators to a field programatically
> when you have no access to the property declarations?
> I could do all validation myself in the onValidate event, but this
> does not seem optimal.
>
> Nathan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>

Reply | Threaded
Open this post in threaded view
|

Re: Validate annotation on field

Thiago H. de Paula Figueiredo
In reply to this post by Nathan Quirynen
On Thu, 15 Sep 2016 11:20:57 -0300, Nathan Quirynen  
<[hidden email]> wrote:

> Hi,

Hi!

> @Validate("regexp="+SOME_REGEXP)
> @InjectComponent
> private Field someField;
> But this does not seem to work?

This isn't supposed to work. You should annotate the field or getter whose  
value is being validated, not the form field component. What about adding  
validate="regexp:[\\d]{1-2}", for example, to the <t:textfield> directly?

> Is there any other way to add validators to a field programatically when  
> you have no access to the property declarations?
> I could do all validation myself in the onValidate event, but this does  
> not seem optimal.

See above.

--
Thiago H. de Paula Figueiredo
Tapestry, Java and Hibernate consultant and developer
http://machina.com.br

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Validate annotation on field

Barry Books-2
In reply to this post by Nathan Quirynen
I think you could do this with JSR303 validators if you detect it's a field
and pass the value of the field to the validator.

See: https://github.com/trsvax/tapestry-nocode/blob/master/04.md for a way
to automatically validate in the onValidate event

On Thursday, September 15, 2016, Nathan Quirynen <
[hidden email]> wrote:

> Hi,
>
> I want to use the @Validate annotation, but I cannot put it at the value
> properties, because these properties are part of a DTO object that is part
> of a library.
> In the documentation (http://tapestry.apache.org/cu
> rrent/apidocs/org/apache/tapestry5/beaneditor/Validate.html) I found the
> following: " May be placed on any getter or setter method, or on the
> matching field".
>
> So I thought I could do the following:
>
> @Validate("regexp="+SOME_REGEXP)
> @InjectComponent
> private Field someField;
>
> But this does not seem to work?
>
> Is there any other way to add validators to a field programatically when
> you have no access to the property declarations?
> I could do all validation myself in the onValidate event, but this does
> not seem optimal.
>
> Nathan
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
>
Reply | Threaded
Open this post in threaded view
|

Re: Validate annotation on field

Nathan Quirynen
In reply to this post by Thiago H. de Paula Figueiredo
Hey thanks for your reply.

The problem is that the regular expression is defined in a library as a
constant and reused at a lot of places, so I want to use this constant
instead of putting it everywhere myself, that's why I'm searching for
another solution where I can use this constant and also without using
the @Validate annotation as I have no access to the field declarations
(also in a library).


On 16/09/16 14:14, Thiago H de Paula Figueiredo wrote:

> On Thu, 15 Sep 2016 11:20:57 -0300, Nathan Quirynen
> <[hidden email]> wrote:
>
>> Hi,
>
> Hi!
>
>> @Validate("regexp="+SOME_REGEXP)
>> @InjectComponent
>> private Field someField;
>> But this does not seem to work?
>
> This isn't supposed to work. You should annotate the field or getter
> whose value is being validated, not the form field component. What
> about adding validate="regexp:[\\d]{1-2}", for example, to the
> <t:textfield> directly?
>
>> Is there any other way to add validators to a field programatically
>> when you have no access to the property declarations?
>> I could do all validation myself in the onValidate event, but this
>> does not seem optimal.
>
> See above.
>


---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]