This is just a snippet of code so that I can refer back in the future if required.
How do you validate non-POST dan POST data in Codeigniter 4 without resorting to creating your own validation rules?
Let say we have a very simple form with one input:
<input type="text" name="item">
And we also have a data set acquired from somewhere else:
$data['price']= "5"
The key is to have our validation runs with a variable:
$validation->run($post)
First we set $post to combine both our POST and non-POST data:
$post = array_merge($request->getPost(), $data);
Then we run our validation script as required:
\\ set our validation rules
$validation->setRules([
'item' => 'required',
'price' => 'greater_than[0]'
]);
if(! $this->validation->run($post)) {
// error
} else {
// success
}
Hope this helps.
Top comments (0)