-
FEATURED COMPONENTS
First time here? Check out the FAQ!
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
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();
}
}
}}
<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
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.");
}
}
}
Great topic! Thanks for sharing samples. ! :)
Asked: 2011-08-04 05:58:23 +0800
Seen: 413 times
Last updated: Aug 07 '11