在laravel 5.0中提交表单后验证失败时重定向到错误的操作

时间:2017-10-29 09:00:53

标签: php forms laravel validation

当我将文本字段留空时,我在一个视图中有两个表单,它会重定向到错误的操作。我正在寻找转发这个问题的解决方案。谢谢你。 当我在输入字段中输入数据时,表单1正常工作。 / inquiry / store还包含http请求验证。

查看包含两种表单的代码。

private func performVisionRequest(pixelBuffer: CVPixelBuffer){
    let visionModel = try! VNCoreMLModel(for: self.iFaceModel.model)
    let visionModel2 = try! VNCoreMLModel(for: self.ageModel.model)
    let request = VNCoreMLRequest(model: visionModel){ request, error in

        if error != nil {
            return
        }

        guard let observations = request.results else {
            return
        }

        let observation = observations.first as! VNClassificationObservation

        print("Name \(observation.identifier) and confidence is \(observation.confidence)")


        DispatchQueue.main.async {
            if observation.confidence.isLess(than: 0.04) { 
                self.displayPredictions(text: "Not recognized")
                print("Hidden")
            }else {
                self.displayPredictions(text: observation.identifier)
            }
        }
    }

搜索学生的第二种形式。当我提交第一个表单而没有一些空字段时,它会重定向到/ inquiry / search_stu。并显示MethodNotAllowedHttpException。

 <div class="col-lg-6">

        {!! Form::open(['role' => 'form', 'url' => '/inquiry/store', 'class' => 'form-horizontal', 'method'=>'POST']) !!}
        <div class='form-group'>
            {!! Form::label('name', 'Student Name *', ['class' => 'control-label col-md-4']) !!}
            <div class="col-md-8">
                {!! Form::text('name', $student->name,['autocomplete'=>'off' , 'placeholder' => 'Student Name', 'class' => 'form-control']) !!}
            </div>
        </div>
        <div class='form-group'>
            {!! Form::label('father_name', 'Father Name *', ['class' => 'control-label col-md-4']) !!}
            <div class="col-md-8">
                {!! Form::text('father_name',$student->father_name, ['autocomplete'=>'off' , 'placeholder' => 'Father Name', 'class' => 'form-control']) !!}
            </div>
        </div>
        <div class="form-group">
            {!! Form::label('class', 'Class', ['class' => 'control-label col-md-4']) !!}
            <div class="col-md-8">
                {!! Form::text('class', $student->admission_class,['autocomplete'=>'off' , 'placeholder' => 'Class', 'class' => 'form-control']) !!}
            </div>
        </div>
        <div class="form-group">
            {!! Form::label('roll_no', 'Roll Number', ['class' => 'control-label col-md-4']) !!}
            <div class="col-md-8">
                {!! Form::text('roll_no', $student->roll_no,['autocomplete'=>'off' , 'placeholder' => 'Roll Number', 'class' => 'form-control']) !!}
            </div>
        </div>
        <div class="form-group">
            {!! Form::label('date', 'Date',['class' => 'control-label col-md-4']) !!}
            <div class="col-md-8 date">
                <div class="input-group input-append date" id="dateRangePicker">
                    {!! Form::input('date', 'date', null, ['autocomplete'=>'off' , 'placeholder' => 'Date of Birth', 'class'=>'form-control col-height datepicker']) !!}
                    <span class="input-group-addon add-on"><span class="glyphicon glyphicon-calendar"></span></span>
                </div>
            </div>
        </div>
        <div class="form-group">
            {!! Form::label('teacher_name', 'Teacher Name', ['class' => 'control-label col-md-4']) !!}
            <div class="col-md-8">
                {!! Form::select('teacher_name', $teacher, ['autocomplete'=>'off' , 'placeholder' => 'Teacher Name', 'class' => 'form-control']) !!}
            </div>
        </div>
        <div class="form-group">
            {!! Form::label('remarks', 'Remarks', ['class' => 'control-label col-md-4']) !!}
            <div class="col-md-8">
                {!! Form::textarea('remarks', null, ['autocomplete'=>'off' ,
                'placeholder' => 'Remarks',
                'class' => 'form-control']) !!}
            </div>
        </div>
        <div class="form-group">
            <div class="col-xs-offset-3 col-md-12">
                {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
                <input type="reset" class="btn btn-default" value="Reset">
            </div>
        </div>
        {!! Form::close() !!}

搜索学生的控制器功能。

<form role="form" id="search_form" action="/inquiry/search_stu" method="post"  class="form-inline">
            <div class='form-group'>
                <label class="control-label col-md-4">Search Student *</label>
                <input class="form-control" type="text" name="search" placeholder="Admission No" required>
            </div>
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
            <div class='form-group'>
                <input type="submit"  form="search_form" class="btn btn-primary" name="submit">
                <input type="reset" class="btn btn-default">
            </div>

        </form>

当我提交商店表单时,它会重定向到搜索学生。

存储功能。

 public function search_student(Request $request)
{
    $teacher = \App\Teacher::lists('name', 'name');
   $adminid = $request['search'];
    $student = Admission::where('admission_no',$adminid)->first();
    return View::make('/inquiry/create', ['student'=> $student,'teacher' => $teacher]);
}

想要存储数据时的Http请求验证

public function store(Requests\StoreInquiryRequest $request) {

    $input = Input::all();
    $inquiry = new Inquiry();
    $inquiry->name = $input['name'];
    $inquiry->father_name = $input['father_name'];
    $inquiry->date = $input['date'];
    $inquiry->class = $input['class'];        
    $inquiry->teacher_name = $input['teacher_name'];
    $inquiry->roll_no = $input['roll_no'];
    $inquiry->remarks = $input['remarks'];        

    try {
        $inquiry->save();
        return redirect()->to('inquiry')->with('message', 'success| Student details have been saved.');
    } catch (Exception $ex) {
        \Log::error($ex);
        return redirect()->to('inquiry')->with('message', 'error| There was an error adding new student, please try again later.');
    }
}

1 个答案:

答案 0 :(得分:0)

您可以根据需要在一个页面上执行尽可能多的表单。据我所知,你的控制器应该有适当的验证 - 对每个表格进行不同的验证。

您需要使用正确的Laravel验证。阅读此内容可使您的编码过程更轻松,更快捷:https://laravel.com/docs/5.0/validation

Laravel 5.0中控制器的验证示例:

public function store(Request $request)
{
    $this->validate($request, [
        'title' => 'required|unique|max:255',
        'body' => 'required',
    ]);
}

Laravel Valiation还允许您通过查询数据库qithout编写整个SQL查询来执行验证:https://laravel.com/docs/5.0/validation#available-validation-rules

要在视图中显示错误:

@if($errors->has())
    @foreach ($errors->all() as $error)
        <div>{{ $error }}</div>
    @endforeach
@endif
祝你好运!