"required|url" This works great. But when a user se...">

Laurvel url validation umlauts

I want to check urls in laravel. My rules contain

"url" => "required|url" 

This works great. But when a user sends an url with umlauts, validation will always fail.

Scarves such as öäü etc. are valid in German Domains. Is there a way in Laravel to accept these characters in urls?

+6
source share
2 answers

Laravel uses filter_var() with the FILTER_VALIADTE_URL option, which does not allow umlauts. You can write a special validator or use the regex validation rule in combination with a regular expression. I'm sure you will find here here

 "url" => "required|regex:".$regex 

Or it is better to specify the rules as an array to avoid problems with special characters:

 "url" => array("required", "regex:".$regex) 

Or, as @michael points out, just replace umlauts before checking. Just make sure you keep the real sweat:

 $input = Input::all(); $validationInput = $input; $validationInput['url'] = str_replace(['ä','ö','ü'], ['ae','oe','ue'], $validationInput['url']); $validator = Validator::make( $validationInput, $rules ); if($validator->passes()){ Model::create($input); // don't use the manipulated $validationInput! } 
+5
source

Thanks to @michael and @lukasgeiter for showing me the right path. I decided to post my solution if anyone has the same problem.

I created a special validator like:

  Validator::extend('german_url', function($attribute, $value, $parameters) { $url = str_replace(["ä","ö","ü"], ["ae", "oe", "ue"], $value); return filter_var($url, FILTER_VALIDATE_URL); }); 

My rules contain:

 "url" => "required|german_url, 

Also do not forget to add the rule to the validation.php file

  "german_url" => ":attribute is not a valid URL", 
+4
source

Source: https://habr.com/ru/post/982400/


All Articles