I can't validate array in json. - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5) +--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24) +--- Thread: I can't validate array in json. (/showthread.php?tid=79787) |
I can't validate array in json. - reinaldo07 - 07-26-2021 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 = [ JSON: Code: { I pass the json normally. PHP Code: $json = $this->request->getJSON(true); It does not validate only on array items, for example: in the "id" field, if only one field is correct, it passes validation. RE: I can't validate array in json. - paliz - 07-27-2021 Which veraion you are using I dont have any problem with json request RE: I can't validate array in json. - reinaldo07 - 07-27-2021 4.1.3 RE: I can't validate array in json. - reinaldo07 - 07-27-2021 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 RE: I can't validate array in json. - InsiteFX - 07-27-2021 You need to json_decode the json array to an associated array first. RE: I can't validate array in json. - paliz - 07-27-2021 Use post man for test your api https://www.postman.com/ RE: I can't validate array in json. - reinaldo07 - 07-27-2021 (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. RE: I can't validate array in json. - includebeer - 07-31-2021 (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 = false, int $depth = 512, int $options = 0) |