Cakephp 3 输入自动标签

Cakephp 3 input auto label

我的 indext.ctp 中有一个代码:

<?php echo $this->Form->input('full_name'); ?>

它给了我

标签名为Full Name我的目标是Full name

我知道我可以使用:

<?php echo $this->Form->input('full_name', ['label'=>'Full name']); ?>

我的问题是:我可以在全球范围内进行吗?以某种方式将自动生成标签中使用的 ucwords(); 覆盖为 ucfirst(); ?

Cakephp 生成标签文本(未提供时)here

它使用Inflector::Humanize()(见manual

我猜你可以覆盖默认助手(记住 input() 已被弃用,你应该改用 control()

class MyFormHelper extends FormHelper
{

    public function control($fieldName, array $options = [])
    {
        if(!isset($options['label']))
            $options['label'] = // you own code here;
        return parent::control($fieldName, $options);
    }
}

然后在您的 AppView.php initialize() 中加载您的助手

$this->loadHelper('Form', [
    'className' => 'MyForm',
]);

因此,当您想要定义自定义标签时,您可以使用 'label' 选项

<?php echo $this->Form->input('full_name', ['label'=>'Insert the full name here']); ?>

相反,如果您不设置 'label' 选项

<?php echo $this->Form->input('full_name'); ?>

帮助者将使用您的逻辑

我测试了该行为,它在我的表单中有效