0

No constraint="no empty" on Checkboxes and Radiogroups?

asked 2010-07-06 08:32:43 +0800

Mikkel gravatar image Mikkel
6

Hi,

Is was wondering if anyone knows how to declaratively specify that a Checkbox must be checked, or a Radiogroup must have a selected item? Intuitively I would have thought that adding a constraint="no empty" seemed like a good way to achieve this, however this doesn't work as these components don't except constraints.

I realize I could do this in the event handler and just throw an WrongValueException from there and that would probably work. However this is part of a fairly complex, large application and I would really like to have some uniform way of handling input validation and constraint="" and regular expressions fit the bill perfectly. Otherwise we would need to roll our own validation scheme which kind of seems to defeat the purpose of zk having a built-in one in the first place.

Any help is appreciated.

Kind regards,
Mikkel Løkke

delete flag offensive retag edit

7 Replies

Sort by » oldest newest

answered 2010-07-08 23:10:38 +0800

iantsai gravatar image iantsai
2755 1

to checkbox, you can set it to checked then disable it to make sure nobody can change it.

to radiogroup, give it a default value, then user has no way to give an empty value.

link publish delete flag offensive edit

answered 2012-08-27 17:16:20 +0800

rron gravatar image rron
6

Hi,

I have this problem in my project,

The radiogroup should not have default value, but it does not have the parameter constraint.

how to deal with it? I need this functionality...

thanks!

link publish delete flag offensive edit

answered 2012-09-03 03:29:59 +0800

iantsai gravatar image iantsai
2755 1

could you at least provide a pseudo code to show up the concept in your mind? thanks.

link publish delete flag offensive edit

answered 2012-09-12 12:23:09 +0800

rron gravatar image rron
6

<radiogroup constraint="no empty">
<radio label="Apple" ></radio>
<radio label="Orange" ></radio>
<radio label="Banana" ></radio>
</radiogroup>

Message - > Method setConstraint not found for class org.zkoss.zul.Radiogroup

In another words, the component "radiogroup" dont have the parameter "constraint" in your implementation. How I can introduce this functionality.

Extends de radiogroup? and later what?

I Want the "Hint".

link publish delete flag offensive edit

answered 2012-11-09 15:39:07 +0800

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

PUSH!

link publish delete flag offensive edit

answered 2012-11-19 04:56:21 +0800

benbai gravatar image benbai
2228 6
http://www.zkoss.org

Hi rron,

Yes you can use a custom java class extends radiogroup to achieve this,

e.g.

test.zul

<window title="Radiobox & Radio Demo" width="200px" border="normal">
    <vbox>
        <radiogroup id="rg" use="test.CustomRadiogroup"
        	constraint="no empty" onCheck="">
            <radio label="Apple" />
            <radio label="Orange" />
            <radio label="Banana" />
        </radiogroup>
        You have selected :
        <label id="fruit" style="color:red" />
        <button label="click to refresh page">
        	<attribute name="onClick"><![CDATA[
				// try get label of selected item
				// true denotes should check the constraint
        		String label = rg.getSelectedItem(true).getLabel();
				System.out.println(label);
				// refresh page
				Executions.getCurrent().sendRedirect("");
        	]]></attribute>
        </button>
    </vbox>
</window>

CustomRadiogroup.java

package test;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Radiogroup;

public class CustomRadiogroup extends Radiogroup {
	private String _constraint;
	public void setConstraint (String constraint) {
		_constraint = constraint;
	}
	// overload instead of override since
	// getSelectedItem () used in original Radiogroup
	public Radio getSelectedItem (boolean checkConstraint) throws WrongValueException {
		Radio radio = super.getSelectedItem();
		System.out.println(radio);
		if (radio == null) {
			if (_constraint != null && _constraint.contains("no empty")) {
				String msg = "should select one radio item";
				System.out.println(" constraint ");
				throw new WrongValueException(this, msg);
			}
		}
		return radio;
	}
}

Regards,
Ben

link publish delete flag offensive edit

answered 2012-11-19 11:09:49 +0800

RichardL gravatar image RichardL
768 4

This is a good workaround but I think that the default no constraint message is not relevent to a radiogroup,so it would be better to use a custom constraint

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: 2010-07-06 08:32:43 +0800

Seen: 903 times

Last updated: Nov 19 '12

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