Alumuko
Alumuko

Reputation: 191

vee-validate 3.x. validate "required value if only another value is null"

vee-validate 3.x.

I want to validate "required value if only another value is null".

Refereing "Cross Field Validation", I tried to create a such a validator.

        validate: function (value, another) {
            return another.value != null || value != null;
        }

but the validate function is not called when value is null.

how to create such a validator? or how to do that the function is called when value is null?

Thanks for reading my poor English.

Upvotes: 0

Views: 3849

Answers (2)

The Anh Nguyen
The Anh Nguyen

Reputation: 861

For anyone is using ValidationProvider

fieldModel1 will be required if fieldModel2 is null or empty string

<ValidationProvider
  v-slot="{ errors }"
  vid="fieldModel1"
  :rules="{
    required: !fieldModel2 || fieldModel2.length === 0,
  }"
  name="Your field name"
>

Upvotes: 0

Alumuko
Alumuko

Reputation: 191

Using bootstrap-vue 2.x, I sovled the problem by myself.

 <validation-provider name="REQUIRE_ONE"
    :rules='{ required: another == null}' v-slot='v-slot-1'>

Upvotes: 2

Related Questions