4

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

我有一个多对多关系的itemscategoriescategory_item

我有 2 个具有这些验证规则的模型

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();
    }
}

我对这个主题的一些线索非常感兴趣

谢谢

4

3 回答 3

10

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

/**
 * 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();
    }
}

另一种方法是创建类似于项目存储库(域)的东西来编排您的项目和类别(模型)并使用验证服务(您也需要创建)来验证您的表单。

Chris Fidao 的书,Implementing Laravel 很好地解释了这一点。

于 2013-10-02T19:23:56.633 回答
0
$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;
于 2014-10-30T08:46:47.077 回答
0

你也可以使用这个:

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

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

并以同样的方式调用它。

于 2014-06-29T13:24:41.473 回答