php - How to set custom validation rules and error message in laravel 5 -
i'm having address field in form,by using address given fetch lat & long
, save db..
if user enter's wrong address lat & long
null..
so here need add new validator rule field..
here code,
i tried this..but don't know how this..
public function getlatlan( $address = '') { $formattedaddr = str_replace(' ','+',$address); $geocodefromaddr = file_get_contents('http://maps.googleapis.com/maps/api/geocode/json?address='.$formattedaddr.'&sensor=false'); $output = json_decode($geocodefromaddr); $data['latitude'] = $output->results[0]->geometry->location->lat; $data['longitude'] = $output->results[0]->geometry->location->lng; return $data; } function postsave( request $request) { $rules = $this->validateform(); $address_validate = array('location' =>'enter valid address'); if($_request['location'] != ''){ $data = $this->getlatlan($_request['location']); } if($data['latitude'] == '' || $data['longitude'] == ''){ array_merge($rules,$address_validate); } $validator = validator::make($request->all(), $rules); if ($validator->passes()) { .. } }
how should this... me..
thank you,
you've need create request validation class new form request.
- create new request class
app/requests/maprequest.php
,
namespace app\http\requests; use app\http\requests\request; class maprequest extends request { public function rules() { return [ 'lat' => 'required|min:2', 'long' => 'required', ]; } public function messages() { return [ 'lat.required' => 'required message', 'lat.min' => 'minimum value message', 'long.required' => 'required message' ]; } }
use map request class controller method: example, mapcontroller:
public function store(maprequest $request) { // process logic here! }
Comments
Post a Comment