laravel url validation umlauts laravel url validation umlauts php php

laravel url validation umlauts


Laravel uses filter_var() with the FILTER_VALIADTE_URL option which doesn't allow umlauts. You can write a custom validator or use the regex validation rule in combination with a regular expression. I'm sure you'll find one here

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

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

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

Or, as @michael points out, simply replace the umlauts before validating. Just make sure you save the real one afterwards:

$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!}


Thanks @michael and @lukasgeiter for pointing me to the right way. I have decided to post my solution, in case someone has the same issue.

I have created a custom 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 now:

"url" => "required|german_url,

Also don't forget to add the rule to your validation.php file

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