C
C
codeschemer2017-01-20 10:30:06
Laravel
codeschemer, 2017-01-20 10:30:06

How to validate array values?

In my Request class, the rules look like this in short:

public function rules()
     {
           $rules = [
               'detail_id' => 'required|array',
           ];

           $detailId = $this->input('detail_id');
           foreach ($detailId as $index => $value){
               $rules["detail_id[".$index."]"] = 'json';
           }

           return $rules;
     }

The rules dump looks like this
array:21 [▼
  "detail_id" => "required|array"
  "detail_id[0]" => "json"
  "detail_id[1]" => "json"
  "detail_id[2]" => "json"
  "detail_id[3]" => "json"
  "detail_id[4]" => "json"
  "detail_id[5]" => "json"
]

Checking if detail_id is an array is done, but no value check is done. How to check?
Below is my research, which did not lead me to the goal, but maybe it will help you to help me.
In the search, I determined that rules () is called from the FormRequest class by the getValidatorInstance method.
Such a dump
dd($factory->make(
            $this->all(), $this->container->call([$this, 'rules']), $this->messages(), $this->attributes()
        ));

looks
Validator {#266 ▼
  #translator: Translator {#100 ▶}
  #presenceVerifier: DatabasePresenceVerifier {#101 ▶}
  #container: Application {#3 ▶}
  #failedRules: []
  #messages: null
  #data: array:5 [▼
    "_method" => "PATCH"
    "_token" => "yKG4oqNtBJmHIDGAYEwepoQZ13NVXsRPEWGV4oPJ"
    "detail_id" => array:6 [▼
      0 => "{"id":6,"base_detail_name":"\u30d7\u30ed\u30aa\u30da\u30ec\u30fc\u30bf\u30fc\uff08\u8ffd\u52a0\u5206\uff09","base_image1":"images\\base_detail\\no-image.jpg","base_image2":"images\\base_detail\\no-image.jpg","base_price":54665,"base_unit":"\u540d","deleted_at":null}"
      1 =>  "{id":...."deleted_at":null}"
      2 =>  "{id":...."deleted_at":null}"
      3 =>  "{id":...."deleted_at":null}"
      4 =>  "{id":...."deleted_at":null}"
      5 =>  "{id":...."deleted_at":null}"
    ]
  ]
  #files: []
  #rules: array:21 [▼
    "detail_id" => array:2 [▼
      0 => "required"
      1 => "array"
    ]
    "detail_id[0]" => array:1 [▼
      0 => "json"
    ]
    "detail_id[1]" => array:1 [▶]
    "detail_id[2]" => array:1 [▶]
    "detail_id[3]" => array:1 [▶]
    "detail_id[4]" => array:1 [▶]
    "detail_id[5]" => array:1 [▶]
  ]
  #after: []
  #customMessages: []
  #fallbackMessages: []
  #customAttributes: []
  #customValues: []
  #extensions: array:6 [▶]
  #replacers: []
  #sizeRules: array:4 [▶]
  #numericRules: array:2 [▶]
  #implicitRules: array:8 [▶]
}

If you need any more data, I'm all ears.
The dimensions of the arrays can sometimes not match, because in the rules () 3 arrays are checked, and I cleaned everything related to them.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Semenko, 2017-01-20
@abler98

$rules = [
    'detail_id' => 'required|array',
    'detail_id.*' => 'json',
];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question