0

Form validation (methodological approach)

asked 2011-03-23 07:43:08 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

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

delete flag offensive retag edit

17 Replies

Sort by ยป oldest newest

answered 2011-12-02 13:48:43 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

Could the messagebox freezed be a bug?
I do think this form validation thread deserves some more - a dedicated small talk?

link publish delete flag offensive edit

answered 2011-12-02 12:49:49 +0800

SparkySpider gravatar image SparkySpider
444 1 3

updated 2011-12-02 12:50:10 +0800

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());

link publish delete flag offensive edit

answered 2011-12-02 12:39:51 +0800

SparkySpider gravatar image SparkySpider
444 1 3

Got the first part right, thanks to another post of yours, Steva.

Clients.clearWrongValue(exceptionList[0].getComponent());

link publish delete flag offensive edit

answered 2011-12-02 12:02:05 +0800

SparkySpider gravatar image SparkySpider
444 1 3

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

link publish delete flag offensive edit

answered 2011-03-28 08:37:21 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

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.

link publish delete flag offensive edit

answered 2011-03-28 08:18:09 +0800

robertpic71 gravatar image robertpic71
1275 1

>> 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

link publish delete flag offensive edit

answered 2011-03-28 06:05:49 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

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

shows the first two as expected, but nothing (no popup) is shown for the third one.

link publish delete flag offensive edit

answered 2011-03-28 04:42:02 +0800

terrytornado gravatar image terrytornado flag of Germany
9393 3 7 16
http://www.oxitec.de/

Great thanks for this evaluation Stefano. I'm bookmarking this thread.

link publish delete flag offensive edit

answered 2011-03-28 03:59:42 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

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

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 :)!

link publish delete flag offensive edit

answered 2011-03-28 03:17:20 +0800

Steva77 gravatar image Steva77 flag of Italy
1014 3
http://www.research.softe...

updated 2011-03-28 03:26:58 +0800

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

No problems on the console, but nothing prompted on UI either...
Throwing single exceptions works fine instead.
BTW, I had to loop as wve.toArray() caused problem on casting WrongValueException, but I do not think this is the issues here...
Thanks
Stefano

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

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-03-23 07:43:08 +0800

Seen: 1,251 times

Last updated: Dec 02 '11

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More