开发者

How to conditionally change a ListGridField's required attribute in SmartGWT?

I've been trying to achieve similar functionality on ListGrid that already exists for FormItems: com.smartgwt.client.widgets.form.validator.RequiredIfValidator.

I want to set a column required based on a value of another column. Ie. we got ListGridFields A = "Need more coffee? If not, why?" and B for answering the question. B is not required until A has a certain value.

ListGrid grid = new ListGrid();
// Boolean
ListGridField questionA = new ListGridField("questionA", "Need more coffee?");
questionA.setRequired(true);
// String
ListGridField reason = new ListGridField("reason", "Reason");
reason.setRequired(false); // should become true when A is not checked.
grid.setFields(questionA, 开发者_如何学Pythonreason);

I tried using the same validator for the ListGridField and got the validator to fire, but it does not change the field's required attribute, no matter what gets returned from the RequiredIfFunction's execute() method.

Is there a neat way to deal with this?


Although it makes sense, the other answer for this question, equivalent to myConditionalValidatedField.setRequired(myCheckBox.getValueAsBoolean()) in my example code, didn't work for me. I fixed the problem doing the following:

    RequiredIfValidator validator = new RequiredIfValidator();
    validator.setExpression(new RequiredIfFunction() {
        @Override
        public boolean execute(FormItem formItem, Object value) {
            return myCheckBox.getValueAsBoolean();
        }
    });
    validator.setErrorMessage("Required field if 'myCheckBox' is checked");
    myConditionalValidatedField.setValidators(validator);

Hope ot helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜