You could write your own custom validation and add that to the validation and
validation rules xml files. That way you don't have to have the validation in the
validate method of your form.
Also I noticed you didn't call saveErrors(request, errors) in your code. I'm not 100%
sure if you have to call that from the form bean, but I know for sure you have to call
it from any action if you want the errors to get saved.
struts <[EMAIL PROTECTED]> wrote:
Hi-
Getting back to the Validating part of this question. I've got some
REAL ugly code to take care of the problem.
Does anybody have any sample code that uses ValidatorForm so I could get
rid of this mess and put it in my
validation.xml
Cheers,
David Sperling
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
ActionErrors errors = super.validate(mapping, request);
if (errors==null) errors = new ActionErrors();
String readFilePath =
this.getUploadedFile().getFileName().toLowerCase();
if(!readFilePath.equals("") ) {
if(!readFilePath.endsWith("jpg") && !readFilePath.endsWith("jpeg")
// && !readFilePath.endsWith("gif") &&
!readFilePath.endsWith("png")
) {
ActionError error = new ActionError("error.fileMustBeAJPEG");
errors.add("fileNotImageError", error);
}
}
if(this.getUploadedFile().getFileSize() > 512000) {
ActionError error = new ActionError("error.fileTooBig");
errors.add("fileTooBig", error);
}
-----Original Message-----
From: Brian McSweeney [mailto:[EMAIL PROTECTED]
Sent: Friday, August 22, 2003 8:13 PM
To: 'Struts Users Mailing List'
Subject: Validating uploaded file as an image only
Hi everyone,
I've managed to get the struts file upload working successfully,
using the struts FormFile class and the upload example.
Cool :-)
However, in my app, I only want images to be uploaded.
I'm going to store the images on disk and put the path of the
image file into the database. So I need to validate that only images are
getting uploaded, not other file-types.
I know nothing really about how to do this. I could just check that
string returned by:
FormFile.getContentType( )
is equal to image-gif, image-bnp, or image-jpeg. But is this enough?
Also, I have noticed that when I upload an image file and the little
Browser window pops up, if I select a gif image, but the file type
bar says "All files" then the file isn't recognized as an image. It's
recognized as an octet-stream. So does the browser window
specify the file type to the browser?
Anyway,
Hope someone can give me some advice,
As always,
Thanks very much,
Brian
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software