在我的 cakephp 项目中,保存功能不起作用?

发布于 2024-08-30 21:23:28 字数 1443 浏览 2 评论 0原文

我的控制器操作是:

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'));
  }
 }
}

我的模型是:

class Customer extends 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>

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

My controller action is:

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'));
  }
 }
}

My model is:

class Customer extends 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'    
    )
  ));

and this is my view:

<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>

every time I submit the form it splashes:
The customer cannot be added this time. Try again later.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

久而酒知 2024-09-06 21:23:28

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

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

For the hidden input field use 'value' instead of 'default' in your view:

$form->input('Customer.balance', array('type' => 'hidden', 'value' => 0));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文