【问题标题】:CakePHP 2.1 Contact Form Data Won't SaveCakePHP 2.1 联系表单数据不会保存
【发布时间】:2012-05-24 09:18:48
【问题描述】:

我的 CakePHP 2.0 应用程序中有几个不同的联系表格。所有的联系表格都应该通过电子邮件发送,但我需要这个特殊的表格来将表格结果保存到数据库中。帖子数据正在填充,我可以print_r()pr() 表单数据。我什至可以通过电子邮件发送帖子数据。但是,它实际上并没有将数据保存到模型表中。数据库表名为contacts,具有以下字段:id, publication, company, name, email, phone, message, contact_method, selections, received

这是我的模型:

class Contact extends AppModel {
    public $name = 'Contact';

    public $useTable = 'contacts';

    public $validate = array(  
    'name' => array(
            'rule' => 'notEmpty'
        ),
        'email' => array(
            'rule' => 'notEmpty'
        )
    );

这是我的控制器:

App::uses('CakeEmail', 'Network/Email');

class ContactsController extends AppController
{
    public $name = 'Contacts';
    public $helpers = array('Html', 'Form', 'Js');
    public $components = array('Email', 'Session');

...

    public function contact_att() {

        if ($this->request->is('post')) {

            //pr($this->data);
            if ($this->Contact->save($this->request->data)) {
                $this->redirect('/pages/publications-alabama-turf-times');
                $this->Session->setFlash("Mesage Saved!"); 
            }

            else {
                print_r($this->data);
                Configure::write('debug', 2); 
                debug($this->Contact->validationErrors); 

                exit;
            }
        }

这是我认为的形式:

echo $this->Form->create('Contact', array(
    'action' => 'contact_att', 
    'label' => '', 
    'class' => 'pubs'));
echo $this->Form->input('publication', array(
    'type' => 'hidden', 
    'value' => 'A', 
    'label' => ''));
echo $this->Form->input('company', array(
    'default' => 'company name (required)', 
    'onfocus' => 'clearDefault(this)', 
        'label' => array(
            'text' => 'Company Name',
            'style' => 'position:absolute;')));
echo $this->Form->input('name', array(
    'default' => 'name (required)', 
    'onfocus' => 'clearDefault(this)', 
    'label' => array(
        'text' => 'Your Name',
        'style' => 'position:absolute;')));
echo $this->Form->input('phone', array(
    'default' => 'phone number (required)', 
    'onfocus' => 'clearDefault(this)', 
    'label' => array(
        'text' => 'Your Phone Number',
        'style' => 'position:absolute;')));
echo $this->Form->input('email', array(
    'default' => 'email (required)', 
    'onfocus' => 'clearDefault(this)', 
    'label' => array(
        'text' => 'Your Email Address',
        'style' => 'position:absolute;')));
echo $this->Form->input('message', array(
    'label' => array(
        'text' => 'Your Message',
        'style' => 'position:absolute;')));
echo $this->Form->input('contact_method', array(
    'type' => 'radio',
    'style' => 'padding-right:20px;', 
    'legend' => 'Preferred contact method:', 
    'options' => array(
        'phone' => 'phone',
        'email' => 'email'
        )
    ));
echo $this->Form->input('selections', array(
    'type' => 'select', 
    'label' => array(
    'text' => 'I am interested in the following:', 
    'style' => 'display:block; width:250px; margin-left:-12px;padding-bottom:15px;'),
        'multiple' => 'checkbox',
        'options' => array(
            'ABC' => 'ABC', 
            'DEF' => 'DEF', 
            'GHI' => 'GHI'
         )
    ));

echo $this->Form->end('Submit');

我错过了什么?

【问题讨论】:

  • debug($this->Contact->validationErrors); 有输出吗?
  • 不幸的是,不——只是一个空数组。我试过把它放在保存之前,保存之后的if语句中,如果数据没有保存,则放在else语句中。

标签: php forms email cakephp


【解决方案1】:

请添加此行

$this->Contact->create();

在您尝试保存之前使用

if ($this->Contact->save($this->request->data)) {

【讨论】:

    【解决方案2】:

    只有当表单设置了张贴隐藏标志时,张贴才会返回真。所以试试这个吧。

    if(!empty($this->request->data))
    

    【讨论】:

      【解决方案3】:

      朋友,我看到你的问题,检查..

      您的控制器中缺少此行

      public $uses = array('Contact');
      

      放试试看,然后你告诉我...

      【讨论】:

        【解决方案4】:

        在我的头在桌子上敲了很多次之后,答案变得很简单——当然。我只是从我的模型中删除了这条线。我认为将其设置为正确的表就可以了,但事实证明,它需要被删除:

        public $useTable = 'contacts';
        

        【讨论】:

        • 你能告诉我们更多关于你放那条线时发生的事情吗?查询是否生成不正确?以后可能会对其他人有所帮助。 :)
        • 是的,当我删除该行时,查询运行良好,除了保存多个复选框选择的问题。我通过在模型中使用 beforeSave() 函数将该字段的数据数组编译成一个字符串来解决这个问题。
        【解决方案5】:

        尝试:

        debug($this->model->invalidFields());
        

        有时,模型在验证时会出错,而validationErrors() 不会显示

        还要注意这一点..

        如果未提供 $fieldList,恶意用户可以向表单数据添加额外的字段(如果您没有使用 SecurityComponent),并通过此更改原本不打算更改的字段。

        http://book.cakephp.org/2.0/en/models/saving-your-data.html

        花点时间阅读这份文档很重要,希望对你有所帮助。

        【讨论】:

        • 我试过debug($this->Contact->invalidFields());,但它只输出一个空数组。我还将if ($this->Contact->save($this->request->data) 更新为if ($this->Contact->save($this->request->data, $fieldlist)。感谢您提供有关安全性的提示,但现在,问题仍然是数据没有保存。我相当肯定 FormHelper 应该有助于防止添加字段。不过,我还是试过了。
        【解决方案6】:

        你可以试试这个:

        $this->Contact->save($this->request->data, false);
        

        【讨论】:

        • 这有什么帮助?这只是绕过验证。
        • 我想问一下 false 参数...绕过验证?我尝试删除表单验证,但不允许保存表单,所以我认为这不是问题。
        • 我确实尝试了这个建议...仍然没有保存。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-25
        • 2023-03-12
        • 1970-01-01
        • 2021-12-05
        • 1970-01-01
        相关资源
        最近更新 更多