Is it possible to disable jsr303 (bean validation) in AbstractForm? I would like to validate only defined properties.
In my case, I use the wizard addon allowing me to build my bean across different steps. However, the bean is not fully valid after the first step because some fields are missing and will be filled in future steps of my wizard. The MBeanFieldGroup is always returning the jsr303 validation when a property changes. For each step, I pass the bean saved from previous step to a new AbstractForm. It would be great if the form could validate only defined fields (with cross validation of course) across a setter or something. Is it a good idea of pull request?
I would be also interested in this feature. In my case I would like to use the Abstractform for
a) viewing and editing and
b) searching.
While during edit all fields should be valid before clicking save button, when searching for entities not all of the search fields need to be filled and additionally the fields do not need to contain valid values as search is performed with LIKE “%fieldValue%”.
So it would be great to be able to use the AbstractForm without getting validation errors.
It is now part of the latest viritin version. By default, only bound fields will be validated. If you want the form the validate all bean properties, call form.setValidateOnlyBoundFields(false);
I’m using 1.59. Manually setting the values between entity and fields is what I want to avoid, because there are really lot of properties which are used as search criteria.
What do you mean by using “search” with an AbstractForm? I would like to understand what you need.
Basically, an abstract form is used to edit an entity which properties are bound to form fields.
If you have validators on your entity class and want to apply them in a particular situation, I recommend you to create your custom validator in your form class when initializing your fields and remove the ones in your entity class.
if (editmode) {
myField.addValidator(…);
}
I have an entity, lets use “Company” as example. A company has about 20 properties.
I have a CompanyForm extending AbstractForm which is used to view and edit companies. So far working fine with validation.
Now there is a requirement to search for companies. To avoid creating another form with all theese 20 properties I decided to use a CompanySearchForm extending Company form.
In this form the user can enter values into the property fields (bound to the company entity) and instead of saving the entity I would like to perform a query-by-example search with the company.
The search is quite simple:
final Example<Company> example = Example.of(companySearchForm.getEntity(), matcher);
return repo.findAll(example);
But to start the search I need the entity from the form. But until it is validated by AbstractForm I am unable to get the entity with the values entered by the user. And because some company properties have NotNull oder Size validations the entity will not become valid if the user just wants to search for company with name starting with “Foo”.
So the validation of the entity itself are correct for real entities and must be validated e.g. before saving, so I don’t like to remove them from the entity completely, as this is the default case. The special condition is searching when this validations do not matter.
So how to get my entity from the AbstractForm when it is not valid?
Personnally, I would create a complete different form (not inheriting from CompanyForm) with a distinct entity; let’s say CompanyCrieria. CompanyCrieria will not have NotNull and Size constraints. I would also create a constructor from Company which takes a CompanyCriteria as parameter.
This way, the validators would be bypassed by this constructor and you would have a “valid” company to search.
public Company(CompanyCriteria criteria) {
// copy all values
}
public class CompanySearchForm extends AbstractForm<CompanyCriteria>
[/code][code]
Company company = new Company(companySearchForm.getEntity());
final Example<Company> example = Example.of(company, matcher);
return repo.findAll(example);
But of course this will result in lots of duplicate code and copying between Company and CompanyCriteria - which are identical except for the validations on the 20 properties.
And all this because AbstractForm does not let me go away with invalid entities? I think trying to find a solution overwriting AbstractForm or some methods of it should be a better solution.
I finally solved this with overwriting setEntity() in CompanySearchForm.
@Override
public MBeanFieldGroup<Company> setEntity(final Company entity) {
final MBeanFieldGroup<Company> result = super.setEntity(entity);
// remove all validation stuff from form
result.clearValidators();
result.getFields().forEach(f -> {
f.setRequired(false);
f.removeAllValidators();
});
return result;
}