【问题标题】:Laravel Form Validation on POST requestPOST 请求上的 Laravel 表单验证
【发布时间】:2017-10-23 17:40:33
【问题描述】:

我有一个分为两部分的表格。 当用户单击按钮时,表单的第一部分会显示“GET”请求。当用户填写此表单并单击下一步时,页面将通过“POST”请求重定向到表单的下一部分,该请求首先保存表单,然后显示下一部分。 问题是当我尝试验证表单的第二部分时,laravel 给出了错误提示方法不允许。

我的第一个控制器:

public function create(Request $request)
{

    $validator = Validator::make($request->all(), [
        'unit_code' => 'max:25',
        'unit_title' => 'max:255',
        'project_title' => 'max:255',
        'project_ref_number' => 'max:255',
        'storage_location' => 'required|max:255',
        'keeper_name' => 'required|max:255',
    ]);

    if ($validator->fails()) {
        return redirect()->back()
                    ->withErrors($validator)
                    ->withInput();
    }

    $notification = Notification::create([
        'unit_code'         =>$request->unit_code,
        'unit_title'        =>$request->unit_title,
        'project_title'     =>$request->project_title,
        'project_ref_number'=>$request->project_ref_number,
        'storage_location'  =>$request->storage_location,
        'keeper_name'       =>$request->keeper_name,
        'user_id'           =>Auth::user()->id
        ]);

    return view('Notification.notification_for_lmo')->with('notification', $notification);
    //return redirect()->route('show.material_List')->with('notification'); 

}

路线:

/*route to personal information form for notification*/
    Route::get('/personal_information_notification_form', 'HomeController@getNotificationForm');

    /*submit personal information and go to next part of the form*/
    Route::post('personal_information_notification_form/submit/', 'NotificationController@create')->name('submit.personal_info_for_notification');

    /*route to material list form for notification application*/
    Route::get('personal_information_notification_form/material_list/', 'NotificationController@showMaterialListForm')->name('show.material_List');

    /*submit the material list*/
    Route::post('/personal_information_notification_form/add_lmo/', 'NotificationTypeAController@create')->name('submit.add_lmo');

第二个控制器:

public function create(Request $request)
{
    /*this loop is because im adding rows dynamically to the table*/
    $count = count($request->input('item_name'));

    for ($i=0; $i<$count; $i++){

        $validator = Validator::make($request->all(), [
            'material_type' => 'required|max:25',
            'item_name' => 'required|max:255',
            'risk_level' => 'required|max:255',
            'quantity' => 'required|max:255',
            'volume' => 'required|max:255',
            'notification_id' => 'required|max:255',
        ]);

    }

    if ($validator->fails()) {
        return view('Notification.notification_for_lmo')->withErrors($validator)->withInput();
    }

    for ($i=0; $i<$count; $i++){

        $data  = NotificationTypeA::create([
            'material_type'  =>$request->material_type[$i],
            'item_name'      =>$request->item_name[$i],
            'risk_level'     =>$request->risk_level[$i],
            'quantity'       =>$request->quantity[$i],
            'volume'         =>$request->volume[$i],
            'notification_id'=>$request->notification_id
        ]);
    }



        $admin = Admin::find(1);
        $user = Auth::user();
        $notification = Notification::find($request->notification_id);
        $admin->notify(new NewNotificationApplicationSubmitted($user->name, $notification->id));

        return redirect()->route('show.go_to_notification')->with('message', 'We have notified '.$user->name.' that he/she is added to SSBC')->with('status', 'info');

    }

在第二个控制器中,有一个 for 循环,因为用户将数据填充到表格中。 (刀片文件很大,所以我不在这里发布。)

这是什么问题,为什么会这样?

第一把刀片:

<div class="well">
                    <form class="form-horizontal" role="form" method="POST" action="{{route('submit.personal_info_for_notification')}}">
                        {{ csrf_field() }}

                        <fieldset>
                            <legend>
                                SECTION 1 - Personal Details
                            </legend>
                            <div class="row">
                                <div class="col-xs-12">

                                    <div class="form-group">
                                        <div class="col-xs-12">
                                            {!! Form::label('unit_code', 'Unit Code (if teaching):', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('unit_code', $value = null, ['class' => 'form-control']) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('unit_title', 'Unit Title (if teaching):', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('unit_title', $value = null, ['class' => 'form-control']) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('project_title', 'Project Title (if FYP/research):', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('project_title', $value = null, ['class' => 'form-control']) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('project_ref_number', 'Ref. No (if FYP/research):', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('project_ref_number', $value = null, ['class' => 'form-control']) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('storage_location', 'Storage Location:', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('storage_location', $value = null, ['class' => 'form-control' ]) !!}
                                        </div>
                                        <div class="col-xs-12">
                                            {!! Form::label('keeper_name', 'Name of the Keeper:', ['class' => 'control-label']) !!} <br>
                                            {!! Form::text('keeper_name', $value = null, ['class' => 'form-control' ]) !!}
                                        </div>
                                    </div>

                                </div>
                            </div>
                        </fieldset>
                        <div class="row">
                            <div class="form-group">
                                <div class="col-md-12">
                                    <button type="submit" class="btn btn-primary btn-block">Next</button>
                                </div>
                            </div>
                        </div>
                    </form>
                </div>

第二把刀片:

<form class="form-horizontal" role="form" method="POST" action="{{ route('submit.add_lmo') }}">
                        {{ csrf_field() }}

                        <fieldset>
                            <div class="row">
                                <div class="col-xs-12">
                                    <div class="row">
                                        <legend>
                                            SECTION 2 – Details of the Biohazardous Materials
                                        </legend>
                                        <div class="col-xs-8">
                                            <h4>List of Living Modified Organism (LMO)</h4>
                                        </div>
                                        <div class="col-xs-4">
                                            <input type="checkbox"/>Applicable
                                        </div>
                                    </div>
                                    <div class="row">
                                        <div class="col-xs-12">
                                            <div id="LMOtablediv">
                                                <input type="button" id="addmoreLMObutton" value="Add" onclick="insRow(event)" />
                                                <table id="addLMOtable" border="1">
                                                    <thead>
                                                        <tr>
                                                            <td>No.</td>
                                                            <td>Notification ID</td>
                                                            <td>Material Type</td>
                                                            <td>Name</td>
                                                            <td>Risk Level</td>
                                                            <td>Quantity</td>
                                                            <td>Volume</td>

                                                        </tr>
                                                    </thead>
                                                    <tbody>
                                                        <tr>
                                                            <td>1</td>
                                                            <td><input type="text" name="notification_id" id="notification_id" value="{{$notification->id}}"></td>
                                                            <td>{!! Form::text('material_type[]', null, array('id'=>'material_type'))!!}</td>
                                                            <td>{!! Form::text('item_name[]', null, array('id'=>'item_name'))!!}</td>
                                                            <td>{!! Form::text('risk_level[]', null, array('id'=>'risk_level'))!!}</td>
                                                            <td>{!! Form::number('quantity[]', null, array('id'=>'quantity'))!!}</td>
                                                            <td>{!! Form::number('volume[]', null, array('id'=>'volume'))!!}</td>

                                                            <td><input type="button" id="delLMObutton" value="Delete" onclick="deleteRow(this)"/></td>
                                                        </tr>
                                                    </tbody>
                                                </table>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>

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

【问题讨论】:

  • 分享您的查看代码。
  • 按照您的要求添加了视图。
  • 我看过表格,动作和方法没有问题。你可以尝试运行'php artisan route:clear'和'php artisan view:clear'然后试一试。还可以尝试复制粘贴错误消息并提供更多信息。
  • 仍然显示相同的错误 Connection.php 第 647 行中的 QueryException:SQLSTATE[23000]:完整性约束违规:1048 列 'material_type' 不能为空(SQL:插入到 notification_type_asmaterial_type, item_name, risk_level, quantity, volume, notification_id, updated_at, created_at) 值 (, , , , , 24, 2017-05-24 01: 19, 2017-05-24 01:23:19))
  • 不知何故,它不会重定向回同一页面,而是直接转到出现 laravel 错误的帖子。

标签: laravel validation laravel-form


【解决方案1】:
  1. 验证规则有问题,你写错了。
  2. 由于尝试将空值保存在数据库中的 material_type 列而不是可空字段而引发异常。

要解决验证问题,请删除验证规则的循环并尝试以下操作:

$validator = Validator::make($request->all(), [
    'material_type'     => 'array',
    'material_type.*'   => 'required|max:25',
    'item_name'         => 'array',
    'item_name.*'       => 'required|max:255',
    'risk_level'        => 'array',
    'risk_level.*'      => 'required|max:255',
    'quantity'          => 'array',
    'quantity.*'        => 'required|max:255',
    'volume'            => 'array',
    'volume.*'          => 'required|max:255',
    'notification_id'   => 'array',
    'notification_id.*' => 'required|max:255',
]);

if ($validator->fails()) {
    return view('Notification.notification_for_lmo')->withErrors($validator)->withInput();
}

如果您确定所有输入都有值且没有空值,请确保属性存在于 Eloquent 模型的 $fillable 数组中。

【讨论】:

  • 如你所说尝试过,但我在 View.php 第 402 行得到 ErrorException: Undefined offset: 0 ERROR
  • 是的,这可能是因为数组值是动态生成的,所以你应该在你的视图中处理它你可以刷新一些输入以返回到视图,你可以通过删除调用来测试它withInput 函数
猜你喜欢
  • 2018-03-23
  • 2016-09-02
  • 2016-05-23
  • 2016-03-11
  • 2015-06-09
  • 1970-01-01
  • 2018-02-11
  • 2020-12-11
相关资源
最近更新 更多