We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon Form Validation of Input Array

I have multiple text boxes having name abc[] and did not know how to validate my text box using phalcon validation object.



3.8k
Accepted
answer

I'm also new to Phalcon, but as I know. You can create your own validator.

On controller you should get the array from Request, example:

$abcValues = $this->request->getPost("abc");//If you're using form method POST to send data

You can validate on controller action method, or you can create your own Validator. There is an example of custom validator: https://docs.phalcon.io/pt/latest/reference/validation.html#cancelling-validations

On your own validator you could do something like this:

class ArrayValidator extends Validator implements ValidatorInterface
{
  public function validate(Validation $validator, $attribute)
  {
    // If the attribute value is name we must stop the chain
    if (is_array($attribute) && count(attribute) > 0) {
        //Validate whatever
        return true;//Valid data
    }
    return false;//invalid data
  }
}

Of course you should create this on a folder that is loaded on loader, like "library" folder as shown on INVO example.

And on your validation logic do something like this:


....
validation->add('abc', new ArrayValidator(array(
    'message': 'Oh Snap! You gave me the wrong data'
)));
....

There are some other resources on forum about "Custom Validator" which could be usefull, but is pretty much it.

Thanks @Hodes after all my research I have also created my own validator for my array validation. Waiting For any new update in phalcon to validate array