0

How to force constraint validations before processing the form

asked 2011-08-04 05:58:23 +0800

mrego gravatar image mrego
63 1
http://blogs.igalia.com/m...

I've found the following way to show constraint error messages when I click the button, in order to don't process the data while it's not valid yet.

My example is here: http://zkfiddle.org/sample/1o3ojpi/2-Force-validation-of-constraints-before-processing-the-form

ZKFiddle-Link

TestComposer.java
package j1o3ojpi$v2;

import org.zkoss.zk.ui.*;
import org.zkoss.zk.ui.event.*;
import org.zkoss.zk.ui.util.*;
import org.zkoss.zk.ui.ext.*;
import org.zkoss.zk.au.*;
import org.zkoss.zk.au.out.*;
import org.zkoss.zul.*;
import org.zkoss.zul.impl.*;

import java.util.*;

public class TestComposer extends GenericForwardComposer{

private Component form;
private Label label;

public void doAfterCompose(Component comp) throws Exception {
super.doAfterCompose(comp);
}

public void onClick$btn(Event e){
check(form);

// This line is only executed is form is valid
label.setValue("Validation OK!");
}

private void check(Component component) {
checkIsValid(component);

List<Component> children = component.getChildren();
for (Component each: children) {
check(each);
}
}

private void checkIsValid(Component component) {
if (component instanceof InputElement) {
if (!((InputElement) component).isValid()) {
// Force show errorMessage
((InputElement) component).getText();
}
}
}

}


index.zul
<zk>
<window border="normal" title="hello" apply="j1o3ojpi$v2.TestComposer">

<grid id="form" width="400px">
<columns>
<column label="Label" width="100px" />
<column label="Value" width="300px" />
</columns>
<rows>
<row>
<label value="Text" id="text" />
<textbox constraint="no empty" />
</row>
<row>
<label value="Date" id="date" />
<datebox constraint="no empty" />
</row>
</rows>
</grid>

<button label="Save" id="btn" />

<label value="" id="label" />

</window>
</zk>

Not sure if it's the best way to do it, but it's working fine so far.

Any feedback is welcomed ;-)

Bye,
Rego

delete flag offensive retag edit

4 Replies

Sort by ยป oldest newest

answered 2011-08-04 07:49:14 +0800

gersonlc gravatar image gersonlc
129 1

Hi Rego,

I think that is ok. I did something like your code and put in myproject.package.zk.util to turn it generic for my project.

Regards

link publish delete flag offensive edit

answered 2011-08-04 07:56:04 +0800

mhj gravatar image mhj flag of Brazil
806 1 7

updated 2011-08-04 07:57:03 +0800

I made a class that validates the fields:

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package ;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.Bandbox;
import org.zkoss.zul.Constraint;
import org.zkoss.zul.Textbox;
/**
*
* @author mhj
*/
public class Validator implements Constraint, java.io.Serializable {
private static final long serialVersionUID = 1L;
public Validator() {
super();
}
@Override
public void validate(Component comp, Object value) {
String nmField = "";

String enteredValue = String.valueOf(value);

if (enteredValue.isEmpty()) {
if (comp instanceof Textbox) {
Textbox var = (Textbox) comp;
nmField = var.getTooltip();
var.setFocus(true);
} else if (comp instanceof Bandbox) {
Bandbox var = (Bandbox) comp;
nmField = var.getTooltip();
var.setFocus(true);
}
//throw new WrongValueException(comp, String.format(Texts.MSG_OBRIG_FIELD, nmCampo));
throw new WrongValueException("Field " + nmField + " must be completed.");
}
}
}

link publish delete flag offensive edit

answered 2011-08-04 07:59:57 +0800

mhj gravatar image mhj flag of Brazil
806 1 7

in the controller I add the required fields:

private void addConstraints() {
bandboxDocumentType.setConstraint(new Validator());
textboxTitle.setConstraint(new Validator());//and more....
}

link publish delete flag offensive edit

answered 2011-08-07 17:02:16 +0800

TonyQ gravatar image TonyQ
642
https://www.masterbranch....

Great topic! Thanks for sharing samples. ! :)

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-08-04 05:58:23 +0800

Seen: 413 times

Last updated: Aug 07 '11

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