lundi 21 décembre 2015

Nested array validation laravel

I am building a REST based API, where one of the API is having the following request

{
   "categories_id" :"1",
   "product_name" : "Pen",
   "product_description" : "this is pen",
   "tags" : "pen,write",
   "image_count" : "4",
   "skus": 
      {
          "is_shippable":"n",
          "actual_price":"100.55", 
          "selling_price":"200.45",
          "quantity_type":"bucket",
          "quantity_total":"10",
          "bucket_value":"instock",
          "sort_order":"1"
      }
}

These are my validation rules

protected $rules = [
        ValidatorInterface::RULE_CREATE => [
        'users_id' => 'required',
        'user_profiles_id' => 'required',
        'categories_id' => 'required',
        'product_name' => 'required|max:100',
        'product_description' => 'required|max:1000',
        'tags' => 'required',
        'image_count'=>'required|integer',
        'creation_mode'=>'required|integer',
        'skus.is_shippable'=>'in:y,n',
        'skus.actual_price'=>'http://regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
        'skus.selling_price' => 'http://regex:/^\s*(?=.*[1-9])\d*(?:\.\d{1,2})?\s*$/',
        'skus.quantity_type' => 'sometimes|required|in:finite,infinite,bucket',
        'skus.quantity_total' => 'integer|required_if:skus.quantity_type,finite', 
        'skus.bucket_value'=>'in:instock,soldout,limited|required_if:skus.quantity_type,bucket',
        'skus.sort_order'=> 'required|integer'
        ],
        ValidatorInterface::RULE_UPDATE => [
        ]
    ];

The above request is properly getting validated. But The skus can have multiple entities inside like below request

{
       "categories_id" :"1",
       "product_name" : "Pen",
       "product_description" : "this is pen",
       "tags" : "pen,write",
       "image_count" : "4",
       "skus": 
          [{
              "is_shippable":"n",
              "actual_price":"100.55", 
              "selling_price":"200.45",
              "quantity_type":"bucket",
              "quantity_total":"10",
              "bucket_value":"instock",
              "sort_order":"1"
          },
          {
              "is_shippable":"n",
              "actual_price":"100.55", 
              "selling_price":"200.45",
              "quantity_type":"bucket",
              "quantity_total":"10",
              "bucket_value":"instock",
              "sort_order":"1"
          }]
    }

How do I validate if there are multiple nested entities?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire