JSR 303 - Validate a field if it is not null - validation

I have a form which is filled in, and some of the fields are options. I want to apply validations on the information that was filled in but the optional fields should be validated only if there was something filled in, so if theses are not null. Did any of you do something similar?
Or do you have any suggestion?

Bean Validation constraints usually accept null as valid value (with the exception of #NotNull of course). Depending on your UI framework you might retrieve empty strings instead of null for fields without user input. If you're are working with JSF 2, you can set the context parameter javax.faces.VALIDATE_EMPTY_FIELDS to false to avoid a validation of empty fields.

Related

How to check thrown errors during Sequelize validation execution

I have a sequelize model, which needs custom validation. That custom validation relies on some foreign keys being valid (valid as in valid uuid, not checked that they exist in db) on a record. There is also another validator appended that checks for mutual exclusivity of the foreign keys (only one can be present at the same time). After all this, I access the database in another validator to fetch data from another collection. I dont want this to happen if any of above validators fail because I dont want to access the database if I dont have to and I dont want to check if uuid is valid again because that is a job for one of previous validators.
The order is like so:
check if field contains valid type (uuid) - field type validator
check that field is not clashing with another field (mutually exclusive) - model-wide validator
fetch record from another collection to do further validation - model-wide validator
I want to check if previous validator has already thrown an error and not execute the next one if it has. Even better if I can check which one has thrown an error. Sequelize executes all validators even if error was already thrown. In documentation it says:
Any error messages collected are put in the validation result object
alongside the field validation errors, with keys named after the
failed validation method's key in the validate option object. Even
though there can only be one error message for each model validation
method at any one time, it is presented as a single string error in an
array, to maximize consistency with the field errors.
I tried several ways to access this "error" object but to no avail. Is there a way at all to know if error was already thrown by one of previous validators?

Can I use a JSF validator both ways?

I have an application where you can create Items. Those items are saved in a database with the primary key beeing its itemnumber. In the facelet where you create the Item I have a h:inputText with a validator which checks if the item is already in the database or not. If it is I give out an error message. I now have a second facelet where you can order Items by placing its number into an inputText field. Now I want the Validator to react in the opposite way as before.
So is there any way I can define the expected outcome of a validator? Or do I have to make a second validator?

How can redux-form validation based on multiple fields be implemented?

Redux Form validation examples only include individual field validations.
Having a list of a certain number of fields that are the same, how can I model a validation that requires at least one of the inputs to be a valid email address?
For example, for the given form made up of the following fields:
User 1,
User 2,
User 3,
Special User 4
How can I write a validation function that requires Special User 4 to always be set, and either one of the first 3 users?
Your synchronous validation function is given all the values of the form. Your errors must correspond to a field key, or you can use the generic _error key, which will result in this.props.error being populated.
Does that make sense?

How to validate not blank values in Access Data Validation

I'm just wondering if there is a way in Access Data Validation to run if and only if the fields they are validating are not blank? In other words, do not run data validation if the corresponding fields are blank?
Must I insert an if expression before the data validation rule to do so?

Different validation on a field depending on user selection

For example lets say I have the following validator:
Acme\BlogBundle\Entity\Person:
properties:
ID:
- NotBlank: ~
However the kind of validation for ID depends on the property IDType that is selected by a user. Which some IDS can be blank and some cannot. Not only that but other types of ID needs other types of validation. Is this possible to do? Or should I have one property for each ID?
The Callback validation constraint is very suitable in this case. It lets you define complex validation logic and add errors to any form field you see fit.

Resources