A
A
Alexander Ivanov2016-05-07 19:47:49
PHP
Alexander Ivanov, 2016-05-07 19:47:49

How to fix user registration error on phalcon?

I started to learn placon and stumbled on this lesson
https://docs.phalconphp.com/ru/latest/reference/tu...
When I downloaded the invo example, I installed its database from the schemas folder.
Then, when registering a user, an error began to pop up:
Catchable fatal error: Argument 1 passed to Phalcon\Mvc\Model::validate() must implement interface Phalcon\ValidationInterface, instance of Phalcon\Mvc\Model\Validator\Email given in C:\ www\OpenServer\domains\localhost\invo\app\models\Users.php on line 13
At the same time, when I log in as [email protected], then everything is authorized and it is from the database
Users.php code

<?php

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

class Users extends Model
{
    public function validation()
    {
        $this->validate(new EmailValidator(array(
            'field' => 'email',
        )));
        $this->validate(new UniquenessValidator(array(
            'field' => 'email',
            'message' => 'Sorry, The email was registered by another user'
        )));
        $this->validate(new UniquenessValidator(array(
            'field' => 'username',
            'message' => 'Sorry, That username is already taken'
        )));
        if ($this->validationHasFailed() == true) {
            return false;
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
ZaxapKramer, 2016-05-07
@cimonlebedev

What version of the framework are you using?
In version 2.1, the developers abandoned the separate validator in the models (in my opinion, this is the right decision).
Use Validation:

<?php
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()
    {
        $validation = new Validation();

        $validation
            ->add('email', new EmailValidator())
            ->add('email', new UniquenessValidator(array(
                'model'   => $this,
                'message' => 'Sorry, The email was registered by another user'
            )))
            ->add('username', new UniquenessValidator(array(
                'model'   => $this,
                'message' => 'Sorry, That username is already taken'
            )));

        return $this->validate($validation);
    }
}

And if anything, you can display errors in the controller using the flash method , of course, passing your model to it:
<?php
// ...

$user = new Users;

foreach ($user->getMessages() as $message) {
    $this->flash->error($message->getMessage());
}

// ...

PS The framework is developing, the documentation is outdated :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question