I've been able to create a Validator, but I am having problems getting the
Error Message to show up in my new Validator.

1.) First I copied one of the default T5 validators (I used the Required
class) -- here it is:

----------------------------------------------------------------------------------------------------------------------------------------------------
import static org.apache.tapestry.TapestryUtils.quote;

import org.apache.tapestry.Field;

import org.apache.tapestry.MarkupWriter;
import org.apache.tapestry.PageRenderSupport;

import org.apache.tapestry.ValidationException;
import org.apache.tapestry.Validator;
import org.apache.tapestry.ioc.MessageFormatter;

/**
 * @author Doug.Schroeder
 */
public class MessageFieldValidator implements Validator<Void, Object> {

        public Class<Void> getConstraintType() {
                return null;
        }

        public String getMessageKey() {
                return "messageFieldValidator";
        }

        public Class getValueType() {
                return Object.class;
        }

        public boolean invokeIfBlank() {
                return true;
        }

        public void render(Field field, Void constraintValue, MessageFormatter
formatter, MarkupWriter writer, PageRenderSupport pageRenderSupport) {
                pageRenderSupport.addScript(
                "MyDomain.Field.messageFieldValidator('%s', %s);",
                field.getClientId(),
                quote(buildMessage(formatter, field)));
        }

        public void validate(Field field, Void constraintValue, MessageFormatter
formatter, Object value)
    throws ValidationException
        {
                if (value == "some value I am checking for")
                        throw new ValidationException(buildMessage(formatter, 
field));
        }
        
         private String buildMessage(MessageFormatter formatter, Field field)
            {
                return formatter.format(field.getLabel());
            }
}


----------------------------------------------------------------------------------------------------------------------------------------------------

2.) Next I added the following to my Module class:


----------------------------------------------------------------------------------------------------------------------------------------------------

public static void contributeFieldValidatorSource(
     MappedConfiguration<String, Validator> configuration) {
        configuration.add("messageFieldValidator", new
MessageFieldValidator());
}


----------------------------------------------------------------------------------------------------------------------------------------------------

3.) Next, in my component class, I turned on client-side validation:


----------------------------------------------------------------------------------------------------------------------------------------------------

<t:form t:id="myForm" clientValidation="true">
 <label for="messageSubject"
class="editor-label">${message:message-subject.title}</label>
 <t:textField t:id="messageSubject" class="editor-input"
t:validate="messageFieldValidator" />
 <t:submit class="action-button submit-button" t:id="submit"
value="message:message-submit-button.title" />
</t:form>


----------------------------------------------------------------------------------------------------------------------------------------------------

4.) Finally, I added a piece of javascript that would render the client-side
validation:


----------------------------------------------------------------------------------------------------------------------------------------------------


if(typeof MyDomain == "undefined") { var MyDomain={}; };

MyDomain.Field = {

        messageFieldValidator : function(field, message) {
                Tapestry.addValidator(field, true, function(value, event) {
                                if (value == '')
                                        event.recordError(message);
                });
        }
};


----------------------------------------------------------------------------------------------------------------------------------------------------

I am having problems getting the validation message to show up -- this is
what I see instead of a message:

[[missing key: messageFieldValidator]]

How do I link a message up with a validator?  Also, is it possible to
override the default error message that shows at the top of the page (which
says "You must correct the following errors before you may continue.")?


jglanz wrote:
> 
> Hey guys, I've been using T5 for a while and haven't needed to create my
> own validator until now and I am absolutely dumb founded for how to do
> it....any insight would be great.
> 
> many thx, Jon
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/T5-Validator-tf4282766.html#a12193987
Sent from the Tapestry - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to