【问题标题】:In my cakephp project the save function is not working?在我的 cakephp 项目中,保存功能不起作用?
【发布时间】:2023-04-07 05:22:01
【问题描述】:

我的控制器动作是:

function add() {  
 if (!empty($this->data)) {
  $this->Customer->create();
  if ($this->Customer->save($this->data)) {
    $this->Session->setFlash('A new Customer has been added');
    $this->redirect(array('action'=>'index'));
  } 
  else {    
   $this->Session->setFlash('The customer cannot be added this time. Try again later.');
   $this->redirect(array('action'=>'index'));
  }
 }
}

我的模型是:

类客户扩展 AppModel {

var $name = 'Customer';
var $validate = array(
   'name' => array(
    'length'=> array(
     'rule' => array('between', 4,50),
         'message' => 'Name must be minimum 4 and maximum 50 characters long.'    
         ),                 
    'checkUnique'=> array(
       'rule' => 'isUnique',
       'message' => 'This Name is already registered'    
    )
  ));

这是我的观点:

<div class="customers form">
<?php echo $form->create('Customer',array('action'=>'add'));?>
 <fieldset>
   <legend><?php __('Add Customer');?></legend>
 <?php
  echo $form->input('Customer.name');
  echo $form->input('Customer.balance',array('type'=>'hidden','default'=>0)); 
 ?>
 </fieldset>
<?php echo $form->end('Submit');?>
</div>

每次我提交表单时它都会飞溅: 这次无法添加客户。请稍后再试。

【问题讨论】:

    标签: cakephp save


    【解决方案1】:

    对于隐藏的输入字段,在您的视图中使用“值”而不是“默认”:

    $form->input('Customer.balance', array('type' => 'hidden', 'value' => 0));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 2010-10-20
      • 2022-06-15
      • 2021-04-15
      • 2021-03-10
      相关资源
      最近更新 更多