0

这是我上一个问题的继续

我正在使用模型进行验证

我将通过这种方式将值发送到模型

$VehicleData = Input::all();
$validation  = Validator::make($VehicleData, VehicleModel::$rules);

并使用以下规则进行验证

public static $rules = array(
        'VehicleNumber' => 'required|unique:vehicle', 
        'NumberSeats' => 'required', 
);

但现在我想验证上传文件的大小和扩展名

我试图在模型中做到这一点SetFooAttribute

public function setInsurancePhotoAttribute($InsurancePhoto)
{
    if($InsurancePhoto && Input::file('InsurancePhoto')->getSize() < '1000')
    {
    $this->attributes['InsurancePhoto'] = Input::get('VehicleCode') . '-InsurancePhoto.' . Input::file('InsurancePhoto')->getClientOriginalExtension();
    Input::file('InsurancePhoto')->move('assets/uploads/vehicle/', Input::get('VehicleCode') . '-InsurancePhoto.' . Input::file('InsurancePhoto')->getClientOriginalExtension());
    }
}

但我意识到这是一种不好的做法。因为它只会在验证之后以及调用时调用VehicleModel::create($VehicleData);

我不知道在哪里进行文件验证以及如何在模型中进行。

请建议我如何进行。

4

1 回答 1

4

在模型中

public static $rules = array(
    'VehicleNumber'  => 'required|unique:vehicle', 
    'NumberSeats'    => 'required',
    'InsurancePhoto' => 'required|image|mimes:jpg,png,gif|max:2000'
);

在控制器中

$VehicleData = Input::all();
$validation  = Validator::make($VehicleData, VehicleModel::$rules);
    if($validation->passes())
    {
        // Input::file('InsurancePhoto') stuff
        // store the data to the db
    }
于 2014-12-25T11:06:58.450 回答