Required Rules

Required rules determine whether a field is mandatory or optional. The pre-built required rules are as follows:

required

The required rule makes a field absolutely mandatory. Non existence of the field will throw an error.

Usage:

  • { foo: 'required' }
  • { foo: { required: [] } }

Placeholders:

  • :attribute
  • :value

requiredIf

The requiredIf Rule applies if another field exists and has a value equal to the one mentioned in the argument. You can have multiple field value pairs in the arguments.

Usage:

  • { foo: 'requiredIf:bar,barbaz ...' }
  • { foo: { requiredIf: ['bar','barbaz'...] } }

In the above example if the value of the field bar == 'barbaz', the foo field is mandatory.

Placeholders:

  • :attribute
  • :value

requiredNotIf

The requiredNotIf Rule applies if another field exists and has a value not equal to the one mentioned in the argument. You can have multiple field value pairs in the arguments.

Usage:

  • { foo: 'requiredNotIf:bar,barbaz ...' }
  • { foo: { requiredNotIf: ['bar','barbaz'...] } }

In the above example if the value of the field bar != 'barbaz', the foo field is mandatory.

Placeholders:

  • :attribute
  • :value

requiredWith

The requiredWith Rule is applied if either of the fields mentioned in the arguments is part of the same request. You can add multiple fields to the arguments

Usage:

  • { foo: 'requiredWith:bar,barbaz ...' }
  • { foo: { requiredWith: ['bar','barbaz'...] } }

In the above case, the foo field will be a mandatory field only if either bar or barbaz fields are part of the request input.

Placeholders:

  • :attribute
  • :value

requiredWithout

The requiredWithout Rule is applied if either of the fields mentioned in the arguments is not part of the same request. You can add multiple fields to the arguments

Usage:

  • { foo: 'requiredWithout:bar,barbaz ...' }
  • { foo: { requiredWithout: ['bar','barbaz'...] } }

In the above case, the foo field will be a mandatory field if either foo or bar fields are not part of the request.

Placeholders:

  • :attribute
  • :value

requiredWithAll

The requiredWith Rule is applied if all of the fields mentioned in the arguments is part of the same request. You can add multiple fields to the arguments

Usage:

  • { foo: 'requiredWith:bar,barbaz ...' }
  • { foo: { requiredWith: ['bar','barbaz'...] } }

In the above case, the foo field will be a mandatory field only if both bar and barbaz fields are part of the request input.

Placeholders:

  • :attribute
  • :value

requiredWithoutAll

The requiredWithout Rule is applied if all of the fields mentioned in the arguments is not part of the same request. You can add multiple fields to the arguments

Usage:

  • { foo: 'requiredWithout:bar,barbaz ...' }
  • { foo: { requiredWithout: ['bar','barbaz'...] } }

In the above case, the foo field will be a mandatory field if both foo and bar fields are not part of the request.

Placeholders:

  • :attribute
  • :value