Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel validation rule "URL" does not pass empty value

I have input text field whith validation rule :

public function rules()
{
    return [
        'field' => 'url',
    ];
}

It`s not required (field can be empty) but validation return an error.

like image 577
Odin Thunder Avatar asked Mar 21 '17 10:03

Odin Thunder


People also ask

How to validate a website URL in Laravel?

Here we provides some validation rules which helps you to validate URL. You can validate a website URL in laravel using url rule in validate () method.

How to pass required parameter in a Laravel route?

to get in this way need to be uri?id=2 ,isn't it? With Laravel, if you need to pass a required parameter in your route, you need to specify it into your routes/web.php file, and get it from your controller action.

How to generate a temporary signed route url in Laravel?

If you would like to generate a temporary signed route URL that expires after a specified amount of time, you may use the temporarySignedRoute method. When Laravel validates a temporary signed route URL, it will ensure that the expiration timestamp that is encoded into the signed URL has not elapsed:

How does the route Helper Work in Laravel?

The route helper will automatically extract the model's route key: Laravel allows you to easily create "signed" URLs to named routes. These URLs have a "signature" hash appended to the query string which allows Laravel to verify that the URL has not been modified since it was created.


Video Answer


2 Answers

Solve problem use "nullable":

public function rules()
{
    return [
        'field' => 'nullable|url',
    ];
}
like image 137
Odin Thunder Avatar answered Sep 28 '22 11:09

Odin Thunder


when we submmitting values from js, like through a FormData, the value null could be submitted as string containing 'null', which may pass a nullable rule, cause further type check fail. so be sure to make this value be posted as '', literaly nothing, no a 'null' string

like image 25
pilishen Avatar answered Sep 28 '22 10:09

pilishen