Laravel 4 表单生成器自定义字段宏
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16259488/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 Form builder Custom Fields Macro
提问by AndrewMcLagan
Im trying to create a custom HTML 5 date field for using in a laravel 4 framework view.
我正在尝试创建一个自定义的 HTML 5 日期字段,以便在 laravel 4 框架视图中使用。
{{
Form::macro('datetime', function($field_name)
{
return '';
});
}}
{{ Form::label('event_start', 'Event Date', array('class' => 'control-label')) }}
{{ Form::datetime('event_start') }}
The only problem is the value is not being populated, and i do not know how to do this.
唯一的问题是值没有被填充,我不知道如何做到这一点。
Im using this form to create and edit a model called Event.
我使用这个表单来创建和编辑一个名为 Event 的模型。
how can i populate the value of this field?
我怎样才能填充这个字段的值?
回答by Rafa Gómez Casas
I added in app/start/global.php the following:
我在 app/start/global.php 中添加了以下内容:
Form::macro('date', function($name, $value = null, $options = array()) {
$input = '<input type="date" name="' . $name . '" value="' . $value . '"';
foreach ($options as $key => $value) {
$input .= ' ' . $key . '="' . $value . '"';
}
$input .= '>';
return $input;
});
But the "good way" would be to extend the Form class and implement your methods.
但是“好方法”是扩展 Form 类并实现您的方法。
回答by Bjorn Theart
Here's what I did:
这是我所做的:
in my view I added the following macro
在我看来,我添加了以下宏
<?php
Form::macro('datetime', function($value) {
return '<input type="datetime" name="my_custom_datetime_field" value="'.$value.'"/>';
});
...
...
// here's how I use the macro and pass a value to it
{{ Form::datetime($datetime) }}
回答by harryg
Using a macro is not necessary. Just use Laravel's built-in Form::input
method defining date
as your desired input type:
不需要使用宏。只需使用 Laravel 的内置Form::input
方法定义date
为您想要的输入类型:
{{ Form::label('event_start', 'Event Date', array('class' => 'control-label')) }}
{{ Form::input('date', 'event_start', $default_value, array('class'=>'form-control')) }}
This appears not to be in the main docs but is in the API docs as linked above.
这似乎不在主文档中,而是在上面链接的 API 文档中。
回答by engma
I've found another way to do this which is putting my macros in a file named macros.php
then place it under app/
directory along with filters.php
and routs.php
, then in the app/start/global.php
I added the following line at the end
我找到了另一种方法来做到这一点,即将我的宏放在一个名为的文件中,macros.php
然后将它app/
与filters.php
and一起放在目录下routs.php
,然后在最后app/start/global.php
添加以下行
require app_path().'/macros.php';
this will load your macros after the app has started and before the view is constructed.
it seamed neater and following the Laravel's convention because this is the same way Laravel uses to load the filters.php
file.
这将在应用程序启动之后和视图构建之前加载您的宏。它缝合得更整齐并遵循 Laravel 的约定,因为这与 Laravel 用来加载filters.php
文件的方式相同。
回答by Victor Odiah
this works for me:
这对我有用:
Form::macro('date', function($name, $value = null, $options = array()) {
$attributes = HTML::attributes($options);
$input = '<input type="date" name="' . $name . '" value="' . $value . '"'. $attributes.'>';
return $input;
});
instead of doing
而不是做
foreach($options)
you can use
您可以使用
HTML::attributes($options)