Welcome Guest, Not a member yet? Register   Sign In
Input validation return empty array
#1

I'm facing a weird issue trying to validate post input.
Following the userguide I set a controller to validate few POST input sent using a form.
The controller is the following:
Code:
public function collection()
{

    if ( $this->request->is('post')) {
        $rules = [
            'record'    => 'alpha_numeric_punct',
            'record_id' => 'is_natural_no_zero',
            'region'    => 'is_natural_no_zero',
        ];

        $data = $this->request->getPost(array_keys($rules));

        if (! $this->validateData($data, $rules)) {
            $errors = $this->validator->getErrors();
        }
       
        $validData = $this->validator->getValidated();

        $data['validated'] = $validData;
        $data['errors'] = $errors;
    }
}

In my view, using a print_r command, I get and empty array as 'validated' data...while 'errors' return the error if input are not valid. I really cannot understand what's wrong with $validData and why I get an empty array. Anny hint or help to figure out what's wrong?
Thanks a lot for any help.
Reply
#2

What? It work.
 
PHP Code:
  public function index(): string
    
{
        $this->request->setGlobal('post', [
            'record'    => '1000',
            'record_id' => '1005',
            'region'    => '-1008',
        ]);
        $rules = [
            'record'    => 'alpha_numeric_punct',
            'record_id' => 'is_natural_no_zero',
            'region'    => 'is_natural_no_zero',
        ];

        $data $this->request->getPost(array_keys($rules));

        if (! $this->validateData($data$rules)) {
            $errors $this->validator->getErrors();
        }

        $validData $this->validator->getValidated();

        $data['validated'] = $validData;
        $data['errors'] = $errors ?? [];
        var_dump($data); exit;
    
Code:
array(5) {
  ["record"]=>
  string(4) "1000"
  ["record_id"]=>
  string(4) "1005"
  ["region"]=>
  string(5) "-1008"
  ["validated"]=>
  array(0) {
  }
  ["errors"]=>
  array(1) {
    ["region"]=>
    string(72) "The region field must only contain digits and must be greater than zero."
  }
}
Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#3

(This post was last modified: 11-04-2024, 08:15 AM by Vespa.)

Hi ozornick,
thanks a lot for taking time to check the code and for your feedback.
It's really weird...since I cannot get the validated data...if I edit the code using the getPost() I can see the input POST data...[/url]

You can see the working page at the following link: [url=https://aeclidb.it/census/collection]https://aeclidb.it/census/collection


In the page ( a paginated gallery ) there are a couple of input field: a select and one autocomplete input text. A jQuery code submit the form as soon as selected a region or an item in the autocomplete list.
I don't think that the issue could be related to the Javascript code....
Thanks a lot for any help

EDIT: I just realized that in your code validated is:
Code:
["validated"]=>
  array(0) {
  }
Is this correct? I thought I should get the list of input validated...
Reply
#4

As long as there are errors, getValidated() remains empty.
PHP Code:
array(5) {
  ["record"]=>
  string(4"1000"
  ["record_id"]=>
  string(4"1005"
  ["region"]=>
  string(4"1008"
  ["validated"]=>
  array(3) {
    ["record"]=>
    string(4"1000"
    ["record_id"]=>
    string(4"1005"
    ["region"]=>
    string(4"1008"
  }
  ["errors"]=>
  array(0) {
  }

Simple CI 4 project for beginners codeigniter-expenses ( topic )
Reply
#5

Ah, now I see...thanks a lot for the invaluable help ozornick...I didn't realize that.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB