Phalcon Model Validation

Phanix
·
·
IPFS
·

新舊版本的寫法差蠻多的,被舊的 phalcon devtools 自動產生的 model 給陰了..

<?php

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

class Users extends \Phalcon\Mvc\Model
{
    public function validation()
    {
        $this->validate(
            new Email(
                array(
                    'field'    => 'email',
                    'required' => true,
                )
            )
        );

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

        return true;
    }
}
?>

要改成像這樣… 整個連class namespace 都大改

<?php
use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as Email;
use Phalcon\Validation\Validator\PresenceOf as PresenceOf;

class Users extends \Phalcon\Mvc\Model
{
    public function validation()
    {
        $validation = new Validation();

        $validation-> add(
                "email",
                new Email(
                        ['model' => $this, "message" => "The e-mail is not valid"]
                    )
            );

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

Original link: Phanix's Blog

CC BY-NC-ND 2.0 授权

喜欢我的作品吗?别忘了给予支持与赞赏,让我知道在创作的路上有你陪伴,一起延续这份热忱!