CakePHP 3 无模式表单持久性
CakePHP 3 Modeless Form Persistence
我正在根据给定的示例创建无模式表单 here。用户单击提交按钮后,我会从数据库中检索一些信息并将其显示在表单下方的 table 中。当我点击提交按钮时,表单每次都会显示 start
和 end
的默认值,这让使用该页面的用户有些困惑。
有什么方法可以让 FormHelper
显示最终用户提交的值而不是默认值?
src/Form/StartEndForm.php
namespace App\Form;
use Cake\Form\Form;
class StartEndForm extends Form{
protected function _buildSchema(Schema $schema) {
return $schema->addField('start', [
'type' => 'date',
'default' => new Time('-1 month')
])
->addField('end', [
'type' => 'date',
'default' => new Time()
]);
}
protected function _buildValidator(Validator $validator) {
return $validator->add('start', 'date', [
'rule' => ['date'],
'message' => 'Please provide a valid date'
])
->add('end', 'date', [
'rule' => ['date'],
'message' => 'Please provide a valid date'
]);
}
protected function _execute(array $data) {
//do some SQL stuff and return the value
}
}
src/Template/Logs/index.ctp
echo $this->Form->create($form, [
'class' => 'start-end-date',
'type' => 'get'
]);
echo $this->Form->input('start');
echo $this->Form->input('end');
echo $this->Form->submit('Submit');
echo $this->Form->end();
//If values were returned, create a table
foreach(....)....
您正在使用基于 GET
的表单,即表单值通过查询字符串发送,默认情况下表单助手在查找可能的数据填充时不考虑查询字符串它的控件,因此您最终会使用架构默认值填充表单。
您可以启用查询字符串查找(自 CakePHP 3.4 起可用):
echo $this->Form->create($article, [
'class' => 'start-end-date',
'type' => 'get'
'valueSources' => [
'query', // < add this _before_ the default `context` source
'context'
]
]);
这将使表单助手明确查找当前请求中的查询数据,或者您可以切换到使用 POST
表单,它将自动将数据提取为 POST
数据默认情况下由所有内置表单上下文(Array
、Entity
、Form
、Null
)查找,作为后备或作为主要来源。
另见
我正在根据给定的示例创建无模式表单 here。用户单击提交按钮后,我会从数据库中检索一些信息并将其显示在表单下方的 table 中。当我点击提交按钮时,表单每次都会显示 start
和 end
的默认值,这让使用该页面的用户有些困惑。
有什么方法可以让 FormHelper
显示最终用户提交的值而不是默认值?
src/Form/StartEndForm.php
namespace App\Form;
use Cake\Form\Form;
class StartEndForm extends Form{
protected function _buildSchema(Schema $schema) {
return $schema->addField('start', [
'type' => 'date',
'default' => new Time('-1 month')
])
->addField('end', [
'type' => 'date',
'default' => new Time()
]);
}
protected function _buildValidator(Validator $validator) {
return $validator->add('start', 'date', [
'rule' => ['date'],
'message' => 'Please provide a valid date'
])
->add('end', 'date', [
'rule' => ['date'],
'message' => 'Please provide a valid date'
]);
}
protected function _execute(array $data) {
//do some SQL stuff and return the value
}
}
src/Template/Logs/index.ctp
echo $this->Form->create($form, [
'class' => 'start-end-date',
'type' => 'get'
]);
echo $this->Form->input('start');
echo $this->Form->input('end');
echo $this->Form->submit('Submit');
echo $this->Form->end();
//If values were returned, create a table
foreach(....)....
您正在使用基于 GET
的表单,即表单值通过查询字符串发送,默认情况下表单助手在查找可能的数据填充时不考虑查询字符串它的控件,因此您最终会使用架构默认值填充表单。
您可以启用查询字符串查找(自 CakePHP 3.4 起可用):
echo $this->Form->create($article, [
'class' => 'start-end-date',
'type' => 'get'
'valueSources' => [
'query', // < add this _before_ the default `context` source
'context'
]
]);
这将使表单助手明确查找当前请求中的查询数据,或者您可以切换到使用 POST
表单,它将自动将数据提取为 POST
数据默认情况下由所有内置表单上下文(Array
、Entity
、Form
、Null
)查找,作为后备或作为主要来源。
另见