I would like to get the best practice for this problem.
I have a table items, categoriesand category_itemfor many to many
I have 2 models with these validation 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 do not know how to check these two sets of rules from only one form with the fields of category, title and content.
At the moment, I only have a check for the item, but I do not know what is best to do:
- create a new set of rules in my controller -> but it seems superfluous.
- check the element sequentially and then the category →, but I don’t know how to handle verification errors, do I need to merge them? And How?
- third solution I don't know about
here is my ItemsController @store method
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 to know some question on this subject.
thanks