Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phalcon 3: Validating form data using \Phalcon\Mvc\Model\Validator

Since the latest version of Phalcon was released, the examples provided in the documentation do not seem to work correctly.

Firstly, I create a new model with DevTools at the command line using phalcon model User.

Then, I amend the validation() function. My models\User.php file:

use Phalcon\Mvc\Model\Validator\Email as Email;
use Phalcon\Mvc\Model\Validator\Uniqueness as Uniqueness;
use Phalcon\Mvc\Model\Validator\StringLength as StringLength;

class User extends \Phalcon\Mvc\Model
{
    public function validation()
    {
        $this->validate(
            new Email(
                array(
                    'field' => 'email',
                    'message' => 'Please enter a valid email'
                )
            )
        );

        $this->validate(
            new Uniqueness(
                array(
                    'field' => 'email',
                    'message' => 'Your email is already in use'
                )
            )
        );

        $this->validate(
            new StringLength(
                array(
                    'field' => 'password',
                    'min' => 4,
                    'max' => 30,
                    'minMessage' => 'Your password must be at least 4 characters',
                    'maxMessage' => 'Your password must be less than 30 characters'
                )
            )
        );

        if ($this->validationHasFailed() == true) {
            return false;
        }

        return true;
    }
}

However, this throws the following error:

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:\xampp\htdocs\app\models\User.php on line 27

What does this mean? I believed that Phalcon\Validation and Phalcon\Mvc\Model\Validator were completely different beasties (the latter providing more functionality)?

like image 502
mpdc Avatar asked Aug 17 '16 15:08

mpdc


1 Answers

As of Phalcon 3.0 Phalcon\Mvc\Model\Validation is now deprecated in favor of Phalcon\Validation. This was done to reduce the code base, since both of the components were doing similar jobs. Below you can find examples of what needs to be changed.

Old way:

namespace Invo\Models;

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(
                [
                    'field' => 'email',
                ]
            )
        );

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

        if ($this->validationHasFailed() == true) {
            return false;
        }
    }
}

New way:

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', //your field name
            new EmailValidator([
                'model' => $this,
                'message' => 'Please enter a correct email address'
            ])
        );

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

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

More info and patch notes can be found in the Blog.

like image 172
Nikolay Mihaylov Avatar answered Nov 05 '22 06:11

Nikolay Mihaylov