-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Provided that:
- ZK offers a great support to constraints on singe fields (contraints can also be properly customized)
- ZK offers a great databinding mechanism to have in e.g. session a bean ready to be stored on a db
which is the best approach for form validation on submit?
So far my CRUD form page has a listener extending ZK's GenericForwardComposer which reacts to submit button's onclick event by storing on DB.
Should all validation be done on server side?
Then, how to prompt messages to user (e.g. highlighting missing mandatory fields)?
Is there any ZK's native mechanism I am missing?
Thanks in advance,
S
Hi Steva77, you can use the WrongValueException in your Controller class to have the error message displayed just like client side validation, for example:
in your Controller class:
=================
if (txtBox1.getValue() != <some check value>) {
throw new WrongValueException(txtBox1, "Sorry, value is not allowed");
}
Hi zknewbie1,
thanks, I suppose this refers to the possibility to have the feedback compliant to the native ZK constraints...
So:
- I should develop my validation code on server side
- make all necessary controls on the e.g. session bean's field (I use data binding)
if (ok)
<store on db>
else
for each <control>
<prompt message>
Could it work like this?
>> Should all validation be done on server side?
Some basic checks can be done the client siede: i.e. only numerics for intbox...
I prefer validations on serverside and better: inside the model/service-tear (for mulitple clients, ZK, Webservice..).
ZK offers many tools but not a complete "Validationframework".
Some tools:
1.) fire the WrongValueException for one Component
2.) fire a whole List of WrongValueException, check this finished featurerequest
3.) use CustomConstraints
1-3.) create some util stuff to minimize the code i.e.
login.zul <textbox id="user"..> ... controller: public void err(String component, String errorText) { InputElement inp = (InputElement) win.getFellow(component); inp.focus(); throw new WrongValueException(inp, errorText) } and use like .. err("user", "Login failed"); ..
/Robert
Robert, do you use the JGoodies validation in the model ?
Hi Stefan,
not planned at this time - i have not worked with JGoodies until now.
I use some own wrapper-stuff. This require the correct id's for the components. I've also done some tests with Hibernate Validator.
I think, i did not found the final resolution. It works, but there is lot of space for improvements....
Most validationframeworks (like the Hibernate Validator) are made you simple constraints i.e. number 1, 2, 3, 4, email-format...
But i've to do more painfull contraints with many other sources (definied in many configuration tables).
/Robert
Thanks guys,
this is what I wanted to check - I am not missing any specific ZK native validation framework/approach.
I will do it on server-side then, mixing propietary logic and native support for prompting messages.
cheers
S
Hi guys,
experimenting your suggestions:
private void validateForm(){ HttpSession session = (HttpSession) Sessions.getCurrent().getNativeSession(); Project p = (Project) session.getAttribute("newProject"); ArrayList<WrongValueException> wve = new ArrayList<WrongValueException>(); if (p.getName()==null) wve.add(new WrongValueException("Name shouldn't be empty!")); if (p.getDescription()==null) wve.add(new WrongValueException("Description shouldn't be empty!")); if (wve.size()>0) { WrongValueException [] wvea = new WrongValueException[wve.size()]; for (int i = 0; i<wve.size(); i++) wvea<i > = (WrongValueException) wve.get(i); throw new WrongValuesException(wvea); } }
P.S. I don't know why but my code is not wvea<i > = (WrongValueException) wve.get(i);, square brackets are used - slight bug in code rendering?
P.P.S other interesting posts about the topic:
http://www.zkoss.org/forum/listComment/11916
http://www.zkoss.org/forum/listComment/11525
TA-DAH!
This works instead (see how Exceptions are now bound to Component!):
private void validateForm(Event e){ HttpSession session = (HttpSession) Sessions.getCurrent().getNativeSession(); Project p = (Project) session.getAttribute("newProject"); Component c1 = e.getTarget().getSpaceOwner().getFellow("name"); Component c2 = e.getTarget().getSpaceOwner().getFellow("description"); WrongValueException [] wvea = new WrongValueException[2]; wvea[0] = new WrongValueException(c1, "Name shouldn't be empty!"); wvea[1] = new WrongValueException(c2, "Description shouldn't be empty!"); throw new WrongValuesException(wvea); }
Great thanks for this evaluation Stefano. I'm bookmarking this thread.
Asked: 2011-03-23 07:43:08 +0800
Seen: 1,251 times
Last updated: Dec 02 '11