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/RegExValidator.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]

Reply via email to