Laravel 4表单构建器自定义字段宏

And*_*gan 4 php frameworks php-5.3 laravel laravel-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') }}
Run Code Online (Sandbox Code Playgroud)

唯一的问题是价值没有被填充,我不知道如何做到这一点.

我正在使用此表单来创建和编辑名为Event的模型.

我怎样才能填充这个领域的价值?

小智 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;
});
Run Code Online (Sandbox Code Playgroud)

但"好方法"是扩展Form类并实现您的方法.


Bjo*_*art 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) }}
Run Code Online (Sandbox Code Playgroud)