-
Notifications
You must be signed in to change notification settings - Fork 0
Required
Majid Ahmaditabar edited this page Dec 12, 2021
·
13 revisions
- The field under validation must be present in the input data and not empty. A field is considered "empty" if one of
the following conditions are true:
- The value is null.
- The value is an empty string.
data = {
"name_1": "Majid",
"name_2": "",
"name_3": None,
}
rules = {
"name_1": ["required"],
"name_2": ["required"],
"name_3": ["required"],
}
validate = PyValidations.make(data, rules)
and result is:
{
'failed': True,
"errors": {
'name_2': ['The name_2 field is required.'],
'name_3': ['The name_3 field is required.']
}
}
- required_if:anotherfield
- The field under validation must be present and not empty if the anotherfield field is exist and equal to any value.
data = {
"first_name": "Majid",
"last_name": "Ahmaditabar", #--> if first_name exist and has value last_name field is required and will validate
}
rules = {
"first_name": ["nullable", "alpha"],
"last_name": ["required_if:first_name", "alpha"],
}
validate = PyValidations.make(data, rules)
- required_unless:anotherfield
- The field under validation must be present and not empty unless the anotherfield is Not Exist or be null or empty or "" value.
data = {
"email": "",
"phone": "123345665", #--> if email not exist or has null or "" value the phone field is required
}
rules = {
"email": ["nullable", "email"],
"phone": ["required_unless:email", "numeric"],
}
validate = PyValidations.make(data, rules)
- required_with:foo,bar,...
- The field under validation must be present and not empty only if all of the other specified fields are present and not empty.
data = {
"first_name": "",
"last_name": "Ahmaditabar", #-->required if first_name is exist and not empty
"age": "33", #-->required if first_name,last_name are exist and not empty
}
rules = {
"first_name": ["nullable", "alpha"],
"last_name": ["required_if:first_name", "alpha"],
"age": ["required_with:first_name,last_name", "numeric"],
}
validate = PyValidations.make(data, rules)
- required_without:foo,bar,...
- The field under validation must be present and not empty only when all of the other specified fields are empty or not present.
data = {
"email": "",
"phone": "",
"user_name": "John", #--> if email and phone not exist or have null or "" value the user_name field is required
}
rules = {
"email": ["nullable", "email"],
"phone": ["nullable", "numeric"],
"user_name": ["required_without:email,phone", "string"],
}
validate = PyValidations.make(data, rules)