将两组错误传递给页面laravel undefined变量

时间:2014-10-06 07:38:43

标签: php laravel laravel-4

如何将重定向上的两种不同类型的$ errors集合传递回同一页面。我遇到的问题是使用variationErrors变量,错误是未定义变量:我在刀片视图页面上发生了变异错误

控制器:

    $variations = Input::get("variations");
    $variationErrors = [];
    // dd($variations);
    foreach ($variations as $id) {

        $data = [
            'producttype' => Input::get('producttype_' . $id),
            'price' => Input::get('price_' . $id),
            'quantity' => Input::get('quantity_' . $id),
            'discount' => Input::get('discount_' . $id)
        ];

        $validationVariations = Validator::make(
            $data,
            ProductVariations::$rules,
            ProductVariations::$rulesMessages
        );

        if($validationVariations->fails()) {
            $variationsHasErrors = true;

            $variationErrors[$id] = $validationVariations->messages()->toArray();

        }
    }

    if($validationProduct->fails() OR $variationsHasErrors) {           
        // problem here passing $variationErrors on blade page undefined $variationErrors
        return Redirect::back()->withErrors($validationProduct, $variationErrors)->withInput(); // ->with(compact('productsVariations'));
    }

页:

{{ var_dump($variationErrors) }}

1 个答案:

答案 0 :(得分:0)

请试试。

$variations = Input::get("variations");
$variationErrors = [];
// dd($variations);
foreach ($variations as $id) {

    $data = [
        'producttype' => Input::get('producttype_' . $id),
        'price' => Input::get('price_' . $id),
        'quantity' => Input::get('quantity_' . $id),
        'discount' => Input::get('discount_' . $id)
    ];

    $validationVariations = Validator::make(
        $data,
        ProductVariations::$rules,
        ProductVariations::$rulesMessages
    );

    if($validationVariations->fails()) {
        $variationsHasErrors = true;

        $variationErrors[] = $validationVariations->messages()->toArray();

    }
}

if(!empty($variationErrors)) {           
    // problem here passing $variationErrors on blade page undefined $variationErrors
    return Redirect::back()->withErrors($variationErrors); // ->with(compact('productsVariations'));
}