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.

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



61.0k

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


61.0k
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!