Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
cakephp3:要加载的FormHelper<;标签>;之后<;输入>;?_Cakephp_Cakephp 3.0 - Fatal编程技术网

cakephp3:要加载的FormHelper<;标签>;之后<;输入>;?

cakephp3:要加载的FormHelper<;标签>;之后<;输入>;?,cakephp,cakephp-3.0,Cakephp,Cakephp 3.0,有没有办法在之后加载。默认情况下,CakePHP在之前加载,如下所示: <input name="email" placeholder="Enter your email" id="email" type="email"> <label for="email">Email</label> <?php echo $this->Form->input('email', ['placeholder' => 'Email', 'label'

有没有办法在
之后加载
。默认情况下,CakePHP在
之前加载
,如下所示:

<input name="email" placeholder="Enter your email" id="email" type="email">
<label for="email">Email</label>
<?php
echo $this->Form->input('email', ['placeholder' => 'Email', 'label' => false]);
echo $this->Form->label('email', 'Email');
?>
Php代码:

<?php echo $this->Form->input('email', ['placeholder' => 'Email']); ?>

生成的代码:

<label for="email">Email</label>
<input name="email" placeholder="Enter your email" id="email" type="email">
电子邮件
但当我想得到这样的结果时,我该怎么办:

<input name="email" placeholder="Enter your email" id="email" type="email">
<label for="email">Email</label>
<?php
echo $this->Form->input('email', ['placeholder' => 'Email', 'label' => false]);
echo $this->Form->label('email', 'Email');
?>

电子邮件
除了黑客攻击之外,还有其他方法吗?

您必须使用
FormHelper
的方法,如下所示:

<input name="email" placeholder="Enter your email" id="email" type="email">
<label for="email">Email</label>
<?php
echo $this->Form->input('email', ['placeholder' => 'Email', 'label' => false]);
echo $this->Form->label('email', 'Email');
?>

另一种方法是创建自定义帮助程序,并使用@Choma建议的方式覆盖现有的帮助程序

因此,您必须添加您的

//View/Helpers/MyCustomFormHelper.php

App::uses('FormHelper', 'View/Helper');

MyCustomFormHelper extends FormHelper{

    public function input($fieldName, $options) {
        $defaults = array_merge($options, array('label' => false))
        echo parent::input($fieldName, $defaults);
        echo parent::label(fieldName);
    }
}

//AppController.php

class AppController extends Controller {

    public $helpers = array(
        'Form' => array(
            'className' => 'MyCustomFormHelper'
         )
    );
}
或者,您也可以使用after选项,以便在自己的助手中更轻松地再次执行此操作

//View/Helpers/MyCustomFormHelper.php

App::uses('FormHelper', 'View/Helper');

MyCustomFormHelper extends FormHelper{

    public function input($fieldName, $options) {
        $after = parent::label(fieldName);
        $defaults = array_merge($options, array(
            'label' => false,
            'after' =>  $after,
        ));
        return parent::input($fieldName, $defaults);
    }

    //or do not overwrite the input function and use a custom one
    public function inputLabelAfter($fieldName, $options) {
    ...
    ...
    }

}
我很抱歉,如果代码有错误,我还没有测试它,但我认为你得到了图片,给了你更多的想法

编辑:我没有看到这是针对CakePHP3.x的。上面的例子适用于2.x,但我相信它可以。

Templates 您可以使用模板功能,负责此功能的模板是
formGroup
one,默认情况下为
{{label}}{{input}

echo$this->Form->input('email')[
'占位符'=>'电子邮件',
“模板”=>[
'formGroup'=>'{{input}}{{label}}'
]
]);
还可以按照类型名称的命名约定,后跟
FormGroup
,为每个输入类型定义表单组临时值。因此,对于
电子邮件
类型,应该是
emailFormGroup

echo $this->Form->create(null, [
    'templates' => [
        'emailFormGroup' => '{{input}}{{label}}',
        'textFormGroup' => '{{input}}{{label}}'
        // ...
    ]
]);
这样,您可以轻松地将此选项全局应用于特定类型

另请参见

CSS 当然,你也可以使用CSS,有表格

.input.email{
显示:表格;
宽度:100%;
}
.input.email输入{
显示:表头组;
}
.input.email标签{
显示:表尾组;
}
软盒订购

.input.email{
显示器:flex;
柔性流动:柱;
}
.input.email输入{
顺序:1;
}
.input.email标签{
顺序:2;
}

等等。

使用模板是正确的方法-我希望这有助于

 $this->Form->templates([
        'nestingLabel' => '{{input}}<label{{attrs}}>{{text}}</label>',
        'formGroup' => '{{input}}{{label}}',
 ]);
$this->表单->模板([
'nestingLabel'=>'{{input}}{{text}}',
'formGroup'=>'{{input}}{{label}}',
]);

是的。我不想采用这种方法,因为我想要
input
来处理它,因为我有一个
来将它们包装在一起,它来自于为inputContainer设置
表单模板。我只是希望在
标签上有一个
选项
,我可以像
之后
之前
那样激活该选项,该选项将在
应该放置的位置激活。无论如何,我会尝试一下……我不知道你到底在看什么,但是如果你只需要在标签前看到输入,也许你可以用css来做。我会采用你建议的方法。我已经开始创建一个
FormEx
,但没有这样做。我一定要拿这个试试。。谢谢我将发布backTemplating
formGroup
正是我想要的。。谢谢