-
flux1on Junior Member
 
-
Posts: 15
Threads: 7
Joined: Apr 2020
Reputation:
0
I have case user upload CSV/XLSX file in my website and convert to JSON. I want json request to insertBatch with Validation first
Example JSON :
Code: [
{
"params_id": "IP",
"params_name": "Inpatient",
"params_gender": "All",
"params_description": "Rawat Inap",
"params_created": 1
},
{
"params_id": "OP",
"params_name": "Outpatient",
"params_gender": "All",
"params_description": "Rawat Jalan",
"params_created": 1
}
]
My Controller :
PHP Code: public function createBatch() { $this->request->setHeader('Content-Type', 'application/json');
$validationRules = [ '*.params_id' => 'required|string|max_length[3]|is_unique[sm_benefit_group.benefitgroup_id]', '*.params_name' => 'required|string|max_length[100]', '*.params_gender' => 'required|validBenefitGroupGender', '*.params_description'=> 'required|string|max_length[255]', '*.params_created' => 'required|integer', ];
$validationMessages = [ '*.params_id' => [ 'required' => 'ID must be required', 'string' => 'ID must be string', 'max_length' => 'ID must be maximum 3 length', 'is_unique' => 'ID already exists, try another unique', ], '*.params_name' => [ 'required' => 'Name must be required', 'string' => 'Name must be string', 'max_length' => 'Name must be maximum 100 length', ], '*.params_gender' => [ 'required' => 'Gender Type must be required', 'validBenefitGroupGender' => 'Gender Type must be string and between ("All","M","F")', ], '*.params_description' => [ 'required' => 'Description must be required', 'string' => 'Description must be string', 'max_length' => 'Description must be maximum 255 length', ], '*.params_created' => [ 'required' => 'Created By must be required', 'integer' => 'Created By must be integer', ], ];
$this->validation->setRules($validationRules, $validationMessages);
if ($this->validation->withRequest($this->request)->run()) { // insertBatch here } else { $response = [ 'success' => false, 'status' => 400, 'messages' => 'Validation data benefit group failed!', 'validation' => $this->validation->getErrors(), ]; // Result return $this->respond($response, 400); } }
and want make array data to insertBatch like
PHP Code: $data = [ [ "benefitgroup_id" => "IP", "benefitgroup_name" => "Inpatient", "benefitgroup_gender" => "All", "benefitgroup_description" => "Rawat Inap", "benefitgroup_created_by" => 1, "benefitgroup_modified_by" => 1, ], [ "benefitgroup_id" => "OP", "benefitgroup_name" => "Outpatient", "benefitgroup_gender" => "All", "benefitgroup_description" => "Rawat Jalan", "benefitgroup_created_by" => 1, "benefitgroup_modified_by" => 1, ], ];
$result = $this->my_model->saveDataBatch($data);
-
flux1on Junior Member
 
-
Posts: 15
Threads: 7
Joined: Apr 2020
Reputation:
0
07-07-2022, 06:49 PM
(This post was last modified: 07-07-2022, 07:46 PM by flux1on.)
If example input reading file Excel with SheetJS
Result :
```
[
{
"params_id": "IP",
"params_name": "Inpatient",
"params_gender": "All",
"params_description": "Rawat Inap",
"params_created": 1
},
{
"params_id": "OP",
"params_name": "Outpatient",
"params_gender": "All",
"params_description": "Rawat Jalan",
"params_created": 1
}
]
```
And send request result above with params_json POST, get var_dump($this->request->getVar()) below
```
array(1) {
["param_json"]=>
string(364) "[
{
"params_id": "IP",
"params_name": "Inpatient",
"params_gender": "All",
"params_description": "Rawat Inap",
"params_created": 1
},
{
"params_id": "OP",
"params_name": "Outpatient",
"params_gender": "All",
"params_description": "Rawat Jalan",
"params_created": 1
}
]"
}
```
What sample validation correct to result above and insert it to database with InsertBatch?
Currently i try
Code: $dataValidate = json_decode($this->request->getPost('params_json'), true);
and get array below
Code: array(2) {
[0]=>
array(5) {
["params_id"]=>
string(2) "IP"
["params_name"]=>
string(9) "Inpatient"
["params_gender"]=>
string(3) "All"
["params_description"]=>
string(10) "Rawat Inap"
["params_created"]=>
int(1)
}
[1]=>
array(5) {
["params_id"]=>
string(2) "OP"
["params_name"]=>
string(10) "Outpatient"
["params_gender"]=>
string(3) "All"
["params_description"]=>
string(11) "Rawat Jalan"
["params_created"]=>
int(1)
}
}
And validate with
PHP Code: $validationRules = [ '*.params_id' => 'required|string|max_length[3]|is_unique[sm_benefit_group.benefitgroup_id]', '*.params_name' => 'required|string|max_length[100]', '*.params_gender' => 'required|validBenefitGroupGender', '*.params_description'=> 'required|string|max_length[255]', '*.params_created' => 'required|integer', ];
$validationMessages = [ '*.params_id' => [ 'required' => 'ID must be required', 'string' => 'ID must be string', 'max_length' => 'ID must be maximum 3 length', 'is_unique' => 'ID already exists, try another unique', ], '*.params_name' => [ 'required' => 'Name must be required', 'string' => 'Name must be string', 'max_length' => 'Name must be maximum 100 length', ], '*.params_gender' => [ 'required' => 'Gender Type must be required', 'validBenefitGroupGender' => 'Gender Type must be string and between ("All","M","F")', ], '*.params_description' => [ 'required' => 'Description must be required', 'string' => 'Description must be string', 'max_length' => 'Description must be maximum 255 length', ], '*.params_created' => [ 'required' => 'Created By must be required', 'integer' => 'Created By must be integer', ], ];
$this->validation->setRules($validationRules, $validationMessages);
if ($this->validation->run($dataValidate)) { // InsertBatch here } else { $response = [ 'success' => false, 'status' => 400, 'messages' => 'Validation data benefit group failed!', 'validation' => $this->validation->getErrors(), ]; // Result return $this->respond($response, 400); }
Still not valid
Code: {
"success": false,
"status": 400,
"messages": "Validation data benefit group failed!",
"validation": {
"*.params_id": "ID must be required",
"*.params_name": "Name must be required",
"*.params_gender": "Gender Type must be required",
"*.params_description": "Description must be required",
"*.params_created": "Created By must be required"
}
}
-
iRedds Senior Member
   
-
Posts: 662
Threads: 36
Joined: Apr 2019
Reputation:
45
-
flux1on Junior Member
 
-
Posts: 15
Threads: 7
Joined: Apr 2020
Reputation:
0
07-08-2022, 01:01 AM
(This post was last modified: 07-08-2022, 01:30 AM by flux1on.)
(07-08-2022, 12:41 AM)iRedds Wrote: This is a bug.
Until it's fixed, you can validate each dataset individually.
https://github.com/codeigniter4/CodeIgniter4/pull/6243
Thankyou for helping to fix this
(07-08-2022, 12:41 AM)iRedds Wrote: This is a bug.
Until it's fixed, you can validate each dataset individually.
https://github.com/codeigniter4/CodeIgniter4/pull/6243
Now with your PR works for me.
PHP Code: public function createBatch() { $dataValidate = json_decode($this->request->getPost('params_json'), true);
$validationRules = [ '*.params_id' => 'required|string|max_length[3]|is_unique[sm_benefit_group.benefitgroup_id]', '*.params_name' => 'required|string|max_length[100]', '*.params_gender' => 'required|validBenefitGroupGender', '*.params_description'=> 'required|string|max_length[255]', '*.params_created' => 'required|integer', ];
$validationMessages = [ '*.params_id' => [ 'required' => 'ID must be required', 'string' => 'ID must be string', 'max_length' => 'ID must be maximum 3 length', 'is_unique' => 'ID already exists, try another unique', ], '*.params_name' => [ 'required' => 'Name must be required', 'string' => 'Name must be string', 'max_length' => 'Name must be maximum 100 length', ], '*.params_gender' => [ 'required' => 'Gender Type must be required', 'validBenefitGroupGender' => 'Gender Type must be string and between ("All","M","F")', ], '*.params_description' => [ 'required' => 'Description must be required', 'string' => 'Description must be string', 'max_length' => 'Description must be maximum 255 length', ], '*.params_created' => [ 'required' => 'Created By must be required', 'integer' => 'Created By must be integer', ], ];
$this->validation->setRules($validationRules, $validationMessages);
if ($this->validation->run($dataValidate)) { $dataParams = array();
foreach ($dataValidate as $index => $value) { $dataParams[$index]['benefitgroup_id'] = $value['params_id']; $dataParams[$index]['benefitgroup_name'] = $value['params_name']; $dataParams[$index]['benefitgroup_gender'] = $value['params_gender']; $dataParams[$index]['benefitgroup_description'] = $value['params_description']; $dataParams[$index]['benefitgroup_created_by'] = $value['params_created']; $dataParams[$index]['benefitgroup_modified_by'] = $value['params_created']; } $result = $this->benefitgroup_model->saveDataBatch($dataParams); if ($result) { $response = [ 'success' => true, 'status' => 201, 'messages' => 'Data benefit group saved!', ]; // Result return $this->respond($response, 201); } else { $response = [ 'success' => false, 'status' => 400, 'messages' => 'Data benefit group cant be saved!', ]; // Result return $this->respond($response, 400); } } else { $response = [ 'success' => false, 'status' => 400, 'messages' => 'Validation data benefit group failed!', 'validation' => $this->validation->getErrors(), ]; // Result return $this->respond($response, 400); } }
-
kenjis Administrator
      
-
Posts: 3,671
Threads: 96
Joined: Oct 2014
Reputation:
230
|