Make http/https optional in laravel validation - laravel-4

When I use laravel url validation rules like:-
array(
'name' => 'required|min:3',
'url' => 'required|url'
)
It rejects any url that does not have http:// or https://. However I would like to make it optional.

You can just ignore the url rule. Or you need to prefix your input.
Another way is to set an custom rule.

If you use url rule, http(s):// is mandatory, because the part that makes a URI a URL is the inclusion of the "access mechanism", which is what http:// or ftp:// is for.
You should consider custom rule perhaps or some logic that would add http:// to validated value if you detect no "access mechanism" part present.

Related

Generating URL's in Laravel

I am working on a side project on Laravel and I am a junior intern. Currently I need to generate an URL and send that to a specific email, where the user can view a pdf for a limited time and only once.
For now I am just working on the unique URL generation and would like some advice for generating them securely, following standards and for a limited time only.
Would love just some resources or advice.
I think what you are looking for is Signed Routes in Laravel.
Probably this help
https://laravel.com/docs/9.x/urls#signed-urls
Example:
First of all Before using signed URL you need chek that in your App/Http/Kernel you have next line uncommented
'signed' => \Illuminate\Routing\Middleware\ValidateSignature::class,
after that in web routes you have to add signed middleware
Route::get('pdf/{id}/{user}/{response}', PdfController::class)->name('pdf.generate')->**middleware('signed')**
You finally can use your signed url as
use \Illuminate\Support\Facades\URL;
URL::temporarySignedRoute('pdf.generate', now()->addHour(), [
'id' => 25,
'user' => 100,
'response' => 'yes'
]);
which create a signed url like
https://example.com/pdf/25/100/yes?expires=1521543365
&signature=d32f53ced4a781f287b612d21a3b7d3c38ebc5ae53951115bb9af4bc3f88a87a
Where signature hash include all params in route and avoid manipulation, and the best of all in one hour will expire forever.
Throwing an Illuminate\Routing\Exceptions\InvalidSignatureException.
when visited after expiring

Laravel validation not required but still comes back as if it is required

Having some slight issues with laravel validation rules. I.e. I have setup a form with a field name of 'url'. This url needs to be a URL but is not required.
So I have:
'url' => 'url',
In the validation rules, but it still comes back on submit that the URL is an invalid format. But I didn't fill it out and it isn't required.
Slightly confused here, anything I should look out for here?
https://laravel.com/docs/5.6/validation#a-note-on-optional-fields
By default, Laravel includes the TrimStrings and ConvertEmptyStringsToNull middleware in your application's global middleware stack. These middleware are listed in the stack by the App\Http\Kernel class. Because of this, you will often need to mark your "optional" request fields as nullable if you do not want the validator to consider null values as invalid.
So, this validation rule will do the trick:
'url' => ['nullable', 'url']
For that, I usually use nullable in the very beginning
'url' => 'nullable|url',

Laravel return true validation for incorrect urls

in laravel i want to check simple url such as :
http://www.google.com
http://google.com
www.google.com
http://www.google.com
http://www.google
unfortunately laravel validation code as
['redirect_url' => 'required|url']
return false for this urls:
www.google.com
and return true for http://www.google address, how to check correctly urls in laravel, this validation is not best validation for urls
You can use regex validation rule for URL validation
['redirect_url' => ['required','regex:/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i']]
Before validation check if string contains http:// or https:// with this code:
if(!(starts_with($url, "http://") || starts_with($url, "https://")) {
$url = "http://".$url;
}
Function starts_with() is helper function from Laravel. docs
The regex solution that Jay Dhameliya provided is probably the best one for your scenario but I just thought I would explain why the url validator doesn't work as you expected.
If you look at the illuminate/validation/Validator validateURL function it is expecting a the following to be specified as part of the URL.
protocol ( http/https/etc )
optional basic auth
a domain or ip address
optional port
a trailing / or nothing or / followed by something else
There doesn't seem to be any alternatives provided for this validator method or ways to say don't require the protocol. So once again a custom regex seems the best option. If you are planning on using it in more than one place you might want to consider creating your own Custom Validation Rule see the docs for details.

Validation for file types not working in laravel 4

After lots of search and no luck to found the solution.
I am validating a input file in laravel 4.2.*. Using the model validation rules but rules not working for me.
rules i am using in my model.php
'reqfile' => 'mimes:txt,pdf,doc,docx|max:20000'
and
'reqfile' => 'mimes:application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,text/plain,application/pdf|max:20000'
After applying these rules the validation errors generated but the errors also appear on the valid file selection.
Here's anybody tells me, where i am wrong to apply the rules.
Thanks in Advance.
For this validator rule to work you need to make sure that the value being validated for reqfile is an instance of:
Symfony\Component\HttpFoundation\File\File
or
Symfony\Component\HttpFoundation\File\UploadedFile
So if you're validating a form, reqfile must be a uploaded file. That means its value should come from Input::file(). So your validator should look something like this:
Validator::make(
// Value for reqfile
array('reqfile' => Input::file('reqfile')),
// Validator rule for reqfile
array('reqfile' => 'mimes:txt,pdf,doc,docx|max:20000')
);
The validation rule will actually try to guess the extension by extracting the mime type and compare that to the extension list passed into the rule. That means that you need to pass file extensions to the rule, not actual mime types, as the documentation clearly states:
The file under validation must have a MIME type corresponding to one of the listed extensions

cakephp url validation

One of my models has a 'url' field. I am using the default url validation rule on it. When trying to add a specific url it is not validated.
The url causing validation to fail is http://careers2.hiredesk.net/ViewJobs/JobDetail.asp?Comp=I3&TP_ID=1&PROJ_ID={BDA01FCD-5703-4D40-9197-CCF688633951}
The { character is causing the validation to fail. What workarounds do I have here?
Do you pass your given URL through a URL Encode Function first? See the linked page for an example.

Resources