CodeIgniter Forums

Full Version: I can't validate array in json.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm not able to validate the array of items that I'm passing via json.

Here's how I'm trying:

Rules:
PHP Code:
public $createTransaction = [
        'api_key' => [
 
'label' => 'api-key',
 
'rules' => 'required'
 
],

        'date_expire' => [
 
'label' => 'Data Expiração',
 
'rules' => 'required|valid_date[Y-m-d H:m:s]'
 
],
        
        
'customer.name' => [
 
'label' => 'Nome',
 
'rules' => 'required'
 
],
        'customer.email' => [
 
'label' => 'Email',
 
'rules' => 'required|valid_email'
 
],
        'customer.external_id' => [
 
'label' => 'Id',
 
'rules' => 'required'
 
],

        'items.*.id' => [
 
'label' => 'Item Id',
 
'rules' => 'required'
 
],
        'items.*.name' => [
 
'label' => 'Nome do Item',
 
'rules' => 'required'
 
],
        'items.*.description' => [
 
'label' => 'Descrição do Item',
 
'rules' => 'required'
 
],
        'items.*.value' => [
 
'label' => 'Valor do Item',
 
'rules' => 'required'
 
],

        'amount' => [
 
'label' => 'Valor total',
 
'rules' => 'required|numeric'
 
],
    ]; 

JSON:
Code:
{
"api_key": "syPK1zJAUT",
"date_expire": "2021-07-27 00:01:00",
"customer":
{
"name": "Reinaldo",
"email": "[email protected]",
"external_id": "1"
},
"items":
[
{
"id": "1",
"name": "Notebook Dell",
"description": "Notebook Dell 4GB",
"value": "3000"
},
{
"id": "2",
"name": "Notebook Samsung",
"description": "Notebook Samsung 4GB",
"value": "3000"
}
],
"amount": "6000"
}

I pass the json normally.

PHP Code:
$json $this->request->getJSON(true);
$this->validation->run($jsoncreateTransaction); 

It does not validate only on array items, for example: in the "id" field, if only one field is correct, it passes validation.
Which veraion you are using

I dont have any problem with json request
4.1.3
I use the current version 4.1.3.Below is a json that is wrong, but passes validation:

{
"api_key": "syPK1zJAUT",
"date_expire": "2021-07-27 00:01:00",
"customer":
{
"name": "Reinaldo",
"email": "[email protected]",
"external_id": "1"
},
"items":
[
{
"id": "1",
"name": "Notebook Dell",
"description": "Notebook Dell 4GB",
"value": "3000"
},
{
"ii": "2",
"name": "Notebook Samsung",
"description": "Notebook Samsung 4GB",
"value": "3000"
}
],
"amount": "6000"
}

Note that the second product id is wrong, but in validation it does not affect any
You need to json_decode the json array to an associated array first.
Use post man for test your api

https://www.postman.com/
(07-27-2021, 05:03 AM)InsiteFX Wrote: [ -> ]You need to json_decode the json array to an associated array first.



I've tried it though when I give a "json_decode($json, true);" Gives typeerror.
(07-27-2021, 05:03 AM)InsiteFX Wrote: [ -> ]You need to json_decode the json array to an associated array first.
He get the data with getJSON(), so no need to call json_decode, it’s already done by getJSON().

PHP Code:
$json $this->request->getJSON(true); 

PHP Code:
public function getJSON(bool $assoc falseint $depth 512int $options 0)
{
    return json_decode($this->body$assoc$depth$options);