Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Validation issue: BadMethodCallException

Using documentation I was trying to use validation class to validate a field submited to $_POST.

use Phalcon\Validation;
use Phalcon\Mvc\Model\Validator\PresenceOf;
[...]
    $validation = new Validation();
    $validation->add('pageNo', new PresenceOf(array(
        'message' => 'Page no is required'
    )));
    if ($this->request->isPost())
    {
        $messages = $validation->validate($_POST);
        [...]

My $_POST is:

$_POST = array(
    'pageNo' => 1,
    'recPerPage' => 10
);

During execution I am getting

BadMethodCallException: Wrong number of parameters
on      $messages = $validation->validate($_POST);

I cannot find what is wrong here - it seems to me that I am copying the documentation, but I guess I must be missing something....



83.2k

Check the unit-tests for that component: https://github.com/phalcon/cphalcon/blob/master/unit-tests/ValidationTest.php#L111, also printing the exception backtrace will show which method is throwing the exception:

try {
    //... code here
} catch (\Exception $e) {
    echo $e->getMessage();
    echo $e->getTraceAsString();
}


12.2k
Accepted
answer

You are adding Phalcon\Mvc\Model\Validator\PresenceOf to Phalcon\Validation... It's not correct... try to add Phalcon\Validation\Validator\PresenceOf ... They are different...