I'm used to have model and form validation together, in another framework. But I'm migrating to Laravel and trying to understand its mindset.
What's the best approach for data validation? I've seen some classes that help out on creating forms and validating requests, but isn't it unsafe to have models saving data without validating it before?
How could I integrate form (frontend), request (backend) and model validation so they all play nicely together? Or this is not done in the Laravel world at all?
答案 0 :(得分:0)
As a starter in Laravel myself, I can tell a mind of a learner.
The first thing to understand is that Laravel is very very very very very abstract. It offers you thousands of solutions for a single problem. Since you're just starting out, I'm going to assume you're using Laravel 5 (5.1 to be more specific).
You can use $this->validate() in your controllers.
class SomeController extends Controller {
public function store(Request $request){
$this->validate($request, [
'title' => 'required|unique:posts|max:255',
'body' => 'required',
]);
// This passed validation.
}
}
Inside the config/app.php
you will find a aliases
field that defines the Validator Facade as 'Validator' => Illuminate\Support\Facades\Validator::class
. You can make validators from basically anywhere.
public function store(Request $request) {
$validator = Validator::make($request->all(), [
'email' => 'required|unique:emails|email',
]);
if ($validator->fails()) {
// Error logic
}
// Store the blog post...
}
Personally, I like Form Requests. They allow you to reuse validation logic defined once in any controller you feel like it. You can run in your project
php artisan make:request MyCustomRequest
That will generate a new request inside app/Http/Requests
where you can write your rules inside the rules method
. And then, when you want to use it, just type-hint your controller method.
Here is how to use it:
public function store(CompanyRequest $request){
// This code will only be executed if the rules inside CompanyRequest
// Are true.
}
Here is the file defining CompanyRequest.
class CompanyRequest extends Request {
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize() {
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules() {
return [
'name' => 'required|max:255',
'domain' => 'required|max:40'
];
}
}
There are probably a few more ways to do it. You can, for instance, use Validator::make
facade from within your Eloquent models. Laravel offers multiple ways of handling basic problems. You just have to find what is best for you and major in it.
答案 1 :(得分:0)
我不确定这是不是最好的方法。 但是我使用这个概念及其在大多数流行的CMS中最流行的验证表单的方式。
通常,表单操作应指向控制器方法。在控制器内部方法中,您可以启动一个类似下面的验证器类。
$validation = \Validator::make(\Input::all(), with(new UserValidation)->getRules());
if ($validation->fails()) {
return redirect()->route('your route path ')->withErrors($validation)->withInput();
}
然后在您的控制器中使用名称空间,如。
use VendorName\PackageName\validations\UserValidation;
这里我将验证放在一个名为validations的单独文件夹中。另请注意,我在Laravel 5.x中使用了Package开发概念,您可以阅读有关here的更多信息。
然后在该UserValidation类中,您可以放置所有验证规则。
class UserValidation {
public function getRules() {
return [
'name' => 'required|max:200|unique:client',
'address'=>'required',
'status' => 'required',
];
}
}
这有几个优点,控制器看起来很整洁,验证定制将完全是单独的文件。 您也可以将路线拆分为单独的文件夹。
希望它有意义......
答案 2 :(得分:0)