以下是我的产品视图中的代码:
{{ Form::open(array('url'=>'admin/products/create' , 'files'=>true)) }}
<p>
{{ Form::label('category_id' , 'Category') }}
{{ Form::select('category_id' , $categories) }}
</p>
<p>
{{ Form::label('title') }}
{{ Form::text('title') }}
</p>
<p>
{{ Form::label('description') }}
{{ Form::textarea('description') }}
</p>
<p>
{{ Form::label('height') }}
{{ Form::text('height' , null , array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('width') }}
{{ Form::text('width' , null , array('class'=>'form-price')) }}
<!--{{ Form::label('image' , 'Choose an image') }}
{{ Form::file('image') }}-->
</p>
<p>
{{ Form::label('length') }}
{{ Form::text('length' , null , array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('color') }}
{{ Form::text('color' , null , array('class'=>'form-price')) }}
</p>
<p>
{{ Form::label('material') }}
{{ Form::text('material' , null , array('class'=>'form-price')) }}
</p>
{{ Form::submit('Create Product' , array('class'=>'secondary-cart-btn')) }}
{{ Form::close() }}
及以下是我的产品控制器的代码:
<?php
/**
*
*/
class ProductsController extends BaseController {
public function __construct() {
$this->beforeFilter('csrf' , array('on'=>'post'));
}
public function getIndex() {
/* NOTE :: the categories array is being
created so that the products index page
can have access to all the availability
categories */
$categories = array();
foreach (Category::all() as $category) {
$categories[$category->id] = $category->name;
}
return View::make('products.index')
->with('products' , Product::all())
->with('categories' , $categories);
}
public function postCreate() {
$validator = Validator::make(Input::all() , Product::$rules);
if($validator->passes()) {
$product = new Product;
$product->category_id = Input::get('category_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->height = Input::get('height');
$product->width = Input::get('width');
$product->length = Input::get('length');
$product->color = Input::get('color');
$product->material = Input::get('material');
/* code for saving the image */
/* $image = Input::file('image');
$filename = date('Y-m-d-H-i-s')."-".$image->getClientOriginalName();
$path = public_path('img/products/' .$filename);
Image::make($image->getRealPath())->resize(468, 249)->save($path);
$product->image = 'img/products/'.$filename; */
/*end of code for saving the image */
$product->save();
return Redirect::to('admin/products/index')
->with('message' , 'Product created');
}
return Redirect::to('admin/products/index')
->with('message' , 'something went wrong')
->withError($validator)
->withInput();
}
public function postDestroy() {
$product = Product::find(Input::get('id'));
if ($product) {
$product->delete();
return Redirect::to('admin/products/index')
->with('message' , 'product has been deleted');
}
return Redirect::to('admin/products/index')
->with('message' , 'something went wrong , Please try again');
}
}
?>
现在,当我填写表单中的值并单击“提交”时,我会收到如下错误:
序列化'关闭不允许',在谷歌上搜索了一下我发现了以下有用的帖子:
接受的解决方案不适用于我,我在第二个答案中尝试了解决方案,我仍然有同样的错误。
我仍然不明白这个错误可能是什么原因或我做错了什么?谁能解释一下?
答案 0 :(得分:1)
除非您发布的代码中存在拼写错误,否则您发布的相关问题的已解决方案确实与您有关。
在您的退货声明中,您有->withError($validator)
,这应该是->withErrors($validator)
(您错过了's'。