cakephp - 使用 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(); ?>
...
非常感谢
解决方案
推荐阅读
- angular - 如何将 3 个数据数组合并到一个可迭代数组中?
- ios - “GIDSignIn”类型的值没有成员“presentingViewController”
- javascript - 如何从 mysql 表中评估 php 代码(代码包含 javascript 代码)
- php - PHP/Laravel - Short(er)hand 如果只有 true
- java - 导出后,java无法显示图像
- python - 无法使用请求解析网页中不同项目的链接
- vue.js - 在vuex中循环处理表单?
- r - 当我想创建自己的包时如何安装对象'create'?
- firebase - 无法从命令提示符输入/选择选项
- linux - Linux文件套接字中是否存在“一对多”连接?