Using setRequeried in checkbox

Hi,

I’m using a vaadin with Liferay, and i have a problem when i like a checkbox as required. The following is a resume snippet:



/* Get Form Lazy getter */
	public Form getFrm() {
		if (fFrm == null) {
			fFrm = new FormLayout(fData);
			fFrm.setWidth("300px");
			fContext.setDebugId(fFrm, Consts.ID_FRM.getValue());
			fFrm.setImmediate(true);
		}
		return fFrm;
	}

/* Get CheckBox Lazy getter */
	public CheckBox getChk() {
		if (fChk == null) {
			fChk = new CheckBox();
			fContext.setDebugId(fChk, Consts.ID_CHK.getValue());
			fChk.setRequired(true);
			fChk.setRequiredError(fContext.localize(Consts.ERROR_CHK.getValue()));
			fChk.setImmediate(true);
		}
		return fChkAgreed;
	}

	public class FormLayout extends Form {

		public FormLayout(final MyItem Item) {

			setSpacing(true);

			addComponent(getChk());
			setFormFieldFactory(new SignupFieldFactory());
			setItemDataSource(Item); // bind to POJO via Item
		}

		@Override
		protected void attachField(final Object propertyId, final Field field) {}
	}

	public MyView(Context context, final MyItem data) {
		fContext = context;
		fData = data;
		addComponent(getFrm());
	}

My problem is that i can’t validate that checkbox is checked, when i use the commit in form this is ignored. I need added my own validator:



		MyView.getChk().addValidator(new Validator() {

			/** */
			private static final long	serialVersionUID	= 1L;

			@Override
			public boolean isValid(final Object value) {
				if (value == null)
					return false;
				else
					return (Boolean) value;
			}

			@Override
			public void validate(final Object value) throws InvalidValueException {
				if (!isValid(value))
					throw new InvalidValueException(localize(Consts.ERROR_CHK.getValue()));
			}
		});

The validator resolve my problem but when i load by first time my portlet, the form is validated and show notification warning.

I checked the isRequeried() implementation and checked some old tickets http://dev.vaadin.com/ticket/3851 and i think that the problem is the isValid() implementation



    public boolean isValid() {

        if (isEmpty()) {
            if (isRequired()) {
                return false;
            } else {
                return true;
            }
        }
.
.

Always return a boolean value and the checkbox is never null, but I don’t sure what is wrong in my approach. How i remove the notification in form.

Thanks for your help,
Cristian
11631.png

The required property doesn’t work because it is usually meaningless for a CheckBox, as a CheckField can’t be “empty”. Unchecked state isn’t empty. You also can’t give null values for it, unlike with most other fields.

Your case is a rather special one, and I suppose the required property would be meaningful in that case. In your case, it would be useful only for displaying the * required indicator. You can have that by putting a Label on the right side of the CheckBox.

Your validator looks OK and should work, as you actually say it does.

I set the required in true and the * notification is not showing in the CheckBox (Usually on the right side).

I have doubt, there is a way to disable the validations when this view is loading the first time. I initialize my itemProperty for the CheckBox with ‘false’ value and not like show the warning notification when the form is showing the first time.