Cakephp element invalidate array

I am using cakephp. I have a form with an array of elements. For example: -

<textarea name="data[User][0][description]>
<textarea name="data[User][1][description]>

From the controller, I need to invalidate (manually) the array field if it is empty, and I need to show errors in the corresponding field. What is the correct syntax for invalidating a field if it is an array of elements? I know the following will work for a single item. How will it be for an array of elements?

$this->User->invalidate("description");
+2
source share
4 answers

Unfortunately, you cannot invalidate a field using this function.

But what does invalidate () do?

function invalidate($field, $value = true) {
        if (!is_array($this->validationErrors)) {
            $this->validationErrors = array();
        }
        $this->validationErrors[$field] = $value;
    }

It just sets the validationErrors of the model.

So, you can do the following in your controller (but I also urge you to move this check in the model):

$this->User->validationErrors[1]['description'] = 'Your error message';

The following code will invalidate the second description in the list.

+4

:

<?php 
    echo $this->Form->error("User.1.description");
?>
+4

Nik,

, , .

account_number {
    bank_code, 
    bank_office,
    check_digit,
    account
}

, , :

$this->Model->validationErrors['account_number']['bank_code'][0] = 'Your message error';

I hope this helps someone.

Sincerely.

0
source

I had a similar problem, since it was for the admin panel that I displayed an error message at the first level of the field, that is, only for this part.

If you are checking the controller, simply create an error array with the field name and error message, set it in the controller and display a message if in_array ($ field, $ withErrorArray) is in view.

0
source

All Articles