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.
更多推荐
发布评论