How would I allow some empty field on form submission? What do I need to add on my controller? Because the form would only insert data if all fields weren't empty.
Thanks in advance
Printable View
How would I allow some empty field on form submission? What do I need to add on my controller? Because the form would only insert data if all fields weren't empty.
Thanks in advance
Depends what the field is. AFAIK, if it's a primitive you can't if your binding to it. Otherwise you can define it in your property editor. For example, CustomDateEditor has the signature:Quote:
How would I allow some empty field on form submission?
Code:CustomDateEditor(DateFormat dateFormat, boolean allowEmpty)
So it would be better to get the value with request.getParameter() rather than binding it?
All HTML input types *except for checkboxes* will return an empty string. Is this what you mean?
To get around the horrific flaw in checkbox handling, you can have a hidden element with "_propertyName" in your form. When spring binds it will intialise to the default values *including primitives* any properties which have a corresponding "_propertyName" parameter.
HTH.
I'm sure yatesco means horrific handling by browsers ;)Code:To get around the horrific flaw in checkbox handling
This is a problem for all server side handling of checkboxes. You can also override onBindAndValidate:
Code:protected void onBindAndValidate(HttpServletRequest request, Object command, BindException errors) throws java.lang.Exception {
MyObject myObject = (MyObject) command;
myObject.setLikesSpring(request.getParameter("likesSpringCheckBox") != null);
}
Probably not if it is an object. I guess your other option is to set the allowFields property accordingly, and do the binding yourself - a bit messy though.Quote:
So it would be better to get the value with request.getParameter() rather than binding it?
yes, I did mean in browsers ;) :)
strbuf; I am not sure I get your problem, are you asking whether a form can be partially populated, i.e. only with parameters that are sent?
For the intellectually challenged amongst us (i.e. me ;)) can you explain the problem, and the solution you want.
Say I have two input fields.
input type="text" name="name"
and
input type="text" name="address"
I want the form still submitted/insert the data eventhough the address field is not filled in/empty. Using spring:bind won't allow me to have empty fields on form submission.
Why? What exception do you get?
assume:
you can certainly bind on the properties of that form regardless of whether they are null :)Code:class MyForm {
private String address;
private String name;
//accessors hidden.
}
This should work. You might have to post the relevant code to get this sorted out.Quote:
Using spring:bind won't allow me to have empty fields on form submission
Can I suggest first looking at the sample applications in the Spring distribution, and try submitting empty fields yourself.