0

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:

  1. 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?
  2. 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.

  • What does *"required as per condition"* mean? – steven7mwesigwa Dec 11 '22 at 22:20
  • @steven7mwesigwa: If you read the Scenario #1 that's the condition for the mentioned field – Varun Krishnan Dec 12 '22 at 04:05
  • *b. It should validate if the state mentioned belongs to that country or not. I have that state array list of those 3 countries* How is that 'state array list' structured? – steven7mwesigwa Dec 12 '22 at 06:14
  • @steven7mwesigwa: It is structured as normal array like below: `$caStateArr = ['QC','PE','NS','NL','NB','NC']; $uaeStateArr = ['DU','AZ'];` currently i handle this validation separately using in_array – Varun Krishnan Dec 12 '22 at 06:30
  • What if the country is not in [x, y, z], how do you validate the submitted 'state' then? – steven7mwesigwa Dec 12 '22 at 06:45
  • Do you have a *'country'* or *'state'* database table by any chance? If so, could you share their DB schema? – steven7mwesigwa Dec 12 '22 at 06:48
  • @steven7mwesigwa: Currently I manage separately in php before the laravel validation is called. So using in_array I show the validation & in this case if it doesn't matches I give the final response & stop execution by saying please enter the proper state of if it is part of those where state is required, else it will say state is not required with this country. – Varun Krishnan Dec 12 '22 at 06:52
  • @steven7mwesigwa: Yes this can be done via DB, tables also. I have no issues in that. It should validate all these validation points via single laravel validation code. – Varun Krishnan Dec 12 '22 at 06:53
  • @steven7mwesigwa: Currently no table exists for this but you can take a respective country state map table where columns are id int(11), country char(2), state char(2) – Varun Krishnan Dec 12 '22 at 07:13
  • @steven7mwesigwa: Here I can go for `exists:table,column` but here I need to a way to tell the validation that check with provided country value like 'US' only. If 'CA' i.e. canada is passed then CA only. If I keep the each country & state table then half the problem is solved but now I will require to find a way to dynamically mention the table to the validation rule during run time, which adds complexity, thus I was looking to get it done via array itself. – Varun Krishnan Dec 12 '22 at 07:21

0 Answers0