如何在Cakephp中不使用模型执行表单验证? [英] How do you perform Form validation without a model in Cakephp?

查看:74
本文介绍了如何在Cakephp中不使用模型执行表单验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要执行一些验证。我没有在应用程序中的模型。

I need to perform some validation. I don't have the model in the application.

有没有人知道如何做没有模型的验证?
你能告诉我一个小样本或语句吗?

Does anyone know how to do the validation without a model? Can you show me using a small sample or statement?

推荐答案

老实说,验证。您可以通过添加

Honestly, I'd create a model just for the validation. You can create a model that doesn't use a table by adding

var $useTable = false;

然后为要验证的每个字段创建一个包含规则的验证数组:

And then create a validation array with rules for each field you want to validate:

var $validate = array('login' => 'alphaNumeric','email' => 'email','born' => 'date');

然后,在您的控制器中,执行以下操作:

Then, in your controller, do something like:

$this->MyModel->set($this->data);
if($this->MyModel->validates()){
    // do stuff with valid data
}

如果真的,真的不能使用模型,那么你必须简单地循环 $ this-> data 并使用正则表达式验证它或使用 Validation :: [rule]()例如:

If you really, really can't use a model, then you'll have to simply loop over each value in $this->data in your controller action and validate it against a regular expression or use the Validation::[rule]() stuff, like:

if(Validation::email($someThingThatMightBeAnEmailAddress)){
    // do stuff with valid email address.
}

这篇关于如何在Cakephp中不使用模型执行表单验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆