You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@wicket.apache.org by Ned Collyer <ne...@gmail.com> on 2008/06/05 06:43:04 UTC

Best way of validating FileUploadField

Hi,

I have the requirement where I need to validate things like image format,
height, width etc for a file upload.

I'd like to do this using validators, which I can do - but im curious if its
the right way.


Here is what I currently have. 



form.add(fileUploadField = new FileUploadField("fileInput") {
    protected void convertInput() {
        setConvertedInput(getFileUpload());
    }
});

fileUploadField.add(new AbstractValidator() {
    protected void onValidate(IValidatable validatable) {
        FileUpload fileUpload = (FileUpload)validatable.getValue();
        if (fileUpload.getContentType().equals("text/plain")) {
            error(validatable);
        }
    }

    protected String resourceKey() {
        return "CustomFileError";
    }
});


This works... but is it correct?

I'm not handling it in the onSubmit - and don't believe I should have to -
despite the examples.


I have double submit implemented on my base form in a similar fashion to
what igor recommends here
http://www.nabble.com/double-form-submission-handling---tp13816084p13850262.html
- which means I only execute the "submit" if all the validation passes. 
This works SUPER AWESOME thus far :)
-- 
View this message in context: http://www.nabble.com/Best-way-of-validating-FileUploadField-tp17662018p17662018.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Best way of validating FileUploadField

Posted by Ned Collyer <ne...@gmail.com>.

Michael Sparer wrote:
> 
> Might be a bit off-topic, but I just wanted to add that if you're
> validating if the file has the correct content-type you shouldn't rely on
> the getContentType method, as it determines the type just using the
> extension. I did rely on that and to prevent users from uploading annoying
> animated gifs, allowed only jpg and png. Only a few days in production
> someone uploaded an animated gif by just changing the extension .gif to
> .jpg. To check the real type you should use an external tool such as e.g.
> JMimeMagic.
> Just have a look at the proposed approaches on
> http://www.rgagnon.com/javadetails/java-0487.html
> 
> regards,
> Michael
> 

Thanks for the info.

The example I gave was simple and for demo purpose only :) - I'm going to do
a lot of different things  (like checking color depth, bytesize, dimensions
etc).
-- 
View this message in context: http://www.nabble.com/Best-way-of-validating-FileUploadField-tp17662018p17666608.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Best way of validating FileUploadField

Posted by Michael Sparer <mi...@gmx.at>.
Might be a bit off-topic, but I just wanted to add that if you're validating
if the file has the correct content-type you shouldn't rely on the
getContentType method, as it determines the type just using the extension. I
did rely on that and to prevent users from uploading annoying animated gifs,
allowed only jpg and png. Only a few days in production someone uploaded an
animated gif by just changing the extension .gif to .jpg. To check the real
type you should use an external tool such as e.g. JMimeMagic.
Just have a look at the proposed approaches on
http://www.rgagnon.com/javadetails/java-0487.html

regards,
Michael

Ned Collyer wrote:
> 
> Thanks,
> 
> Raised as https://issues.apache.org/jira/browse/WICKET-1684
> 
> Curiously mentioned in https://issues.apache.org/jira/browse/WICKET-300
> but marked as resolved/invalid.
> 
> Rgds
> 
> Ned
> 
> 
> igor.vaynberg wrote:
>> 
>> argh, didnt read closely enough. yes that is the right way to do it,
>> in fact you shouldnt have to do that at all, fileuploadfield should be
>> doing that...
>> 
>> please create a jira ticket, that component is pretty old so it
>> probably got overlooked. multifileuploadfield does it properly
>> already.
>> 
>> -igor
>> 
> 
> 


-----
Michael Sparer
http://talk-on-tech.blogspot.com
-- 
View this message in context: http://www.nabble.com/Best-way-of-validating-FileUploadField-tp17662018p17664375.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Best way of validating FileUploadField

Posted by Ned Collyer <ne...@gmail.com>.
Thanks,

Raised as https://issues.apache.org/jira/browse/WICKET-1684

Curiously mentioned in https://issues.apache.org/jira/browse/WICKET-300 but
marked as resolved/invalid.

Rgds

Ned


igor.vaynberg wrote:
> 
> argh, didnt read closely enough. yes that is the right way to do it,
> in fact you shouldnt have to do that at all, fileuploadfield should be
> doing that...
> 
> please create a jira ticket, that component is pretty old so it
> probably got overlooked. multifileuploadfield does it properly
> already.
> 
> -igor
> 

-- 
View this message in context: http://www.nabble.com/Best-way-of-validating-FileUploadField-tp17662018p17662943.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Best way of validating FileUploadField

Posted by Igor Vaynberg <ig...@gmail.com>.
argh, didnt read closely enough. yes that is the right way to do it,
in fact you shouldnt have to do that at all, fileuploadfield should be
doing that...

please create a jira ticket, that component is pretty old so it
probably got overlooked. multifileuploadfield does it properly
already.

-igor

On Wed, Jun 4, 2008 at 10:43 PM, Ned Collyer <ne...@gmail.com> wrote:
>
> So the answer is:
>
> protected void convertInput() {
>    setConvertedInput(getFileUpload());
> }
>
> Is the right way of dealing with this?
>
> I know the validators are used for validation ;)
>
>
> igor.vaynberg wrote:
>>
>> that is what validators are for
>>
>> -igor
>>
>
> --
> View this message in context: http://www.nabble.com/Best-way-of-validating-FileUploadField-tp17662018p17662573.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Best way of validating FileUploadField

Posted by Ned Collyer <ne...@gmail.com>.
So the answer is:

protected void convertInput() {
    setConvertedInput(getFileUpload());
} 

Is the right way of dealing with this?

I know the validators are used for validation ;)


igor.vaynberg wrote:
> 
> that is what validators are for
> 
> -igor
> 

-- 
View this message in context: http://www.nabble.com/Best-way-of-validating-FileUploadField-tp17662018p17662573.html
Sent from the Wicket - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org


Re: Best way of validating FileUploadField

Posted by Igor Vaynberg <ig...@gmail.com>.
that is what validators are for

-igor

On Wed, Jun 4, 2008 at 9:43 PM, Ned Collyer <ne...@gmail.com> wrote:
>
> Hi,
>
> I have the requirement where I need to validate things like image format,
> height, width etc for a file upload.
>
> I'd like to do this using validators, which I can do - but im curious if its
> the right way.
>
>
> Here is what I currently have.
>
>
>
> form.add(fileUploadField = new FileUploadField("fileInput") {
>    protected void convertInput() {
>        setConvertedInput(getFileUpload());
>    }
> });
>
> fileUploadField.add(new AbstractValidator() {
>    protected void onValidate(IValidatable validatable) {
>        FileUpload fileUpload = (FileUpload)validatable.getValue();
>        if (fileUpload.getContentType().equals("text/plain")) {
>            error(validatable);
>        }
>    }
>
>    protected String resourceKey() {
>        return "CustomFileError";
>    }
> });
>
>
> This works... but is it correct?
>
> I'm not handling it in the onSubmit - and don't believe I should have to -
> despite the examples.
>
>
> I have double submit implemented on my base form in a similar fashion to
> what igor recommends here
> http://www.nabble.com/double-form-submission-handling---tp13816084p13850262.html
> - which means I only execute the "submit" if all the validation passes.
> This works SUPER AWESOME thus far :)
> --
> View this message in context: http://www.nabble.com/Best-way-of-validating-FileUploadField-tp17662018p17662018.html
> Sent from the Wicket - User mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
> For additional commands, e-mail: users-help@wicket.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@wicket.apache.org
For additional commands, e-mail: users-help@wicket.apache.org