I'm having an address field in my form,by using the address given I fetch that lat & long for that and save it into DB..
If the user enter's wrong address the lat & long would be null..
So here I need to add a new validator rule for that field..
Here is my code,
I tried something like this..But Don't know how to do 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 I do this... Could someone help me..
Thank you,