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

Form validation in Phalcon 2.1 beta 2

Hello,

I have try the new validation from 2.1 beta 2 release but it does not seem to work. Here is the code i have used:

namespace Invo\Models;

use Phalcon\Mvc\Model;
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as EmailValidator;
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;

class Users extends Model
{
    public function validation()
    {
        $validator = new Validation();

        $validator->add(
            'email',
            new EmailValidator()
        );

        $validator->add(
            'username',
            new UniquenessValidator(
                [
                    'message' => 'Sorry, That username is already taken',
                ]
            )
        );

        return $this->validate();
    }
}

I got this error: Wrong number of parameters\n#0 /var/www/html/ngsecure/app/models/Users.php(103): Phalcon\Mvc\Model->validate()\n#1



85.5k

validators in 2.1 are :


use Phalcon\Validation;

public function validation() {
$validator = new Validation();

$validator->add(
            "number",
            new Numericality([
                "model" : $this,
                "message" : "Enter a number"
            ])
        );
        return $this->validate();
    }
edited Jan '16

Thank you for aswer but I got the same error :(

Wrong number of parameters\n#0 /var/www/html/ngsecure/app/models/Users.php(148): Phalcon\Mvc\Model->validate()\n#1 [internal function]: NedaGames\Models\Users->validation()\n#2

Here is my code:

use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;

public function validation()
    {
        $validator = new Validation();

        $validator->add(
            'name',
            new UniquenessValidator(
                [
                    'model' => $this,
                    'message' => 'Sorry, That username is already taken.',
                ]
            )
        );

        return $this->validate();
    }


85.5k
Accepted
answer

oh man i am dump, i am very sorry.

change return $this->validate();

to

return this->validate($validator);

because my models are in zephir its a bit tricky to read and translate it :D

cheers!

Yes, it works now! :) Thank you again!