我在Laravel 5中有Product
模型和ProductController
。
但Validator错误不会返回。
我无法理解为什么会这样。
以下是已发布的表单字段:
<form class="form-horizontal" action="/imalatci/products/addproduct" enctype="multipart/form-data" method="post">
{!! csrf_field() !!}
<input type="hidden" name="imalatci_id" value="{{ Auth::user()->id }}"/>
<div class="form-group">
<label class="col-md-2 control-label">Ürün Başlığı</label>
<div class="col-md-8">
<input type="text" name="title" class="form-control" placeholder="Ürün başlığını giriniz.">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Ürün Açıklaması</label>
<div class="col-md-8">
<textarea class="form-control" rows="5" name="description" placeholder="Ürün hakkında detaylı bir açıklama yapınız." ></textarea>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Ürünün Fiyatı</label>
<div class="col-md-8">
<input type="text" name="price" class="form-control" placeholder="Ürününü fiyatını belirleyiniz (TL Olarak).">
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Ürünün Stok Durumu</label>
<div class="col-md-8">
<select class="form-control" name="avaliability">
<option value="1">Stokta Var</option>
<option value="0">Stokta Yok</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Ürün Kategorisi</label>
<div class="col-md-8">
<select class="form-control" name="subcategory" id="subcategory">
@foreach(App\Category::all() as $category)
<optgroup label="{{ $category->name }}">
@foreach(App\Category::find($category->id)->subcategories as $subcategory)
<option value="{{$subcategory->id}}">{{$subcategory->name}}</option>
@endforeach
</optgroup>
@endforeach
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Ürün Ana Resmi</label>
<div class="col-md-8">
<input id="product_main_img" name="image" type="file" class="file-loading" accept="image/*">
</div>
</div>
<!--
<div class="form-group">
<label class="col-md-2 control-label">Ürün Diğer Resimler</label>
<div class="col-md-8">
<input id="products_img" name="product_images[]" type="file" multiple=true class="file-loading" accept="image/*">
</div>
</div>
-->
<div id="kv-success-modal" class="modal fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Yükleme İşlemi Başarılı</h4>
</div>
<div id="kv-success-box" class="modal-body">
</div>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-8">
<button type="submit" class="btn btn-success btn-lg">Ürünü Ekle</button>
</div>
</div>
</form>
这是Model Class:
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
protected $table = "products";
protected $fillable = ['category_id','subcategory_id','imalatci_id','title','description','price','avaliability','image','verified','published'];
public static $rules = array(
'category_id' => 'required|integer',
'subcategory' => 'required|integer',
'imalatci_id' => 'required|integer',
'title' => 'required|min:2',
'description' => 'required|min:10',
'price' =>'required|numeric',
'avaliability' => 'required|integer',
'image' => 'required|image|mimes:jpeg,jpg,bmp,png,gif',
'verified' => 'required|integer',
'published' => 'required|integer'
);
/*
public static $messages = array(
'required' => ':attribute alanı boş bırakılamaz.',
'numeric' => ':attribute alanına sadece sayı girilebilir.',
'min' => ':attribute alanı en az :min karakter olmalıdır.',
'integer' => ':attribute alanı sadece sayı olabilir.',
'image' => 'Resim formatında yükleme yapılabilir.',
'mimes' => 'Resim olarak sadece jpeg,jpg,png,gif formatlarından birisi yüklenebilir.'
);
*/
public function category()
{
return $this->belongsTo('Category');
}
public function subcategory()
{
return $this->belongsTo('Subcategory');
}
public function imalatci()
{
return $this->belongsTo('User');
}
public function productImage(){
$this->hasMany('ProductImage');
}
}
这是我们的控制器类:
namespace App\Http\Controllers;
use App\Category;
use App\Product;
use App\ProductImage;
use App\SubCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Intervention\Image\Facades\Image;
use Validator;
class ProductsController extends Controller
{
public function getIndex()
{
$products = Product::all();
return view('pages.imalatci.products')
->with('products',$products);
}
public function postAddproduct()
{
$validator = Validator::make(Input::all(),Product::$rules);
if(!$validator->fails()){
$product = new Product();
$subcategory = SubCategory::find(Input::get('subcategory'));
$product->category_id = $subcategory->category_id;
$product->subcategory_id = Input::get('subcategory');
$product->imalatci_id = Input::get('imalatci_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('product_main_img');
$filename =Input::get('imalatci_id').'_'.date('Y_m_d_H_i_s').'_'.$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(250,280)->save('images/products/'.$filename);
$product->image = 'images/products/'.$filename;
$product->save();
flash()->success('Ürün başarıyla eklendi.Yönetici onayından sonra ürün yayınlanacaktır.');
return Redirect::to('/imalatci/products');
}
flash()->important('Hata Oluştu');
return Redirect::to('/imalatci/products')
->withErrors($validator)
->withInput();
}
}
如何解决这个问题?
答案 0 :(得分:1)
您可以使用watson/validating通过$ rules属性验证模型。
首先安装包
composer require "watson/validating"
在模型中使用特征
namespace App;
use Illuminate\Database\Eloquent\Model;
use Watson\Validating\ValidatingTrait;
class Product extends Model
{
use ValidatingTrait;
protected $table = "products";
protected $fillable = ['category_id','subcategory_id','imalatci_id','title','description','price','avaliability','image','verified','published'];
protected $rules = array(
'category_id' => 'required|integer',
'subcategory' => 'required|integer',
'imalatci_id' => 'required|integer',
'title' => 'required|min:2',
'description' => 'required|min:10',
'price' =>'required|numeric',
'avaliability' => 'required|integer',
'image' => 'required|image|mimes:jpeg,jpg,bmp,png,gif',
'verified' => 'required|integer',
'published' => 'required|integer'
);
/*
public static $messages = array(
'required' => ':attribute alanı boş bırakılamaz.',
'numeric' => ':attribute alanına sadece sayı girilebilir.',
'min' => ':attribute alanı en az :min karakter olmalıdır.',
'integer' => ':attribute alanı sadece sayı olabilir.',
'image' => 'Resim formatında yükleme yapılabilir.',
'mimes' => 'Resim olarak sadece jpeg,jpg,png,gif formatlarından birisi yüklenebilir.'
);
*/
public function category()
{
return $this->belongsTo('Category');
}
public function subcategory()
{
return $this->belongsTo('Subcategory');
}
public function imalatci()
{
return $this->belongsTo('User');
}
public function productImage(){
$this->hasMany('ProductImage');
}
}
在您的控制器类中,您可以使用非常简单的代码来验证规则
namespace App\Http\Controllers;
use App\Category;
use App\Product;
use App\ProductImage;
use App\SubCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Intervention\Image\Facades\Image;
class ProductsController extends Controller
{
public function getIndex()
{
$products = Product::all();
return view('pages.imalatci.products')
->with('products', $products);
}
public function postAddproduct()
{
$product = new Product();
$subcategory = SubCategory::find(Input::get('subcategory'));
$product->category_id = $subcategory->category_id;
$product->subcategory_id = Input::get('subcategory');
$product->imalatci_id = Input::get('imalatci_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('product_main_img');
$filename = Input::get('imalatci_id') . '_' . date('Y_m_d_H_i_s') . '_' . $image->getClientOriginalName();
Image::make($image->getRealPath())->resize(250, 280)->save('images/products/' . $filename);
$product->image = 'images/products/' . $filename;
if (!$product->save()) {
flash()->important('Hata Oluştu');
return Redirect::to('/imalatci/products')
->withErrors($product->getErrors())
->withInput();
} else {
flash()->success('Ürün başarıyla eklendi.Yönetici onayından sonra ürün yayınlanacaktır.');
return Redirect::to('/imalatci/products');
}
}
}
答案 1 :(得分:0)
您应该在控制器中定义rules
数组。尝试这样的事情:
namespace App\Http\Controllers;
use App\Category;
use App\Product;
use App\ProductImage;
use App\SubCategory;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Redirect;
use Intervention\Image\Facades\Image;
use Validator;
use Input;
class ProductsController extends Controller
{
protected $rules = array(
'category_id' => 'required|integer',
'subcategory' => 'required|integer',
'imalatci_id' => 'required|integer',
'title' => 'required|min:2',
'description' => 'required|min:10',
'price' =>'required|numeric',
'avaliability' => 'required|integer',
'image' => 'required|image|mimes:jpeg,jpg,bmp,png,gif',
'verified' => 'required|integer',
'published' => 'required|integer'
);
public function getIndex()
{
$products = Product::all();
return view('pages.imalatci.products')
->with('products',$products);
}
public function postAddproduct()
{
$validator = Validator::make(Input::all(),$this->rules);
if(!$validator->fails()){
$product = new Product();
$subcategory = SubCategory::find(Input::get('subcategory'));
$product->category_id = $subcategory->category_id;
$product->subcategory_id = Input::get('subcategory');
$product->imalatci_id = Input::get('imalatci_id');
$product->title = Input::get('title');
$product->description = Input::get('description');
$product->price = Input::get('price');
$image = Input::file('product_main_img');
$filename =Input::get('imalatci_id').'_'.date('Y_m_d_H_i_s').'_'.$image->getClientOriginalName();
Image::make($image->getRealPath())->resize(250,280)->save('images/products/'.$filename);
$product->image = 'images/products/'.$filename;
$product->save();
flash()->success('Ürün başarıyla eklendi.Yönetici onayından sonra ürün yayınlanacaktır.');
return Redirect::to('/imalatci/products');
}
flash()->important('Hata Oluştu');
return Redirect::to('/imalatci/products')
->withInput()
->withErrors($validator);
}
}