Welcome Guest, Not a member yet? Register   Sign In
I can't validate array in json.
#1

(This post was last modified: 07-26-2021, 07:48 PM by reinaldo07.)

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.
Reply
#2

Which veraion you are using

I dont have any problem with json request
Enlightenment  Is  Freedom
Reply
#3

4.1.3
Reply
#4

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
Reply
#5

You need to json_decode the json array to an associated array first.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#6

Use post man for test your api

https://www.postman.com/
Enlightenment  Is  Freedom
Reply
#7

(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.
Reply
#8

(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);

CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply




Theme © iAndrew 2016 - Forum software by © MyBB