Re: multipart form problem - form validation fails

2012-09-20 Thread Michał Bartoszewski
Hi Andrea,
I am using wicket 1.5.7

2012/9/19 Andrea Del Bene an.delb...@gmail.com:
 Which version of Wicket are you using? I've tested your code enabling file
 upload but it worked fine with both version 1.5.8 and 6.0.0

 class AddProductForm extends FormProduct {

  private FileUploadField photoField;

  public AddProductForm(String id, CompoundPropertyModelProduct
 model) {
  super(id, model);
  //setMultiPart(true);
  //setMaxSize(Bytes.megabytes(1L));

  TextFieldString nameField = new TextFieldString(name);
  nameField.setRequired(true);
  nameField.setLabel(Model.of(Name));
  nameField.add(StringValidator.lengthBetween(3, 255));

  TextAreaString descriptionField = new
 TextAreaString(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 !);
  }
  }
 }



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


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



Re: multipart form problem - form validation fails

2012-09-20 Thread Andrea Del Bene
also with version 1.5.7 I couldn't reproduce your error. I get an error 
because during form submission Wicket doesn't find field 'photo' of 
class Product, but that's perfectly normal. The following is the code I 
used for the test:



public class HomePage extends WebPage {

private static final long serialVersionUID = 1L;

private Product product = new Product();

public HomePage(PageParameters parameters) {
super(parameters);

FormProduct form = new AddProductForm(form, new
CompoundPropertyModelProduct(product));
FeedbackPanel feedback = new FeedbackPanel(feedback);

add(feedback);
add(form);
}
}

class AddProductForm extends FormProduct {

private FileUploadField photoField;

public AddProductForm(String id, CompoundPropertyModelProduct 
model) {

super(id, model);
setMultiPart(true);
setMaxSize(Bytes.megabytes(1L));

TextFieldString nameField = new TextFieldString(name);
nameField.setRequired(true);
nameField.setLabel(Model.of(Name));
nameField.add(StringValidator.lengthBetween(3, 255));

TextAreaString descriptionField = new 
TextAreaString(description);

descriptionField.setRequired(false);
descriptionField.setLabel(Model.of(Description));
descriptionField.add(StringValidator.maximumLength(8000));

photoField = new FileUploadField(photo);

add(nameField);
add(descriptionField);
add(photoField);
}

@Override
protected void onSubmit() {
super.onSubmit();


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 !);
}
}
}

class Product implements Serializable {

private int id;
private String name;
private String description;
}





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



multipart form problem - form validation fails

2012-09-19 Thread Michał Bartoszewski
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
titleProduct administration/title
/head
body
h1Add new product !/h1
div wicket:id=feedback/div
form wicket:id=form
label for=product_nameName/label
input type=text id=product_name wicket:id=name size=20 /
br /
label for=product_descriptionDescription/label
textarea id=product_description wicket:id=description
cols=20 rows=5 /
br /
!--label for=photoPhoto/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);

FormProduct form = new AddProductForm(form, new
CompoundPropertyModelProduct(product));
FeedbackPanel feedback = new FeedbackPanel(feedback);

add(feedback);
add(form);
}
}
 AddProductForm.java =

public class AddProductForm extends FormProduct {

private FileUploadField photoField;

public AddProductForm(String id, CompoundPropertyModelProduct model) {
super(id, model);
//setMultiPart(true);
//setMaxSize(Bytes.megabytes(1L));

TextFieldString nameField = new TextFieldString(name);
nameField.setRequired(true);
nameField.setLabel(Model.of(Name));
nameField.add(StringValidator.lengthBetween(3, 255));

TextAreaString descriptionField = new TextAreaString(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: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org



Re: multipart form problem - form validation fails

2012-09-19 Thread Andrea Del Bene
Which version of Wicket are you using? I've tested your code enabling 
file upload but it worked fine with both version 1.5.8 and 6.0.0

class AddProductForm extends FormProduct {

 private FileUploadField photoField;

 public AddProductForm(String id, CompoundPropertyModelProduct model) {
 super(id, model);
 //setMultiPart(true);
 //setMaxSize(Bytes.megabytes(1L));

 TextFieldString nameField = new TextFieldString(name);
 nameField.setRequired(true);
 nameField.setLabel(Model.of(Name));
 nameField.add(StringValidator.lengthBetween(3, 255));

 TextAreaString descriptionField = new 
TextAreaString(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 !);
 }
 }
}



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