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.

  1. 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'         ];     } } 
  1. use map request class controller method: example, mapcontroller:

    public function store(maprequest $request) {      // process logic here!  } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -