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

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....



98.9k

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();
}


18.5k
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...