Class: ChoiceValidator

sogv.ChoiceValidator

This constraint is used to ensure that the given value is one of a given set of valid choices.

It can also be used to validate that each item in an array of items is one of those valid choices.

new sogv.ChoiceValidator (data, options, optionRules, lang, internal)

Create a new Validator.

Name Type Description
data * The data which needs to be validated.
options Object The setting options
optionRules Object The validation rules for setting options.
lang String The language used by the application. Default: "en".
internal Boolean If this parameter is true, it means, that validation called from core.
Properties:
Name Type Description
alias Array

The aliases for the current validator.

They could be used in the short validation format.

Defined aliases: ['choice'].

options Object The description of the required options.
Example
var validator = new sogv.ChoiceValidator(["Liam", "Noah", "William", "James"], {
    'choice': {
        "choices": [
            "Liam", "Noah", "William", "James", "Logan", "Benjamin", "Mason", "Elijah",
            "Oliver", "Jacob", "Lucas", "Michael", "Alexander", "Ethan", "Daniel",
            "Matthew", "Aiden", "Henry", "Joseph", "Jackson", "Samuel", "Sebastian",
            "David", "Carter", "Wyatt", "Jayden", "John", "Owen", "Dylan", "Luke"
        ],
        "multiple": true,
        "min": 5
    }
});
if (false === validator.isValid()) {
     validator.errors().first();
}

Extends

Members

callback String | Array | Closure

This is a callback method that can be used instead of the choices option to return the choices array.

choices Array

A required option (unless callback is specified) - this is the array of options that should be considered in the valid set.

The input value will be matched against this array.

data * inherited

Data that needs to be validated.

lang String inherited

Language of error messages.

max Integer

If the multiple option is true, then you can use the max option to force no more than XX number of values to be selected.

For example, if max is 3, but the input array contains 4 valid items, the validation will fail.

maxMessage String

This is the validation error message that's displayed when the user chooses too many options per the max option.

Default: "You must select at most {{ limit }} choices."

You can use the following parameters in this message:

Parameter Description
%%limit%% Max count of selected options
%%choices%% A comma-separated list of available choices
%%value%% The current (invalid) value

message String

This is the message that you will receive if the multiple option is set to false and the underlying value is not in the valid array of choices.

Default: "The value you selected is not a valid choice."

You can use the following parameters in this message:

Parameter Description
%%value%% The current (invalid) value

min Integer

If the multiple option is true, then you can use the min option to force at least XX number of values to be selected.

For example, if min is 3, but the input array only contains 2 valid items, the validation will fail.

minMessage String

This is the validation error message that's displayed when the user chooses too few choices per the min option.

Default: "You must select at least {{ limit }} choices."

You can use the following parameters in this message:

Parameter Description
%%limit%% Min count of selected options
%%choices%% A comma-separated list of available choices
%%value%% The current (invalid) value

multiple Boolean

If this option is true, the input value is expected to be an array instead of a single, scalar value.

The constraint will check that each value of the input array can be found in the array of valid choices.

If even one of the input values cannot be found, the validation will fail.

Default: false

multipleMessage String

This is the message that you will receive if the multiple option is set to true and one of the values on the underlying array being checked is not in the array of valid choices.

Default: "One or more of the given values is invalid."

You can use the following parameters in this message:

Parameter Description
%%value%% The current (invalid) value

Methods

errors ()sogv.Error inherited

Return error errors

Returns:
Type Description
sogv.Error Error messages

isValid ()Boolean inherited overrides

Check if data valid.

Returns:
Type Description
Boolean Validation status