首页 > 解决方案 > 使用 CakePHP 4 创建联系表的麻烦

问题描述

不久前我开始使用 cakePHP 4,我的网站已经完成了。但是我在创建联系表格时遇到了一些麻烦。我按照食谱 > 无模型表单、验证和表单上的指示做了,但我没有正确理解(对我来说,食谱有时真的很难理解)。
当我按下按钮发送表单时,会出现相同的页面并填充字段,但不显示验证或错误消息。我确定我忘记了从控制器发送这些信息的东西以及从控制器获取这些数据的东西,但是......我找不到我真正需要的东西。我也不完全明白我必须填写 ContactController "$this->request->is('get')"
我希望你们中的某个人能帮助我。也许我只是在我的头前有一个大板...

这是我的代码(模板代码只是摘录)

src/Controller/ContactController.php

<?php
namespace App\Controller;

use App\Controller\AppController;
use App\Form\ContactForm;

class ContactController extends AppController
{
    public function index()
    {
        $contact = new ContactForm();
        if ($this->request->is('post')) {
            if ($contact->execute($this->request->getData())) {
                $this->Flash->success('We will get back to you soon.');
            } else {
                $this->Flash->error('There was a problem submitting your form.');
                $contact->setErrors(['name' => ['_required' => 'at least 5 signs'],['email' => ['_required' => 'Your email is required']]);
            }
        }

        if ($this->request->is('get')) {
            $contact->setData([
                'name' => 'John Doe',
                'email' => 'john.doe@example.com'
            ]);
        }

        $this->set('contact', $contact);
    }
}

src/Form/ContactForm.php

<?php
/* https://book.cakephp.org/4/en/core-libraries/form.html */
namespace App\Form;

use Cake\Form\Form;
use Cake\Form\Schema;
use Cake\Validation\Validator;

class ContactForm extends Form
{
    protected function _buildSchema(Schema $schema): Schema
    {
        return $schema->addField('name', 'string')
            ->addField('email', ['type' => 'string'])
            ->addField('subject', ['type' => 'string'])
            ->addField('message', ['type' => 'text']);
    }

    public function validationDefault(Validator $validator): Validator
    {
        $validator->minLength('name', 5)
            ->email('email')
            ->notEmptyString('subject', 'Please fill this field')
            ->notEmptyString('text', 'Please fill this field')

        return $validator;
    }

    protected function _execute(array $data): bool
    {
        print_r('TESTING OK');
        return true;
    }

    public function setErrors($errors)
    {
        $this->_errors = $errors;
    }
}

templates/Pages/contact.php(与样式的现成 HTML 模板一起使用)

...
<?php echo $this->Form->create($contact); ?>
       <div class="row gtr-50 gtr-uniform">
             <div class="col-6 col-12-mobilep">
             <?php echo $this->Form->control('name',['label' => false,'placeholder'=>$translation['Name'],'templates' => ['inputContainer' => '{{content}}']]);?>
             </div>
       <div class="col-6 col-12-mobilep">
             <?php echo $this->Form->control('email',['label' => false,'placeholder'=>$translation['Email'],'templates' => ['inputContainer' => '{{content}}']]);?>
             </div>
             <div class="col-12">
             <?php echo $this->Form->control('subject',['label' => false,'placeholder'=>$translation['Betreff'],'templates' => ['inputContainer' => '{{content}}']]);?>
             </div>
             <div class="col-12">
             <?php echo $this->Form->textarea('message',['label' => false,'placeholder'=>$translation['Bitte gebe deine Nachricht ein'],'rows'=>'6']); ?>
             </div>
             <div class="col-12">
             <ul class="actions special">
             <li><?php echo $this->Form->submit($translation['Nachricht senden']); ?></li>
       </ul>
    </div>
</div>
<?php echo $this->Form->end(); ?>
...

非常感谢

标签: cakephpcontact-form

解决方案


推荐阅读