Cake PHP - 表单验证不起作用
我的 CakePHP (1.2.5.) 无法验证我的表单是否正确。
$this->UserData->save($this->data);
总是给我真正的价值回报。我找不到问题所在。 UserData.nichname 的标签有效。
这就是视图:
<?php
echo $form->create('UserData');
echo $form->error('UserData.nick_name');
echo $form->input('UserData.nick_name', array('id' => 'UserDatanick_name', 'rule' => 'alphaNumeric', 'label' =>'Nickname:', 'error' =>'false'));
echo $form->end( array( 'label' => ' Save ') );
?>
这是我的控制器:
class UserDatasController extends AppController {
var $name = 'UserDatas';
function add(){
if (!empty ($this->data)){
$this->UserData->create();
if ($this->UserData->save($this->data)){
$this->Session->setFlash('input is valid');
} else {
$this->Session->setFlash('input is not valid');
}
}
}
}
模型中没有规则,这就是我不发布它的原因。
验证还需要什么?
提前致谢 史蒂夫
my CakePHP (1.2.5.) doesn't validate my form correct.
$this->UserData->save($this->data);
gives me always a true value back. I can't find the problem. The label for UserData.nichname works.
That's the View:
<?php
echo $form->create('UserData');
echo $form->error('UserData.nick_name');
echo $form->input('UserData.nick_name', array('id' => 'UserDatanick_name', 'rule' => 'alphaNumeric', 'label' =>'Nickname:', 'error' =>'false'));
echo $form->end( array( 'label' => ' Save ') );
?>
Here is my Controller:
class UserDatasController extends AppController {
var $name = 'UserDatas';
function add(){
if (!empty ($this->data)){
$this->UserData->create();
if ($this->UserData->save($this->data)){
$this->Session->setFlash('input is valid');
} else {
$this->Session->setFlash('input is not valid');
}
}
}
}
The rules for are not in the model, that's the reaseon i don't post it.
What else is necessary for a validation?
Thanks in advance
Steve
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
验证规则必须在模型中定义,而不是在视图中,另请参阅章节关于蛋糕手册中的数据验证。
The validation rules have to be defined in the model, not in the view, see also the chapter about data validation in the cakebook.
^^ 还要检查您的文件的名称。
您已将模型文件命名为 user_data.php 对吗?和你的控制器user_data_controller.php?
请注意下划线,因为您使用的是 CamelCasing。如果您的模型文件名错误,它不会抱怨,而是会使用默认的魔术模型 - 这可能就是模型中的验证规则没有被拾取的原因。
^^ also check what your files are called.
you have named your model file user_data.php right? and your controller user_data_controller.php?
Note the underscores because of your CamelCasing. If you get the model file name wrong, it wont complain but will instead use a default magic model - which could be why your validation rules within the model didnt get picked up.
我相信您只在模型中指定规则,但标签将保留在 $form->input() 函数中
I believe you only specify the rules in the model, but the label would be kept in the $form->input() function