Hi Andreou,
        Well 1st of all thanks for the response to the mail.
        I have created the corresponding files with the below mentioned
code... And also u have mentioned below to edit ValidationString.properties
this file and add an entry "unsupported-file-type={0} only supports files of
type {1}", as u have done.
But my question is - is that I create my own ValidationString.properties
file instead of adding an entry in the existing file which is there in the
tapestry jar file. And if I have created my own file, where do I am supposed
to place that file....
Also, I am using Tapestry4Beta4 version, is this feature enabled in
that...???

Please respond with solution,
Thanks in advance,

Regards,
Anjali

-----Original Message-----
From: Andreas Andreou [mailto:[EMAIL PROTECTED] 
Sent: Thursday, October 06, 2005 7:30 PM
To: Tapestry users
Subject: Re: Need help

Ok, here's the validator I mentioned before.
I'll ask if it can be included in directly into Tapestry.
For the moment, you have to add edit your hivemodule.xml, and add this 
contribution:

  <contribution configuration-id="tapestry.form.validator.Validators">
    <validator name="accept" class="ebyz.web.validator.Accept"/>
  </contribution>   

Then you have to add an unsupported-file-type entry in 
ValidationString.properties. Here's mine:
unsupported-file-type={0} only supports files of type {1}.

Then you can define your Upload component like this:

    <component id="file" type="Upload">
        <binding name="file" value="file"/>
        <binding name="validators" value="validators:accept=jpeg-jpg-png"/>
        <binding name="displayName" value="literal:Choose file"/>        
       
    </component>

And here is the java source. Note that it does both client side and 
server side validation...

package ebyz.web.validator;

import org.apache.tapestry.IMarkupWriter;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.TapestryUtils;
import org.apache.tapestry.form.FormComponentContributorContext;
import org.apache.tapestry.form.IFormComponent;
import org.apache.tapestry.form.ValidationMessages;
import org.apache.tapestry.form.validator.BaseValidator;
import org.apache.tapestry.multipart.UploadPart;
import org.apache.tapestry.util.RegexpMatcher;
import org.apache.tapestry.valid.ValidationConstraint;
import org.apache.tapestry.valid.ValidatorException;

public class Accept extends BaseValidator
{
    private String accept;
   
    public Accept()
    {       
    }
   
    public Accept(String initializer)
    {
        super(initializer);
    }
   
    public void setAccept(String acc)
    {
        accept = acc;
    }

    public void validate(IFormComponent field, ValidationMessages messages,
            Object object) throws ValidatorException
    {
        UploadPart part = (UploadPart) object;
        String name = part.getFileName();
        String ext = name.toLowerCase();
        int pos = ext.lastIndexOf(".");
        if (pos >=0)
        {
            ext = ext.substring(pos+1);
        }
       
        if (accept.indexOf(ext) >= 0)
        {
            return;
        }       
        throw new ValidatorException(buildMessage(messages, field),
                ValidationConstraint.PATTERN_MISMATCH);       
    }
   
    private String buildMessage(ValidationMessages messages, 
IFormComponent field)
    {
        return messages.formatValidationMessage(
                getMessage(),
                "unsupported-file-type",
                new Object[]
                { field.getDisplayName(), accept });
    }   
   
    public void renderContribution(IMarkupWriter writer, IRequestCycle 
cycle,
            FormComponentContributorContext context, IFormComponent field)
    {
        
context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExVal
idator.js");

        String toReg = accept.replace('-','|');
        toReg = ".*\\.(" + toReg + ")$";
        String pattern = new RegexpMatcher().getEscapedPatternString(toReg);
        String message = buildMessage(context, field);

        StringBuffer buffer = new StringBuffer("function(event) { 
Tapestry.validate_regex(event, '");
        buffer.append(field.getClientId());
        buffer.append("', '");
        buffer.append(pattern);
        buffer.append("', ");
        buffer.append(TapestryUtils.enquote(message));
        buffer.append("); }");

        context.addSubmitHandler(buffer.toString());
    }   

}


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

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

Reply via email to