如何使用laravel检索post值并在控制器中进行验证?

时间:2017-04-06 04:19:56

标签: laravel-5

  

在我的路线档案中

Route::get('griev_reg_form', 'GrievanceRegisterController@show');

Route::post('griev_reg_form', 'GrievanceRegisterController@postdata');
  

并在控制器中

<?php

namespace App\Http\Controllers;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;


use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\GrievanceRegister;
use App\Department;
use Input;
use Validator;
use Redirect;
use Session;

class GrievanceRegisterController extends Controller
{
   public function show()
   {
  $departments = Department::orderBy('edesc','ASC')->get();
  return view('griev_reg_form', array('departments' => $departments));
}

public function postdata()
{

     $data = Input::all();
    // print_r($data);
    echo $name = Input::get('name');
        $rules = array(
            'name' => 'required',
            'email_address' => 'required|email',
            'g-recaptcha-response' => 'required|captcha',

        );
        $validator = Validator::make($data, $rules);
        if ($validator->fails()){
            echo "1";
            return Redirect::to('/griev_reg_form')->withInput()->withErrors($validator);
        }
        else{
            // Do your stuff.
        }
}

}

   ?>
  

并在视图文件中

{!! Form::open(array('url'=>'griev_reg_form','method'=>'POST', 'id'=>'myform')) !!}
                    <div class="box-body">              
                        <div class="form-group">
                                <label class="col-sm-3 control-label">Grievance Case:
                                </label>
                                 <label>
                                          <input type="radio" name="gretype" class="minimal" checked>
                                        </label>
                                        <label>Normal Case
                                        </label>
                                        <label>
                                          <input type="radio" name="gretype" class="minimal">
                                        </label>
                                        <label>
                                                  NRI
                                  </label>
                        </div>
                        <div class="form-group">
                                <label class="col-sm-3 control-label">Grievance/Demand/Suggestion/Others:
                                </label>
                                 <label>
                                          <input type="radio" name="sugg_demand" class="minimal" checked value="G">
                                        </label>
                                        <label>Grievance 
                                        </label>
                                        <label>
                                          <input type="radio" name="sugg_demand" class="minimal" value="S">
                                        </label>
                                        <label>
                                                 Suggestion
                                         </label>
                                         <label>
                                          <input type="radio" name="sugg_demand" class="minimal" 
                                          value="D">
                                        </label>
                                        <label>
                                               Demand
                                         </label>
                                         <label>
                                          <input type="radio" name="sugg_demand" 
                                          class="minimal" value="O">
                                        </label>
                                        <label>
                                                 Others 
                                         </label>
                        </div>
                        <div class="form-group">
                                <label class="col-sm-3 control-label">Department/Office:
                                </label>
                                  <select class="form-control select2" name="dept_name" style="width: 28%;">
                                  <option value="">Select Department</option>
                                  <?php
                                    foreach($departments as $result)
                                                    {
                                                        ?>
                                    <option value="<?php echo $result->deptcode; ?>"><?php echo $result->edesc; ?></option>
                                        <?php
                                        }
                                            ?>
                                </select>
                        </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">Mobile No:</label>

                         <div class="input-group">
                            <div class="input-group-addon">
                              <i class="fa fa-phone"></i>
                         </div>
                         <input type="text" class="form-control" name="mobileno">
                     </div>

                   </div>


   <div class="form-group">
                    <label class="col-sm-3 control-label">Alternative Mobile No:</label>

                     <div class="input-group">
                        <div class="input-group-addon">
                          <i class="fa fa-phone"></i>
                     </div>
                     <input type="text" class="form-control" name="amobileno">
                 </div>

               </div>
               <div class="form-group">
                    <label  class="col-sm-3 control-label">Received Date:</label>

                    <div class="input-group date">
                      <div class="input-group-addon">
                        <i class="fa fa-calendar"></i>
                      </div>
                      <input type="text" class="form-control" name="recvd_date" id="datepicker" readonly="">
                    </div>
                    <!-- /.input group -->
                  </div>
                    <div class="form-group">
                    <label class="col-sm-3 control-label">Name:</label>


                     <input type="text" class="form-control" name="cname">
                 </div>
                 <div class="form-group">
                            <label class="col-sm-3 control-label">Individual or Group Complainant(s): 
                            </label>
                             <label>
                                      <input type="radio" name="indiv_grp" value="I" class="minimal" checked>
                                    </label>
                                    <label>Individual
                                    </label>
                                    <label>
                                      <input type="radio" name="indiv_grp" value="G" class="minimal">
                                    </label>
                                    <label>Group
                              </label>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-3 control-label">AADHAR Card Number:</label>

                       <input type="text" class="form-control" name="idproofdetail">
                 </div>
                 <div class="form-group">
                      <label class="col-sm-3 control-label">Address:</label>

                       <input type="text" class="form-control" name="address1">
                 </div>
                  <div class="form-group">


                       <input type="text" class="form-control box-right" name="address2">
                 </div>
                  <div class="form-group">


                       <input type="text" class="form-control  box-right" name="address3">
                 </div>
                  <div class="form-group">
                          <label class="col-sm-3 control-label">Pin code:</label>


                       <input type="text" class="form-control" name="pincode">
                 </div>
                <div class="form-group">
                          <label class="col-sm-3 control-label">Email:</label>

                 <div class="input-group">
                    <span class="input-group-addon"><i class="fa fa-envelope"></i></span>
                    <input type="email" class="form-control" placeholder="Email" name="email_address">
                  </div>
                  </div>
                  <div class="form-group">
                            <label class="col-sm-3 control-label">District:
                            </label>
                              <select class="form-control select2" name="district_problem" style="width: 28%;">


                            </select>
                    </div>
                     <div class="form-group">
                            <label class="col-sm-3 control-label">विधानसभा निर्वाचन क्षेत्र  /Assembly Constituency:
                            </label>
                              <select class="form-control select2" name="ac_problem" style="width: 28%;">


                            </select>
                    </div>
                    <div class="form-group">
                            <label class="col-sm-3 control-label">Area:
                            </label>
                             <label>
                                      <input type="radio" value="urban" name="problem_area" class="minimal" checked>
                                    </label>
                                    <label>Urban
                                    </label>
                                    <label>
                                      <input type="radio" value="rural" name="problem_area" class="minimal">
                                    </label>
                                    <label>
                                            Rural
                              </label>
                    </div>
                    <div class="form-group">
                            <label class="col-sm-3 control-label">Town/Block:
                            </label>
                              <select class="form-control select2" name="city_problem" style="width: 28%;">


                            </select>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-3 control-label">Please Enter Specific Details about Your Grievance </label>
                      <textarea class="form-control" rows="3" name="Description" placeholder="Enter ..."></textarea>
                    </div>
                    <div class="form-group">
                      <label class="col-sm-3 control-label">What do you want?</label>
                      <textarea class="form-control" rows="3" name="remedies" placeholder="Enter ..."></textarea>
                    </div>
                    <div class="form-group">
                          <label class="col-sm-3 control-label">Upload(Relevant Document):</label>
                          <input type="file"  class="form-control">


                     </div>
                        <div class="form-group">
                <label class="col-sm-3 control-label">Captcha</label>
                <label class="col-sm-3 control-label">
                    {!! app('captcha')->display(); !!}
                </label>

            </div>
                <div class="form-group">
                <div class="col-md-6 col-md-offset-4">
                    <button type="submit" class="btn btn-primary">
                        Submit
                    </button>
                </div>
            </div>
        </form>
  

当我点击提交按钮而不填写任何数据时,它不会   在验证部分   请提供帮助我如何检查输入文件的post值以及   验证码并相应验证

1 个答案:

答案 0 :(得分:0)

我检查了你的视图..我没有找到任何名为“name”的输入字段,你有“cname”所以也许你的验证规则应该是:

$rules = array(
   'cname' => 'required',
   'email_address' => 'required|email',
   'g-recaptcha-response' => 'required|captcha'
);

如果您使用&gt; = 5.3版本,则应使用:

public function postdata(Request $request)
    {
        //validation
        $validationArray = [
            'cname' => 'required',
            'email_address' => 'required|email',
            'g-recaptcha-response' => 'required|captcha'
        ];
        $validator = Validator::make($request->all(),$validationArray);
        if ($validator->fails()) {
            $response = ['errors' => $validator->messages()->all()];
            return Response::json($response,200);
        }

        //here if validation is successful
   }

当然在控制器顶部添加:use Illuminate\Http\Request;