hi i m huge fan ci4
i was facing issue which pin in ass but solve it
i figure out which CI4 can not validate Ajax request (JSON Raw Input) with Postman or Angular 9
i was customizing Math/Auth for RestFull API JWT Authentication
i got error which can not validate Raw Input JSON
heres part of code auth controoler
Code:
if ($this->request->isAJAX()) {
/**
* Attempts to verify the user's credentials
* through a POST request.
*/
$rules = [
'login' => 'required',
'password' => 'required',
];
$config = config('Myth\Auth\Config\Auth');
if ($config->validFields == ['email']) {
$rules['login'] .= '|valid_email';
};
i connot validate And Got Error I dont send login and password
Code:
if (!$this->validate($rules)) {
$responseDataModel->setToken('');
$responseDataModel->setData($this->validator->getErrors());
$responseDataModel->setMessage('error during login validate ');
$responseDataModel->setSuccess(false);
return $this->response->setJSON($responseDataModel->getRowData())->setStatusCode(400)->setContentType('application/json');
}
}
i go to validation class CI4 then function
withRequest()
Code:
public function withRequest(RequestInterface $request): ValidationInterface
{
if (in_array($request->getMethod(), ['put', 'patch', 'delete'])) {
$this->data = $request->getRawInput();
} else {
$this->data = $request->getVar() ?? [];
}
return $this;
}
add this line to code and fix bug easily
Code:
public function withRequest(RequestInterface $request): ValidationInterface
{
if (in_array($request->getMethod(), ['put', 'patch', 'delete'])) {
$this->data = $request->getRawInput();
} else if ($request->isAJAX()) {
$this->data = $request->getJSON(true);
} else {
$this->data = $request->getVar() ?? [];
}
return $this;
}
i hope next version ci4 add this line to code
thanks to great team ci4
Enlightenment Is Freedom