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-03-23 07:55:21 +0800

zknewbie1 gravatar image zknewbie1
370 4

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

link publish delete flag offensive edit

answered 2011-03-23 09:20:48 +0800

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

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?

link publish delete flag offensive edit

answered 2011-03-23 11:19:54 +0800

zknewbie1 gravatar image zknewbie1
370 4

Since it's an Exception object, I think it'll draw validation message for 1 text box at a time. Anyway, see if you could create a quick 'HelloWorld'-type zul page and test it out to see if it works..

link publish delete flag offensive edit

answered 2011-03-23 15:34:21 +0800

robertpic71 gravatar image robertpic71
1275 1

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

link publish delete flag offensive edit

answered 2011-03-23 16:10:20 +0800

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

Robert, do you use the JGoodies validation in the model ?

link publish delete flag offensive edit

answered 2011-03-23 18:22:31 +0800

robertpic71 gravatar image robertpic71
1275 1

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

link publish delete flag offensive edit

answered 2011-03-24 03:22:50 +0800

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

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

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

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