4

我试图创建一个自定义 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') }}

唯一的问题是该值没有被填充,我不知道该怎么做。

我使用这个表单来创建和编辑一个名为 Event 的模型。

我怎样才能填充这个字段的值?

4

5 回答 5

10

我在 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;
});

但是“好方法”是扩展 Form 类并实现您的方法。

于 2013-08-19T21:45:42.830 回答
6

这是我所做的:

在我看来,我添加了以下宏

<?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) }}
于 2013-04-28T09:43:18.263 回答
4

不需要使用宏。只需使用 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')) }}

这似乎不在主文档中,但在上面链接的 API 文档中。

于 2014-06-18T11:49:41.487 回答
3

我找到了另一种方法,即将我的宏放在一个名为的文件中,macros.php然后将它与andapp/一起放在目录下,然后在最后添加以下行filters.phprouts.phpapp/start/global.php

require app_path().'/macros.php'; 

这将在应用程序启动后和视图构建之前加载您的宏。它接缝更整洁,并遵循 Laravel 的约定,因为这与 Laravel 用于加载filters.php文件的方式相同。

于 2014-04-09T12:53:39.640 回答
2

这对我有用:

Form::macro('date', function($name, $value = null, $options = array()) {
$attributes = HTML::attributes($options);
$input =  '<input type="date" name="' . $name . '" value="' . $value . '"'. $attributes.'>';
return $input;
});

而不是做

    foreach($options)

您可以使用

    HTML::attributes($options)
于 2014-06-18T22:47:49.313 回答