ElxValidation.Required (elx_validation v0.1.1)
Required
- 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 = %{
first_name: "Majid"
}
rules = [
%{
field: "first_name",
validate: ["required"]
}
]
rules defined by "name" but data defined by "first name"
it returns error: "The name is not exist"
data = %{
name: ""
}
rules = [
%{
field: "name",
validate: ["required"]
}
]
rules defined "required" so null , empty string not excepted
it returns error: "The name field is required."
Required If
- 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: "Ahmadi" ---> if first_name exist and has value last_name field is required
}
rules = [
%{
field: "first_name",
validate: ["nullable", "string" , "max:50"]
},
%{
field: "last_name",
validate: ["required_if:first_name"]
}
]
Required Unless
- 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: "email@example.com",
phone: "+123456789" ---> if email not exist or has null or "" value the phone field is required
}
rules = [
%{
field: "email",
validate: ["nullable" , "email"]
},
%{
field: "phone",
validate: ["required_unless:email"]
}
]
Required with
- required_with:foo,bar,...
- The field under validation must be present and not empty only if any of the other specified fields are present and not empty.
data = %{
first_name: "John",
last_name: "doe", --> required if first_name defined and has any value
full_name: "required_with:first_name,last_name" -->required if first_name,last_name is exist or not empty
}
rules = [
%{
field: "first_name",
validate: ["required"]
},
%{
field: "last_name",
validate: ["required_if:first_name"]
},
%{
field: "full_name",
validate: ["required_with:first_name,last_name"]
}
]
Required Without
- required_without:foo,bar,...
- The field under validation must be present and not empty only when any of the other specified fields are empty or not present.
data = %{
first_name: "John",
last_name: "doe", --> required if first_name defined and has any value
full_name: "required_without:first_name,last_name" -->required if first_name,last_name is/are not exist or empty
}
rules = [
%{
field: "first_name",
validate: ["required"]
},
%{
field: "last_name",
validate: ["required_if:first_name"]
},
%{
field: "full_name",
validate: ["required_with:first_name,last_name"]
}
]
Link to this section Summary
Link to this section Functions
Link to this function
is_require?(arg1)
Link to this function
required_if(req_field, all_data, value)
Link to this function
required_unless(req_field, all_data, value)
Link to this function
required_with(req_fields, all_data, value)
Link to this function