简体   繁体   中英

How to pass % in URL validation in Laravel 5.5?

I have the following route:

Route::get('/delete/{var}',
    'MyController@delete')
    ->where("var", "[A-Za-z0-9\-\.\%]+");

My problem is that I want to allow % to be passed with var . Unfortunately the following two options did not work out for me:

"[A-Za-z0-9\-\.\%]+"
"[A-Za-z0-9\-\.%]+"

I'm trying to pass the folliwng URL:

domain.tld/delete/something.com%2Fhtml%2Fsomestring%2Ffolder

If I remove the % from my URL it's working. So how could I validate the URL in order to use the % sign in it?

Edit

This is strange: If I remove the ->where() check in the route completely, it's still not working if there's a % in the URL. How could that be?

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.

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

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

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

For example

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM