Skip to content

Instantly share code, notes, and snippets.

@stankusl
Created November 16, 2015 10:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stankusl/17ee799b51b86af707e8 to your computer and use it in GitHub Desktop.
Save stankusl/17ee799b51b86af707e8 to your computer and use it in GitHub Desktop.
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
use Illuminate\Support\Facades\Route;
class OfficeRequests extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
\Validator::extend('longitude', function($attribute, $value, $parameters)
{
return preg_match('/^[-]?((((1[0-7][0-9])|([0-9]?[0-9]))\.(\d+))|180(\.0+)?)$/', $value);
}, 'The :attribute is not valid.');
\Validator::extend('latitude', function($attribute, $value, $parameters)
{
return preg_match('/^[-]?(([0-8]?[0-9])\.(\d+))|(90(\.0+)?)$/', $value);
}, 'The :attribute is not valid.');
if( Route::getCurrentRoute()->getPath() == 'office/create' )
{
return [
'name' => 'required',
'address1' => 'required',
'city' => 'required',
'postcode' => 'required',
'telephone' => 'required',
'longitude' => 'required|longitude',
'latitude' => 'required|latitude',
'region' => 'required'
];
}
if( Route::getCurrentRoute()->getPath() == 'office/edit/{id}' )
{
return [
'name' => 'required',
'address1' => 'required',
'city' => 'required',
'postcode' => 'required',
'telephone' => 'required',
'longitude' => 'required|longitude',
'latitude' => 'required|latitude',
'region' => 'required'
];
}
}
}
@stankusl
Copy link
Author

Regex longitude and latitude validation class for Laravel 5.0+.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment