php - Validation error message not showing and datas not being saved to database -
i trying first validate inputs , add values database neither able see validation error message nor able insert records database. have table in database named 'news' have
this 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 list</button> </div> </div> </form>
and routes.php
route::get('addnews', function() { return view::make('pages.boxed'); } ); route::post('addnews', function() { //processing 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'); } }
this model news.php
<?php namespace app; use illuminate\database\eloquent\model; class news extends model { protected $fillable = array('title', 'description', 'reported_by', 'status'); }
if want see error, place following code above form:
@if (count($errors) > 0) <div class="alert alert-danger"> <button type="button" class="close" data-dismiss="alert" aria-hidden="true">×</button> <strong>whoops!</strong> there problems input.<br><br> <ul> @foreach ($errors->all() $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif
update 1: instead of defining methods in routes.php
file, define in controller called newscontroller
so, this, update routes.php
file this:
route::get('/addnews', 'getnewsform'); route::post('/addnews', 'postnewsform');
newscontroller.php
/** * display add news form user. * * @return view */ public function getnewsform() { return view::make('pages.boxed'); } /** * store input in 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'); }
Comments
Post a Comment