Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which validation rule to use for a float with Laravel 4?

I have a form with two text inputs that must be floats (specifically, these text inputs are for geographic coordinates), looked at the documentation and found a rule for integer and numeric but not for float.

I was thinking using the "numeric" because the text inputs are disabled and the value only changes when a marker on a map is moved.

What would be the best way to validate a float?

like image 357
Isaias Avatar asked Mar 05 '14 03:03

Isaias


People also ask

What does -> Validate do Laravel?

It validates the incoming data. By default, base controller class uses a ValidatesRequests trait which provides a convenient method to validate incoming HTTP requests with a variety of powerful validation rules.

How many types of validation are there in Laravel?

Each form request generated by Laravel has two methods: authorize and rules .

What methods should you implement for your custom validator Laravel?

You should add all your validation logic in the passes() function. It should return true or false based on the logic you have written in the function. The message() function returns a string that specifies the error message to be displayed in case the validation fails.

How do I create a custom validation rule in Laravel?

Custom Validation Rule Using Closures $validator = Validator::make($request->post(),[ 'birth_year'=>[ 'required', function($attribute, $value, $fail){ if($value >= 1990 && $value <= date('Y')){ $fail("The :attribute must be between 1990 to ". date('Y').". "); } } ] ]);


1 Answers

You may use a regular expression rule (regex:pattern) for this and since you want to use to validate Geographic Coordinates then you should use a regular expression rule because a Geo Coord may look something like 23.710085, 90.406966, which is the coordinates (lat long) of Dhaka Bangladesh and it also may contain a coordinates like -33.805789,151.002060. So here is the syntax:

$rules = array('form_field_name' => 'required|regex:pattern' );

Or maybe just

$rules = array('form_field_name' => 'regex:pattern' );

So, the pattern should be something like this /^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$/. So, finally, it should look something like this (pattern is copied from internet):

$rules = array('form_field_name' => 'regex:/^[+-]?\d+\.\d+, ?[+-]?\d+\.\d+$/');

Check the Laravel Validation (regex).

like image 119
The Alpha Avatar answered Sep 20 '22 15:09

The Alpha