Hi,
        Let me tell the scenario what I want...
When I select any file other than gif, it should populate the client side
message something like what you have given "unsupported-file-type={0} only
supports files of type {1}." And donnt want any server side validation on
file type..
So as per your below code, I have done the following coding,
---------------------------------------------------------------------------
In my .page file, I have done following changes:
<component id="uploadForm" type="Form">
        <binding name="listener" value="listener:onSubmit"/>
        <binding name="clientValidationEnabled" value="true"/>
</component>

<component id="upload1" type="Upload">
        <binding name="file" value="file1"/>
       <binding name="validators" value="validators:accept=jpeg-jpg-png"/>
       <binding name="displayName" value="literal:Choose file"/> 
</component>
----------------------------------------------------------------------------
I have created a Accept.java file, and the code in that is:

package xxx.yyy.util;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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;

/**
 * Util class for saving and loading image files and getting unique file
name
 * @author ananyag
 *
 */
        
public class Accept extends BaseValidator
{
        protected final Log logger = LogFactory.getLog(getClass());
        
        
    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
    {
        logger.debug("start of Accept Validate method class");
        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)
    {
        logger.debug("start of Accept - buildMessage method");
        return messages.formatValidationMessage(
                getMessage(),
                "unsupported-file-type",
                new Object[]
                { field.getDisplayName(), accept });
    }   

    public void renderContribution(IMarkupWriter writer, IRequestCycle 
                cycle, FormComponentContributorContext context,
IFormComponent field)
    {
        logger.debug("start of Accept - renderContribution method");
 
context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExVal
idator.js");
        logger.debug("after calling js of Accept - renderContribution
method");
        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.addSubmitListener(buffer.toString());
        logger.debug("end of Accept - renderContribution method");
    }   

        

}
-----------------------------------------------------------------------
I have edited my hivemodule.xml like this:
<module>
   <contribution configuration-id="tapestry.form.validator.Validators">
          <validator name="accept" class=" xxx.yyy.util.Accept"/>
    </contribution> 
</module>
-------------------------------------------------------------------------
I Have created a new file, ValidationStrings.properties and I have placed
this file under WEB-INF(in the same level where my other .html and .page
files are) and the content is:

field-is-required=You must enter a value for {0}.
field-too-short=You must enter at least {0} characters for {1}.

invalid-date-format=Invalid date format for {0}.  Format is {1}.
invalid-int-format={0} must be an integer value.
invalid-format={0} is not in a recognized format.
invalid-numeric-format={0} must be a numeric value.

date-too-early={0} must be on or after {1}.
date-too-late={0} must be on or before {1}.

number-too-small={0} must not be smaller than {1}.
number-too-large={0} must not be larger than {1}.

number-range={0} must be between {1} and {2}.

invalid-email-format=Invalid email format for {0}.  Format is [EMAIL PROTECTED]

pattern-not-matched={0} does not fulfill the required pattern {1}.

invalid-url-format = Invalid URL.
disallowed-protocol = Disallowed protocol - protocol must be {0}.

### 4.0 ###

select-field-is-required=You must select a value for {0}.
file-field-is-required=You must select a file to upload.

field-too-long=You must enter no more than {0} characters for {1}.

date-too-small={0} may not be earlier than {1,date}.
date-too-large={0} may not be later than {1,date}.

regex-mismatch={0} is invalid.

invalid-field-equality={0} must be {1,choice,0#different from|1#the same as}
{2}.

unsupported-file-type={0} only supports files of type {1}.
---------------------------------------------------------------------------
with all these codes, its not working.... The page is not breaking, but no
effect also....
Can you point out where I went wrong, and What else I need to do to make it
work...
I am using, tapestry4Beta4.o version...

Please help, since I has become my work stopper...
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]

Reply via email to