Hi,
I am building form that is responsible for submiting simple product information:
- name
- description
- photo
I previously had this form working with name and description then I
have decided to add photo filed for upload and situation got weird.
I had to make form multipart and set maximum allowed file size plus
write some logic to save this file on disk etc.
After those changes I am receiving validation error which tells me
that required field 'name' is empty ?!
But before submiting this form I have filled all required fields.
So I have started to investigate this problem but unfortunatelly I was
able only to indicate lines of code that couses this problem. (Those
lines are commented in code example below).
I am suspecting that problem may lay in CompoundProperty model I am
using to pass Product to form components
or it has to do with some logic that is behind multipart form handling.
Code:
========= ProductPage.html ==========
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:wicket="http://apache.wicket.com/wicket" xml:lang="en"
lang="en">
<head>
<title>Product administration</title>
</head>
<body>
<h1>Add new product !</h1>
<div wicket:id="feedback"></div>
<form wicket:id="form">
<label for="product_name">Name</label>
<input type="text" id="product_name" wicket:id="name" size="20" />
<br />
<label for="product_description">Description</label>
<textarea id="product_description" wicket:id="description"
cols="20" rows="5" />
<br />
<!-- <label for="photo">Photo</label>
<input type="file" wicket:id="photo" id="photo"/>-->
<input type="submit" value="Add" />
</form>
</body>
</html>
======== ProductPage.java ===========
public class ProductPage extends WebPage {
private static final long serialVersionUID = 1L;
private Product product = new Product();
public ProductPage(PageParameters parameters) {
super(parameters);
Form<Product> form = new AddProductForm("form", new
CompoundPropertyModel<Product>(product));
FeedbackPanel feedback = new FeedbackPanel("feedback");
add(feedback);
add(form);
}
}
======== AddProductForm.java =========
public class AddProductForm extends Form<Product> {
private FileUploadField photoField;
public AddProductForm(String id, CompoundPropertyModel<Product> model) {
super(id, model);
//setMultiPart(true);
//setMaxSize(Bytes.megabytes(1L));
TextField<String> nameField = new TextField<String>("name");
nameField.setRequired(true);
nameField.setLabel(Model.of("Name"));
nameField.add(StringValidator.lengthBetween(3, 255));
TextArea<String> descriptionField = new TextArea<String>("description");
descriptionField.setRequired(false);
descriptionField.setLabel(Model.of("Description"));
descriptionField.add(StringValidator.maximumLength(8000));
//photoField = new FileUploadField("photo"); (photo is not
part of Product class)
add(nameField);
add(descriptionField);
//add(photoField);
}
@Override
protected void onSubmit() {
super.onSubmit();
info("Following product was added: " + getModelObject().getName());
new ExpressdrukBeanAccessFacade().saveProduct(getModelObject());
if (photoField != null) {
FileUpload fileUpload = photoField.getFileUpload();
if (fileUpload != null) {
Folder folder = new
Folder(System.getProperty("java.io.tmpdir"));
try {
folder.ensureExists();
} catch (IOException ex) {
error("Folder can't be created");
return;
}
File photo = new File(folder, fileUpload.getClientFileName());
if (photo.exists()) {
error("File with this photo already exists !");
return;
}
try {
fileUpload.writeTo(photo);
} catch (IOException ex) {
error("Can't write file with photo to folder");
return;
}
info(String.format("File %s was saved successfully !",
fileUpload.getClientFileName()));
}
} else {
error("Photo filed is null !");
}
}
}
===== Product.java ======= (just simple POJO nothing intresting)
public class Product implements Serializable {
private int id;
private String name;
private String description;
}
Best regards,
Michał Bartoszewski
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]