tags:

views:

25

answers:

1

Hi,

I am using cakephp. I have a form with an element array. For ex:-

<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 need to show errors to the respective field. What is the correct syntax for invalidating the field if it an element array ? I know, the following will work for single element . How it will be for an element array ?

$this->User->invalidate("description");
+1  A: 

Unfortunately you cannot invalidate the field with that function.

But what invalidate() does?

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

It just set validationErrors of the model.

So, you can do following in your Controller (but I also appeal you to move that validation in the Model):

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

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

HTH

Nik