Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

pasamsin's avatar

how PHP validation convert to Laravel validation?

$request->validate([ 'city' => '??????????????' ]);

How can I do the validation process in my php code below with laravel validate?

function exist_city($post_array = ["country" => "US", "city" => "Angels","state" => "uncle"]){
    
    if($post_array['country'] == "US"){
        $database_query_result = [
            ["city" => "mardiv"],
            ["city" => "Angels"],
            ["city" => "sardan"],
        ];
        if(in_array($post_array['city'],$database_query_result)){
           return "its okay city";
        }else{
           return "its wrong city";
        }
    }else{
        if(strlen($post_array['city']) > 2){
            return "its okay city";
        }else{
            return "its wrong city";
        }
    }
}
0 likes
5 replies
pasamsin's avatar

I guess it does not happen, we cannot fulfill more than one condition for a field.

$request->validate([ 'country' => 'required|min:2|max:2|exists:country,country_code', 'city' => 'required_if:country,US|exists:city,name', ]);

For example, if the country code is US in the above code, city is required and is there a name in the database?

checks if there is a name in the database even if the country is not US?

pasamsin's avatar

I think I should do as follows?

        if($request->country == "US"){
            $request->validate([
                'city' => 'required_if:country,US|exists:city,name',
                'state' => 'required_if:country,US|exists:city,name',
            ]);
        }else{
            $request->validate([
                'city' => 'required|min:2|max:60|regex:/^(?=\S+(?:\s\S+)+$)[\p{Latin}ŞşÇçÖöÜüıİĞğ\s]{2,60}$/',
                'state' => 'required|min:2|max:60|regex:/^(?=\S+(?:\s\S+)+$)[\p{Latin}ŞşÇçÖöÜüıİĞğ\s]{2,60}$/',
            ]);
        }

Please or to participate in this conversation.