如何设置由表单助手生成的日期时间输入的自定义名称? [英] How to set custom names for datetime inputs generated by the form helper?

查看:137
本文介绍了如何设置由表单助手生成的日期时间输入的自定义名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您做了什么



  echo $ this-> Form-> create ,[
'type'=>'get',
'url'=> ['controller'=>'Holidays','action'=>'calendar']
]);
echo $ this-> Form-> month('month',['empty'=>'Select month','value'=> @ $ month]
echo $ this-> Form-> year('year',['minYear'=> date('Y'),'maxYear'=> 2020,'empty'=>'Select year','value'=> @ $ year]);



预期行为



当提交表单时,我生成的网址将显示为 example.com/holiday/calendar?month=03&year=2016



实际行为



example.com/holidays/calendar?month%5Bmonth%5D=03&year%5Byear%5D=2016



第一个想法



所以第一个想法是使用 name 选项。



echo $ this-> Form-> month ['name'=>'foo','empty'=>'Select month','value'=> @ $ month]);



这将产生< select name =foo [month].. ,它有相同的问题。



第二个想法



自定义表单输入模板。这不允许自定义名称令牌,因为它传递为 {{name}} 所以,当它到模板的时间太晚了。



第三个想法



使用数组并将字段命名为相同。



< pre class =lang-php prettyprint-override> echo $ this-> Form-> month('month',['name'=>'when','empty'=> ;'Select month','value'=> @ $ month]);
echo $ this-> Form-> year('year',['name'=>'when','minYear'=> date('Y'),'maxYear'=> 2020,'empty'=>'选择年','值'=> @ $年]);

然后我会期望 example.com/holidays/calendar?when [month ] = 03& when [year] = 2016 ,但是当%5Bmonear%5D = 02时,你实际上得到 / holidays / calendar? code>



结论



不幸的是有三种不同的方法,这个用例。如果有人知道如何自定义表单助手生成的html名称属性,或者防止获取表单被转义,我会感兴趣,以便我的网址看起来不那么乱。

解决方案

网址是否显示编码,取决于您的浏览器。



日期窗口小部件使用嵌套名称,因此日期/时间解析器的日期/时间列接收一切它需要从中解析和构造对象。



由于日期控件当前的工作方式,没有办法改变名称的构建方式。你可以做的是,例如使用自定义的 select 模板的输入,或创建一个 select 自己需要的选项。



您可能尝试过使用全局模板,这就是为什么它失败。你需要使用本地的,因此你必须使用 FormHelper :: input()才能让表单助手使用它们,例如

  echo $ this-> Form-> input('month',[
'type'=>'month'
'empty'=>'选择月份',
'value'=> @ $ month,
'templates'=> [
'select'=> '< select name =month{{attrs}}> {{content}}< / select>'
]
]
echo $ this-> Form-> input('year',[
'type'=>'year',
'minYear'=& ),
'maxYear'=> 2020,
'empty'=>'选择年份',
'value'=> @ $ year,
' => [
'select'=>'< select name =year{{attrs}}> {{content}}< / select>'
]
]);


What you did

echo $this->Form->create(null, [
    'type' => 'get', 
    'url' => ['controller' => 'Holidays', 'action' => 'calendar']
]);
echo $this->Form->month('month', ['empty' => 'Select month', 'value' => @$month]);
echo $this->Form->year('year', ['minYear' => date('Y'), 'maxYear' => 2020, 'empty' => 'Select year', 'value' => @$year]);

Expected Behavior

I was expecting that when a submit the form my resulting url would look like example.com/holiday/calendar?month=03&year=2016

Actual Behavior

example.com/holidays/calendar?month%5Bmonth%5D=03&year%5Byear%5D=2016

First thought

So the first thought is to use the name option.

echo $this->Form->month('month', ['name' => 'foo', 'empty' => 'Select month', 'value' => @$month]);

Which will produce <select name="foo[month]".. which has the same issue.

Second thought

Customise the form input template. This doesn't allow customisation of the name token as it's passed as {{name}} so by the time it's got to the template it's too late.

Third thought

Use the array and name the fields the same.

echo $this->Form->month('month', ['name' =>'when', 'empty' => 'Select month', 'value' => @$month]);
echo $this->Form->year('year', ['name' => 'when', 'minYear' => date('Y'), 'maxYear' => 2020, 'empty' => 'Select year', 'value' => @$year]);

I would then expect example.com/holidays/calendar?when[month]=03&when[year]=2016, but you actually get /holidays/calendar?when%5Bmonth%5D=02&when%5Byear%5D=2016

Conclusion

So unfortunately with three different approaches I've not been able to solve this use-case. I'd be interested if anyone knows how to customise the html name attribute generated by the form helper, or prevent get forms from being escaped, so that my urls don't look so messy.

解决方案

Whether the URL is being displayed encoded or not, depends on your browser. Firefox will for example display it unencoded, while Chromes shows it encoded.

The date widget uses nested names, so that the date/time parser for date/time columns receives everything it needs for parsing and constructing objects from it.

With how the date widget currently works, there is no way to change the way the name is built. What you could do is for example using a custom select template for that input, or create a select type input with the necessary options on your own.

You've probably tried the former with global templates, which is why it failed. You need to use local ones, and consequently you'll have to use FormHelper::input() in order for the form helper to use them, like

echo $this->Form->input('month', [
    'type' => 'month',
    'empty' => 'Select month',
    'value' => @$month,
    'templates' => [
        'select' => '<select name="month"{{attrs}}>{{content}}</select>'
    ]
]);
echo $this->Form->input('year', [
    'type' => 'year',
    'minYear' => date('Y'),
    'maxYear' => 2020,
    'empty' => 'Select year',
    'value' => @$year,
    'templates' => [
        'select' => '<select name="year"{{attrs}}>{{content}}</select>'
    ]
]);

这篇关于如何设置由表单助手生成的日期时间输入的自定义名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆