# How to validate array of form inputs in cakephp 2

**URL:** <https://discourse.cakephp.org/t/how-to-validate-array-of-form-inputs-in-cakephp-2/7070>\
**Category:** Need Help\
**Created:** [January 7, 2020, 4:15pm UTC](https://discourse.cakephp.org/t/how-to-validate-array-of-form-inputs-in-cakephp-2/7070 "2020-01-07T16:15:22Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![marko](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@marko](https://discourse.cakephp.org/u/marko)\
**Post date:** [January 7, 2020, 4:15pm UTC](https://discourse.cakephp.org/t/how-to-validate-array-of-form-inputs-in-cakephp-2/7070/1 "2020-01-07T16:15:22Z")

</div>

I have this kind of array of inputs in view:  
echo $this-\>Form-\>input(“Application.ref.$i”, …

Say $i=1…

How can I validate this in Model public $validate ? I tried ‘Application.ref.1’ but it does not seem to work… I did not find clues in the book…

---

<div class="post-metadata">

**Author:** ![Zuluru](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/zuluru/32/1230_2.png) [@Zuluru](https://discourse.cakephp.org/u/Zuluru)\
**Post date:** [January 7, 2020, 5:10pm UTC](https://discourse.cakephp.org/t/how-to-validate-array-of-form-inputs-in-cakephp-2/7070/2 "2020-01-07T17:10:55Z")

</div>

What is “ref”? In this sort of structure, that’s typically going to be another entity with a `hasMany` or `belongsToMany` relation to the main entity (in which case the input name should have a plural there, like `refs`). If that’s the case, then the place to do that validation is in that other entity’s table.

---

<div class="post-metadata">

**Author:** ![marko](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@marko](https://discourse.cakephp.org/u/marko)\
**Post date:** [January 7, 2020, 5:18pm UTC](https://discourse.cakephp.org/t/how-to-validate-array-of-form-inputs-in-cakephp-2/7070/3 "2020-01-07T17:18:37Z")

</div>

Names can be misleading… Actually there are no relations, it’s just the naming. Only one model in this case. It could be e.g. “Myclub.member.1”. The point is that it is array of form inputs in the view and it works fine. Model: $this-\>data[‘Application’][‘ref’][0]

But I cannot find right validation syntax/way to access those entries… or should i make an my own function in my model to loop and check those data manually?

---

<div class="post-metadata">

**Author:** ![marko](https://avatars.discourse-cdn.com/v4/letter/m/dbc845/32.png) [@marko](https://discourse.cakephp.org/u/marko)\
**Post date:** [January 8, 2020, 6:44am UTC](https://discourse.cakephp.org/t/how-to-validate-array-of-form-inputs-in-cakephp-2/7070/4 "2020-01-08T06:44:15Z")

</div>

Maybe I have to get rid of that dot and use Application.ref1, Application.ref2… It’s more tedious but I cannot find the solution to arrays…

---

<div class="post-metadata">

**Author:** ![dreamingmind](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/dreamingmind/32/1857_2.png) [@dreamingmind](https://discourse.cakephp.org/u/dreamingmind)\
**Post date:** [January 8, 2020, 4:39pm UTC](https://discourse.cakephp.org/t/how-to-validate-array-of-form-inputs-in-cakephp-2/7070/5 "2020-01-08T16:39:10Z")

</div>

In cake 3 I had a similar issue and the solution may apply in 2.

I had a schema that I wanted to define using dot notation in a modelless form. The form helper took in the schema well enough, but the post back to my app was an expanded array and this made the post-data not match my validation rules which were expecting dot notation.

I used the Cake utility class method `Hash::flatten($array)` to satisfy the validators.

Later I had to expand the `errors()` that were returned from the validator to satisfy expectation of the form for error reporting.

I used an override of the `Form::validate($data)` call.

```php
    public function validate($data)
    {
        $result = parent::validate(Hash::flatten($data));
        $this->setErrors(Hash::expand($this->getErrors()));
        return $result;
    }

```
