Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Validation laravel验证多个模型_Validation_Laravel_Laravel 4 - Fatal编程技术网

Validation laravel验证多个模型

Validation laravel验证多个模型,validation,laravel,laravel-4,Validation,Laravel,Laravel 4,我想要一个解决这类问题的最佳实践 对于多对多关系,我有项目、类别和类别项目表 我有两个带有这些验证规则的模型 class Category extends Basemodel { public static $rules = array( 'name' => 'required|min:2|max:255' ); .... class Item extends BaseModel { public static $rules = arra

我想要一个解决这类问题的最佳实践

对于多对多关系,我有
项目
类别
类别项目

我有两个带有这些验证规则的模型

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);
    }
}
我不知道如何从只有类别、标题和内容字段的一个表单中验证这两组规则

目前,我只是对该项目进行了验证,但我不知道最好做什么:

  • 在我的控制器->中创建一组新规则,但这似乎是多余的
  • 按顺序验证项目,然后按类别->但我不知道如何处理验证错误,我必须合并它们吗?怎么做
  • 我不知道的第三种解决方案
这是我的ItemsController@store方法

/**
 * 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();
    }
}
我对这个问题的一些线索很感兴趣


谢谢

正如您自己指出的,一种方法是按顺序验证它:

/**
 * 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();
    }
}
另一种方法是创建类似项目存储库(域)的东西来编排项目和类别(模型),并使用验证服务(您也需要创建)来验证表单

这本书非常好地解释了这一点。

您也可以使用:

$validationMessages = 
    array_merge_recursive(
        $itemValidation->messages()->toArray(), 
        $categoryValidation->messages()->toArray());

return Redirect::back()->withErrors($validationMessages)->withInput();

用同样的方式来称呼它。

谢谢Antonio,你的第一个解决方案正是我想要的,但正如你提到的存储库解决方案,这绝对是通往goDid的方法。你尝试过其他laravel书籍吗?你为什么选择这本书?不仅仅是因为我把它翻译成了我的母语,这是一本很棒的书,里面有真实世界的例子和你在当前项目中可以实际使用的东西。Taylor的书,也是一本很棒的书,给了我们一些关于坚实原则和IoC容器的重要信息,但它主要是关于Laravel编码哲学的。代码Bright很好,但它是为Laravel的初学者准备的。还有Jeffrey Way的书,关于测试,非常适合学习TDD,但不是关于Laravel。
$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;