【问题标题】:Validation error message not showing and datas not being saved to database验证错误消息未显示且数据未保存到数据库
【发布时间】:2015-10-02 16:17:28
【问题描述】:

我正在尝试首先验证输入,然后将值添加到数据库,但我既看不到验证错误消息,也无法将记录插入数据库。我在数据库中有一个名为“新闻”的表,我有

这是我的 boxed.blade.php

<form class="form-horizontal" role="form" method="post" action="/addNews" novalidate>
                                  <div class="form-group">
                                      <label for="inputEmail1" class="col-lg-2 col-sm-2 control-label">Title</label>
                                      <div class="col-lg-10">
                                          <input type="text" name="title" class="form-control" id="inputEmail1" placeholder="News Title" value="{{ Input::old('title') }}">

                                      </div>
                                  </div>

                                  <div class="form-group">
                                      <label for="inputPassword1" class="col-lg-2 col-sm-2 control-label">Description</label>
                                      <div class="col-lg-10">
                                          <textarea name="description" class="form-control" rows="6">{{ Input::old('description') }}</textarea>
                                      </div>
                                  </div>

                                   <div class="form-group">
                                       <label for="inputEmail1" class="col-lg-2 col-sm-2 control-label">Reporter</label>
                                           <div class="col-lg-10">
                                              <input type="text" name="reported_by" class="form-control" id="inputEmail1" placeholder="Reporter Name" value="{{ Input::old('reported_by') }}">
                                            </div>
                                    </div>

                                      <div class="form-group">
                                        <div class="col-lg-offset-2 col-lg-10">
                                            <div class="checkbox">
                                                 <label>
                                                      <input type="checkbox" name="status"> Status
                                                 </label>
                                            </div>
                                        </div>
                                      </div>

                                  <div class="form-group">
                                      <div class="col-lg-offset-2 col-lg-10">
                                          <button type="submit" class="btn btn-danger"><i class="fa fa-comments"></i> Add To List</button>
                                      </div>
                                  </div>
                              </form>

还有routes.php

  Route::get('addNews', function()
        {
          return View::make('pages.boxed');
        }
    );

    Route::post('addNews', function()
        {
            //processing the form
            $rules = array(
                'title' => 'required',
                'description' => 'required|min:50',
                'reported_by' => 'required'
            );

            $validator = Validator::make(Input::all(), $rules);

            if ($validator->fails()){
                $message = $validator->message();
                return Redirect::to('addNews')->withErrors($validator)->withInput(Input::all());
            }

            else{
                $news = new News;
                $news->title = Input::get('title');
                $news->description = Input::get('description');
                $news->reported_by = Input::get('reported_by');
                $news->status = Input::get('status');

                $news->save();
                return Redirect::to('addNews');
            }
        }

这是我的模型 News.php

 <?php

    namespace App;

    use Illuminate\Database\Eloquent\Model;

    class News extends Model
    {
        protected $fillable = array('title', 'description', 'reported_by', 'status');
    }

【问题讨论】:

    标签: php mysql forms routes laravel-5


    【解决方案1】:

    如果您想查看错误,请将以下代码放在表单上方:

    @if (count($errors) > 0)
        <div class="alert alert-danger">
            <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
            <strong>Whoops!</strong> There were some problems with your input.<br><br>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
    @endif
    

    更新 1: 不要在 routes.php 文件中定义方法,而是在名为 NewsController 的控制器中定义它

    因此,为此,请将您的 routes.php 文件更新为:

    Route::get('/addNews', 'getNewsForm');
    Route::post('/addNews', 'postNewsForm');
    

    NewsController.php

    /**
     * Display the add news form to the user.
     *
     * @return view
     */
    public function getNewsForm()
    {
        return View::make('pages.boxed');
    }
    
    /**
     * Store the input in the database after validation
     *
     * @param $request Illuminate\Http\Facade\Request
     */
    public function postNewsForm(Request $request)
    {
        $this->validate($request, [
            'title'       => 'required',
            'description' => 'required|min:50',
            'reported_by' => 'required'
        ]);
    
        News::create($request->all());
    
        return redirect('/addNews');
    }
    

    【讨论】:

    • 我已经将它放在了我的表单上方,但我仍然一无所获。
    • 你能告诉我dd(Input:all()); 在你的post 方法中route 的输出吗?
    • 我已经更新了答案,请看一下。
    • 我根据你的回答更新了我的代码,但同样的问题仍然存在。
    猜你喜欢
    • 2019-12-02
    • 2020-06-20
    • 1970-01-01
    • 2022-06-20
    • 1970-01-01
    • 1970-01-01
    • 2011-04-10
    • 2018-06-15
    • 2017-01-06
    相关资源
    最近更新 更多