You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Anjali Abraham <an...@aztec.soft.net> on 2005/10/06 12:06:23 UTC
Need help
Hi All,
I have a scenario wherein in my html, I have a code something
like this:
<input type="file" jwcid="upload1" name="upload1"/>
I have a requirement of giving a filter to only .gif files can
be shown by default to the dialog box which get opened on click of "browse"
button of the type file. For that to do I need to modify my above html code
to something like this:
<input type="file" jwcid="upload1" name="upload1" accept="image/gif"/>
But here there is a problem, as in my input tag I am using
jwcid, I ignores all the attributes particular to html's.
That's the reason we need to enable javascripting for these
tags. How to do the same.......???????
How can I achieve the same "accept" attribute functionality
through Tapestry4...????????
Please respond with reply,
Thanks in advance,
Regards,
Anjali
Re: Need help
Posted by Andreas Andreou <an...@di.uoa.gr>.
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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
Re: Need help
Posted by Andreas Andreou <an...@di.uoa.gr>.
The @Upload component allows informal parameters, but
are you sure that the accept attribute is valid html and recognized by
browsers.
In general, it's difficult to change the <input type="file"> control's
appearance
and (clientside) behavior due to security reasons. Maybe it's better to
create your
own Upload component and do the filtering server-side.
Also, take a look at:
http://www.irt.org/script/561.htm
http://www.irt.org/script/1154.htm
If the method in the first link works, you have to write some javascript
which will check the filename and which will get triggered on form submit.
A generic solution to implement this (in Tap4) would be to write an
AcceptValidator
, contributing the correct javascript to the onSubmit handler of your
form and bind
it to the @Upload component, i.e.
<component id="file" type="Upload">
<binding name="file" value="file"/>
<binding name="displayName" value="message:filename"/>
<binding name="validators" value="validators:accept=gif-jpeg-other"/>
</component>
Since I'm interested in this, i'll also look into it and report my findings.
Anjali Abraham wrote:
>Hi All,
> I have a scenario wherein in my html, I have a code something
>like this:
><input type="file" jwcid="upload1" name="upload1"/>
>
> I have a requirement of giving a filter to only .gif files can
>be shown by default to the dialog box which get opened on click of "browse"
>button of the type file. For that to do I need to modify my above html code
>to something like this:
><input type="file" jwcid="upload1" name="upload1" accept="image/gif"/>
> But here there is a problem, as in my input tag I am using
>jwcid, I ignores all the attributes particular to html's.
>
> That's the reason we need to enable javascripting for these
>tags. How to do the same.......???????
>
> How can I achieve the same "accept" attribute functionality
>through Tapestry4...????????
>
>Please respond with reply,
>Thanks in advance,
>
>Regards,
>Anjali
>
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org