如何在laravel 5中设置自定义验证规则和错误消息

时间:2016-08-08 06:38:29

标签: php laravel laravel-5

我的表单中有一个地址字段,使用给定的lat & long地址,并将其保存到数据库中。

如果用户输入错误的地址,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()) { .. } } 将为空..

所以在这里我需要为该字段添加一个新的验证器规则..

这是我的代码,

我试过这样的事情......但是不知道怎么做......

cursor.execute("INSERT INTO Staff (ID,Name)VALUES (%d,%s)",(1001,"MR Tan"))
cursor.execute("INSERT INTO Qualification VALUES (%d,%s,%s)",(2001,"Professor","select ID from Staff where ID=1001"))

我该怎么办...... 有人可以帮帮我..

谢谢,

1 个答案:

答案 0 :(得分:3)

您需要创建请求验证类新表单请求。

  1. 创建新请求类
  2. { "key": "ctrl+shift+u", "command": "extension.changeCase.upper", "when": "editorTextFocus" }, { "key": "ctrl+u", "command": "extension.changeCase.lower", "when": "editorTextFocus" },

    App/Requests/MapRequest.php
    1. 将Map Request类用于控制器方法:例如,MapController:

      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'
              ];
          }
      }