开发者

CakePHP - Utility method for checking a string is a valid email address?

开发者 https://www.devze.com 2023-03-22 18:26 出处:网络
In a controller, I need to check a string to see if it is a valid email address.Is there an existing method开发者_开发技巧 in Cake that I can use to check this?

In a controller, I need to check a string to see if it is a valid email address. Is there an existing method开发者_开发技巧 in Cake that I can use to check this?

It has nothing to do with models, so I don't want to use a validate array.


I found the core Validation class. Validation::email()

App::uses('Validation', 'Utility');

class MyController extends AppController
{
    public function myAction()
    {
        $isValid = Validation::email('person@example.com'); // Returns true or false
    }
}


you may still use the model validation for it

$this->Model->set($data);
if ($this->Model->validates()) {}

your current model should have a rule for "email" if you are handling emails in this controller. or you can create one here.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号