I am trying to achieve certain laravel validation criteria which I am having hard time to implement. Please help me in the same. I am using laravel 9 & PHP 8.1
Description:
I have an API which has below mentioned inputs:
a. company_name - required
b. company_number - required as per condition
c. gst_number - required as per condition
d. county - required
e. state - required as per condition
f. source - required
//Scenario #1 - Small intro into Field Condition
where 'company_number' and 'gst_number' are 2 parameter which change from:
a. where both are not required at all
b. where either one of the field being required
c. to one of the either field being mandatory
based on source value entered.
//Scenario #1 - Detailed intro into Field Condition
a. If its source 1 then neither of them is required
b. If its source 2 then either one of them is required but not both & if both are present then it should throw validation error
c. If its any other source value then it should be company_number only and if other one is present then it show validation error
d. Unwanted field should throw validation error
//Scenario #2 - Country State Validation
a. There are 3 countries i.e. x,y,z in which state field is required else not & it should throw validation error if someone mentions it when not required
b. It should validate if the state mentioned belongs to that country or not. I have that state array list of those 3 countries
c. Here country & state are of 2 character in length which is ISO format, thus it should be of 2 digit only & in upper case only.
d. It should properly validate in both the cases of where the state is required & not required when state field is mentioned with blank value.
Now I know the Scenario #2, point 'c' can be done via regex but my main point is how to achieve all this with the all scenario points mentioned such that if all the points of both the scenarios fail then it should that many validation errors together.
//Example #1:
company_name: //blank
company_number: 123
gst_number: 456 //This field is not required as per source value, thus it should throw validation error
country: AB //Country in which state is not required, thus state not required validation should be triggered
state: cde //This should trigger uppercase only validation & 2 char only validation
source: 3
//Intended output #1:
{
"status": false,
"status_code": 400,
"message": "Request validation error.",
"company_name": [
[
"Please enter company name."
]
],
"gst_number": [
[
"gst_number is not required in this case"
]
],
"state": [
[
"State is not required with this country value",
"State should be of 2 characters only",
"State should be in capital letters only"
]
]
}
//Example #2:
company_name: ABC
company_number: //Blank
gst_number: //Blank
country: AB //Valid
state: CD //Valid
source: 2
//Intended output #2:
{
"status": false,
"status_code": 400,
"message": "Request validation error.",
"company_name": [
[
"Please enter either company name or gst_number"
]
],
"gst_number": [
[
"Please enter either company name or gst_number"
]
],
"state": [
[
"State is not required with this country value",
"State should be of 2 characters only",
"State should be in capital letters only"
]
]
}
//My Problem:
- I am facing problem implementing country state mapping validation including its length & casing check. Is it valid country & state or not? Is it the country which can have state value?
- Enabling, disabling the company number, gst_number validation depending upon source value.
//My current validation rule:
$rule = [
'company_name' => ['required'],
'country' => ['required', 'string', 'regex:/^[A-Z]{2}+$/', 'exists:table_name,field_name'],
'company_number'=> ['required_without:gst_number'],
'gst_number' => ['required_without:company_number', 'regex:/^([0-9]{2}[a-zA-z]{5}[0-9]{4}[a-zA-z]{1}[0-9]{1}Z{1}[0-9A-Za-z]{1})$/'],
'state' => ['sometimes','regex:/^[A-Z]{2}+$/'],
'source' => ['required', 'numeric', 'exists:table_name,field_name'],
];
If someone is suggesting custom validation, I know the commands & how to get started but as I have never used it, it will be helpful if someone gives me example of using it with respective the validation criteria mentioned in this post.