-
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
Could the messagebox freezed be a bug?
I do think this form validation thread deserves some more - a dedicated small talk?
For those of you interested. Throwing a validation exception :
1) Breaks any messagebox / dialog you're in.
2) Stops you from completing the method / doing further processing.
Instead of throwing an exceptionlist, you can just manually pop up validation errors on whatever you like.
Clients.wrongValue(exceptionList[0].getComponent(), nameEmpty.getMessage()); Clients.wrongValue(exceptionList[1].getComponent(), telEmpty.getMessage());
Got the first part right, thanks to another post of yours, Steva.
Clients.clearWrongValue(exceptionList[0].getComponent());
Hi guys,
Thanks for all your posts. There are 2 things I'm not able to figure out.
How do you clear exceptions.
I created an array of exceptions, then I created a list that I use keep a reference to all the WrongValuesExceptions. I then threw the WrongValuesException. Then later, I try and loop through my list and clear all the errors by looping through them and setting each one to null. What is the correct way to do this?
Also, any idea how to throw an exception from a messagebox. When you throw an exception from a messagebox, the messagebox gets stuck.
Looking forward to your response.
Thanks,
Mark
My further two cents...
Before the WrongValuesException is handled (correctly showing the message boxes), the UI shows for a moment the field validation for the first field in the form.
Then it is removed and replaced with the message coming from the server/side validation.
>> The code above correctly prompts red error boxes just next to the addressed field, all at once!
>> Soooo cool!
>> BTW - is this the expected behaviour? Maybe a note in documentation might help (a part of course this GREAT thread :)!
Yes, this was an expected behaviour. I've only found hints in the api and the featurerequest.
I've already posted the link to this featurerequest in this thread. There is an example inside featurerequest.
>> 2.) fire a whole List of WrongValueException, check this finished featurerequest
>> shows the first two as expected, but nothing (no popup) is shown for the third one.
This is maybe a bug - or never been expected that way --> feature request
Another experience with WrongValueException:
In some cases - no WrongValueException reach the GUI:
i.e. you enter a new screen, your program can prefill the inputfields (from or url or session data). The user could pass the screen without hit enter or ok - but if there is an error,
the screen should be shown with WrongValueException. However i see nothing the first time. There also some databinding-event that clears the WrongValueExceptions.
So I fire alle my WrongValueExecption via PostEvent - to get the correct timing for i.e. new screens and/or databinding.
/Robert
Just for completeness, mixing the approach:
WrongValueException [] wvea = new WrongValueException[3]; wvea[0] = new WrongValueException(c1, "Name shouldn't be empty!"); wvea[1] = new WrongValueException(c2, "Description shouldn't be empty!"); wvea[2] = new WrongValueException("The whole thing shouldn't be empty!"); throw new WrongValuesException(wvea);
Great thanks for this evaluation Stefano. I'm bookmarking this thread.
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); }
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
Asked: 2011-03-23 07:43:08 +0800
Seen: 1,251 times
Last updated: Dec 02 '11