4

Im trying to create a custom HTML 5 date field for using in a laravel 4 framework view.

{{
    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.

how can i populate the value of this field?

AndrewMcLagan
  • 13,459
  • 23
  • 91
  • 158
  • Do you have any more code in your macro? Have a look at how the [FormBuilder](https://github.com/laravel/framework/blob/master/src/Illuminate/Html/FormBuilder.php#L197-219) already does this kind of thing. Also, blade tags are not the right place for macros to be defined. – Phill Sparks Apr 28 '13 at 08:42
  • Ya im aware blade tags are a bad place to define macros. Although im just tryin to get it working. I ended up using Form::input() as a workaround. i will come back and post more details in a little while. Thank you for your help though! :-) – AndrewMcLagan Apr 28 '13 at 10:05
  • 3
    Where would be the right place to define the macro? – darronz Apr 29 '13 at 07:24

5 Answers5

10

I added in app/start/global.php the following:

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.

dctucker
  • 679
  • 5
  • 12
6

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) }}
Bjorn Theart
  • 121
  • 4
4

Using a macro is not necessary. Just use Laravel's built-in Form::input method defining date as your desired input type:

{{ 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.

harryg
  • 23,311
  • 45
  • 125
  • 198
  • There is a reason to use macros... saying "don't use this" in a question that specific ask it, it's kind of pointless – Michel Ayres Mar 27 '15 at 20:13
  • My answer has clearly helped some people as evidenced by its upvotes. OP doesn't specifically say he must use a macro, but that he wants a date input, which is achievable without needing a macro. I'm not sure what your comment adds to the discussion. SO is not just about blindly answering questions but sometimes suggesting alternative approaches that haven't been thought of. – harryg Mar 30 '15 at 13:17
  • As you said, it helped some people, I didn't down-vote you because you aren't **wrong**, but I comment to point out a specific point. Don't be offended. Thanks for your help to the community and keep the good work. And as you said, SO nor any SE site is to blind do anything. I pointed a specific point to help others that reach your question. – Michel Ayres Mar 30 '15 at 13:42
3

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

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.

engma
  • 1,849
  • 2
  • 26
  • 55
2

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)
Victor Odiah
  • 1,061
  • 11
  • 14