Search code examples
phplaravellaravel-validation

laravel url validation umlauts


I want to validate urls in laravel. My rules contain

"url" => "required|url"

This is working great. But when a user submits an url with umlauts, the rule check will always fail.

Chars like öäü etc.. are valid in German Domains. Is there a way in Laravel to accept these chars in urls?


Solution

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