You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Nicolas Bouillon <ni...@bouil.org> on 2009/12/10 13:21:13 UTC

Date Validator

Hi,

I'm trying to add a validator for the date field. I've created the 
validator "infuture" to verify the picked date is in the future

<t:datefield t:id="endTime" value="item.endTime" 
t:validate="required,infuture"/>

In my app module, i've added

     /**
      * Contributes the set of validators:
      */
     public static void contributeFieldValidatorSource(
             MappedConfiguration<String, Validator> configuration) {

         configuration.add("infuture", new DateValidator());
     }

And my DateValidator is

import java.util.Date;

import org.apache.tapestry5.Field;
import org.apache.tapestry5.MarkupWriter;
import org.apache.tapestry5.ValidationException;
import org.apache.tapestry5.ioc.MessageFormatter;
import org.apache.tapestry5.services.FormSupport;
import org.apache.tapestry5.validator.AbstractValidator;

public class DateValidator extends AbstractValidator<Void, Date> {

     public DateValidator() {
         super(null, Date.class, "date-must-be-in-future");
     }

     public void render(Field field, Void constraintValue,
             MessageFormatter formatter, MarkupWriter writer,
             FormSupport formSupport) {
         // TODO Auto-generated method stub

     }

     public void validate(Field field, Void constraintValue,
             MessageFormatter formatter, Date value) throws 
ValidationException {
         if (value.before(new Date())) {
             throw new ValidationException(buildMessage(formatter, field));
         }
     }

     private String buildMessage(MessageFormatter formatter, Field field) {
         return formatter.format(field.getLabel());
     }
}


It's a simple copy-paste adapt of other validator such as 
org.apache.tapestry5.validator.Email

That works well.

My problem I can't set the default message in case of validation error. 
I've always a [[missing key: date-must-be-in-future]], even if i put the 
key in my WEB-INF/app.properties.

Looking a source code, it seams the source of the message are in 
tapestry-code/org/apache/tapestry5/internal/ValidationMessages.properties.

I can only add my message on a per-page-per-field basis ? Any way to 
configure another global source of message, to put mine ?

Thanks for help.
Nicolas.





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


Re: Date Validator

Posted by Nicolas Bouillon <ni...@bouil.org>.
Thiago H. de Paula Figueiredo a écrit :
  > Validation errors do not come from app.properties. They need to be put
> in some other properties file in the classpath and then you must inform 
> this to Tapestry through a contribution to the ValidationMessagesSource
> 
> 
> public void 
> contributeValidationMessagesSource(OrderedConfiguration<String> 
> configuration) {
>     configuration.add("somename", "path/to/your/file/in/classpath/"); // 
> do not include the .properties suffix.
> }

That looks nice ! I will give it a try :-)

Thank you.
Nicolas.




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


Re: Date Validator

Posted by "Thiago H. de Paula Figueiredo" <th...@gmail.com>.
Em Thu, 10 Dec 2009 10:21:13 -0200, Nicolas Bouillon <ni...@bouil.org>  
escreveu:

> Hi,

Hi!

> My problem I can't set the default message in case of validation error.  
> I've always a [[missing key: date-must-be-in-future]], even if i put the  
> key in my WEB-INF/app.properties.

Validation errors do not come from app.properties. They need to be put in  
some other properties file in the classpath and then you must inform this  
to Tapestry through a contribution to the ValidationMessagesSource


public void  
contributeValidationMessagesSource(OrderedConfiguration<String>  
configuration) {
	configuration.add("somename", "path/to/your/file/in/classpath/"); // do  
not include the .properties suffix.
}


> I can only add my message on a per-page-per-field basis ?

I guess not.

-- 
Thiago H. de Paula Figueiredo
Independent Java, Apache Tapestry 5 and Hibernate consultant, developer,  
and instructor
Owner, software architect and developer, 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