-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Checkbox should implement HasValidation #3397
Comments
Depends on vaadin/web-components#938 to be implemented in the web component. Here is one related concern: vaadin/vaadin-checkbox#188 (comment)
|
Thank you. Meanwhile the workaround is to wrap the Combobox in CustomField: public class CheckboxWithValidation extends CustomField<Boolean> {
private final Checkbox checkbox = new Checkbox();
public CheckboxWithValidation() {
checkbox.addValueChangeListener(e -> updateValue());
add(checkbox);
}
public CheckboxWithValidation(String label) {
this();
setLabel(label);
}
@Override
public void setLabel(String label) {
checkbox.setLabel(label);
}
@Override
public String getLabel() {
return checkbox.getLabel();
}
@Override
protected Boolean generateModelValue() {
return checkbox.getValue();
}
@Override
protected void setPresentationValue(Boolean value) {
checkbox.setValue(value);
}
} Then: @Route("")
public class MainView extends VerticalLayout {
public MainView() {
final CheckboxWithValidation cb = new CheckboxWithValidation("I agree");
cb.addValueChangeListener(e -> {
cb.setInvalid(!cb.getValue());
cb.setErrorMessage(cb.isInvalid() ? "You need to agree to EULA" : null);
});
add(cb);
}
} |
I just want to state my opinion, for future reference – not saying this is the best solution. I think Checkbox should be more of an input control rather than a form field. The form field features (label, required indicator, description, error message, validation) should rather be implemented by a separate component which can be used together with any input control. Therefore, I think it might not be a good idea to implement HasValidation for Checkbox, as that might complicate the former idea in the future. Custom Field is a then perhaps the “correct” solution, but would benefit from a different name and could maybe be made less laborious (less boilerplate)? |
@jouni do you perhaps have in mind some sort of decorator that would wrap around the component and would work with any component? Sounds like a good reusable idea - composition is a very good pattern indeed. Perhaps However, at the moment (nearly) all field components do implement the HasValidation interface at Java side . For consistency reasons, given that Checkbox is also a form component, it would make sense for it to implement HasValidation. Alternatively, all form components should then consistently stop implementing HasValidation. Deciding otherwise feels inconsistent. |
Yeah, pretty much.
Yeah, that seems like a nice default behavior for the Java API.
And yes, I agree with this as well. As we are probably going to live with the existing form fields for quite a while still, perhaps it’s nicer to make them behave consistently. I suppose, if we ever do such a change, we can find a way to migrate from the "form field" |
It would make sense to implement |
Should this be closed? Will be released in 24.4 🤔 |
Yes, this is implemented by #6167. Closing as completed per above comment. |
Describe your motivation
The "I agree with EULA"/"I agree with the following terms"/"I agree" checkbox at the bottom of the form. Unless the Checkbox is checked, the form can not continue. This can easily be implemented by a Checkbox+Binder+Validator which only passes the "true" value. Or
@javax.validation.constraints.AssertTrue(message="Please accept the terms before continuing")
.Describe the solution you'd like
Checkbox should implement HasValidation and should be able to mark itself invalid and show a validation error, exactly as other HasValue components.
Describe alternatives you've considered
No response
Additional context
No response
The text was updated successfully, but these errors were encountered: