Cake PHP模型验证未在我的视图中显示(Cake PHP model validation not displaying in my view)

编程入门 行业动态 更新时间:2024-10-27 22:20:43
Cake PHP模型验证未在我的视图中显示(Cake PHP model validation not displaying in my view)

我正在使用蛋糕PHP来整合我的东西; 但是我的视图中没有显示模型验证错误。我认为我的代码遗漏了一些小想法,但没有发现。 下面是我的模型,视图和控制器代码:

Model: class Category extends Model { var $validate = array( 'title' => array( 'rule' => 'notEmpty', 'message' => 'Title is required' ) ); } View: <?php echo $this->Form->create( 'Category', array( 'action'=>'add', 'enctype' => 'multipart/form-data' ) ); ?> <fieldset> <legend>Add new category</legend> <?php echo $this->Form->input( 'title' ); ?> </fieldset> <?php echo $this->Form->submit( 'Add Category' ); echo $this->Form->end(); ?> Controller: class CategoriesController extends AppController { var $helpers = array( 'Form', 'Html', 'Js' ); function add() { $this->loadModel('Category'); $this->Category->set( $this->data ); $this->Category->save( $this->data ); if( !$this->Category->validates() ){ $errors = $this->Category->validationErrors; } } } Thanks in advance.

I am using cake PHP for integrating my stuff; however it is not showing model validation errors in my view.I think some small think is missed from my code, but didn't find out. Below is my model, view and controller code:

Model: class Category extends Model { var $validate = array( 'title' => array( 'rule' => 'notEmpty', 'message' => 'Title is required' ) ); } View: <?php echo $this->Form->create( 'Category', array( 'action'=>'add', 'enctype' => 'multipart/form-data' ) ); ?> <fieldset> <legend>Add new category</legend> <?php echo $this->Form->input( 'title' ); ?> </fieldset> <?php echo $this->Form->submit( 'Add Category' ); echo $this->Form->end(); ?> Controller: class CategoriesController extends AppController { var $helpers = array( 'Form', 'Html', 'Js' ); function add() { $this->loadModel('Category'); $this->Category->set( $this->data ); $this->Category->save( $this->data ); if( !$this->Category->validates() ){ $errors = $this->Category->validationErrors; } } } Thanks in advance.

最满意答案

if( !$this->Category->save( $this->data ) ){ $errors = $this->Category->validationErrors; }

您只需检查是否已保存数据。 如果没有则处理验证错误。 将错误设置为视图或仅在控制器本身中进行调试。 这可能对你有帮助。

if( !$this->Category->save( $this->data ) ){ $errors = $this->Category->validationErrors; }

You just check wether data has been saved or not. If not then process validation errors. Set errors to the view or just debug in the controller itself. this may help you.

更多推荐

本文发布于:2023-07-29 14:05:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1316717.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:视图   模型   PHP   Cake   view

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!