规则验证动态数组

时间:2018-04-07 15:52:37

标签: php laravel

我有一个details.blade.php,其中有一个表单供用户选择每种票证类型需要的票数:

<ul>
    @foreach($tickets as $ticket)
        <li>
            <span>{{$ticket->title}}</span> 
            <form method="post" action="{{route('congress.registration', ['id' => $congress->id, 'slug' => $congress->slug])}}">

                <select name=types[{{ $ticket->title }}]>
                    <option selected>0</option>
                    ...
                </select>
            </form>
            <span>X {{$ticket->showPrice()}}</span>
        </li>
    @endforeach
    <li>
        <span>TOTAL</span>
        <span>0.00€</span>
    </li>
    <input type="submit" value="Next"/>
    </form>

当用户点击“Next”时,RegistrationController中的storeQuantity()方法接收数据并存储在数组中。

我的疑问是如何在RegistrationController中验证用户在将其存储到数组之前引入的数量?

规则上下文是:每种故障单类型在数据库中都有一列minPerUser和一个maxPerUser列。例如,TicketType表具有名称为“完全访问”的票证类型,其中minPerUser值为“1”,maxPerUser值为“4”。因此,在选择菜单中,用户只能为票证类型“完全访问”选择1到4之间的值。

因此,如果用户在源代码中引入了数量为“100”的故障单类型“完全访问”,则验证应该会导致错误。

您知道如何为此上下文设置验证规则吗?

这个RegistrationController

class RegistrationController extends Controller

   public function storeQuantity(Request $request){

        //dd($request->all());

         $this->validate($request, [
             // how to validate here the quantities by the user?
        ]);

        $typeQuantities = $request->get('types');

        $total = 0;
        foreach($typeQuantities as $typeName => $quantity){
            $type = TicketType::where('name', $typeName)->firstOrFail();
            $price = $type->price;

            $selectedTypes[$type->name]['quantity'] = $quantity;
            $selectedTypes[$type->name]['price'] = $price;
            $selectedTypes[$type->name]['subtotal'] = $price * $quantity;

        }
        return view('congresses.registration')->with('selectedTypes', $selectedTypes);
    }
  }

“dd($ request-&gt; all());”所示:

   array:2 [▼
      "_token" => ""
      "types" => array:2 [▼
        "ticket type 1" => "2"
        "ticket type 2" => "1"
      ]
    ]

TicketType型号:

class TicketType extends Model
{
    protected $fillable = [
        'name', 'price', 'minperUser', 'maxPerUser','congress_id'
    ];
    public function congress(){
        return $this->belongsTo('App\Congress');
    }
}

1 个答案:

答案 0 :(得分:2)

xcodebuild "-exportArchive" "-archivePath" "archive.xcarchive" -exportPath . -exportOptionsPlist ./export_options.plist

}

这样的测试不会对此有所帮助..