laravel validate multiple models

匿名 (未验证) 提交于 2019-12-03 08:48:34

问题:

I would like a best practice for this kind of problem

I have items, categories and category_item table for a many to many relationship

I have 2 models with these validations rules

class Category extends Basemodel {      public static $rules = array(         'name'   => 'required|min:2|max:255'     ); ....   class Item extends BaseModel {      public static $rules = array(         'title'   => 'required|min:5|max:255',         'content' => 'required'     ); ....   class Basemodel extends Eloquent{      public static function validate($data){         return Validator::make($data, static::$rules);     } } 

I don't know how to validate these 2 sets of rules from only one form with category, title and content fields.

For the moment I just have a validation for the item but I don't know what's the best to do:

  • create a new set of rules in my controller -> but it seems redundant
  • sequentially validate Item then category -> but I don't know how to handle validations errors, do I have to merges them? and how?
  • a 3rd solution I'm unaware of

here is my ItemsController@store method

/**  * Store a newly created item in storage.  *  * @return Redirect  */ public function store() {     $validation= Item::validate(Input::all());     if($validation->passes()){         $new_recipe = new Item();         $new_recipe->title    = Input::get('title');         $new_recipe->content = Input::get('content');         $new_recipe->creator_id = Auth::user()->id;         $new_recipe->save();          return Redirect::route('home')             ->with('message','your item has been added');     }     else{         return Redirect::route('items.create')->withErrors($validation)->withInput();     } } 

I am very interested on some clue about this subject

thanks

回答1:

One way, as you pointed yourself, is to validate it sequentially:

/**  * Store a newly created item in storage.  *  * @return Redirect  */ public function store() {     $itemValidation = Item::validate(Input::all());     $categoryValidation = Category::validate(Input::all());      if($itemValidation->passes() and $categoryValidation->passes()){         $new_recipe = new Item();         $new_recipe->title    = Input::get('title');         $new_recipe->content = Input::get('content');         $new_recipe->creator_id = Auth::user()->id;         $new_recipe->save();          return Redirect::route('home')             ->with('message','your item has been added');     }     else{         return Redirect::route('items.create')             ->with('errors', array_merge_recursive(                                     $itemValidation->messages()->toArray(),                                      $categoryValidation->messages()->toArray()                             )                 )             ->withInput();     } } 

The other way would be to create something like an Item Repository (domain) to orchestrate your items and categories (models) and use a Validation Service (that you'll need to create too) to validate your forms.

Chris Fidao book, Implementing Laravel, explains that wonderfully.



回答2:

You can also use this:

$validationMessages =      array_merge_recursive(         $itemValidation->messages()->toArray(),          $categoryValidation->messages()->toArray());  return Redirect::back()->withErrors($validationMessages)->withInput(); 

and call it in the same way.



回答3:

$validateUser = Validator::make(Input::all(), User::$rules); $validateRole = Validator::make(Input::all(), Role::$rules);  if ($validateUser->fails() OR $validateRole->fails()) :     $validationMessages = array_merge_recursive($validateUser->messages()->toArray(), $validateRole->messages()->toArray());      return Redirect::back()->withErrors($validationMessages)->withInput();  endif; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!