Laravel 4 Form builder Custom Fields Macro
Asked Answered
S

5

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?

Slideaction answered 28/4, 2013 at 4:4 Comment(3)
Do you have any more code in your macro? Have a look at how the FormBuilder already does this kind of thing. Also, blade tags are not the right place for macros to be defined.Floaty
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! :-)Slideaction
Where would be the right place to define the macro?Doublebreasted
K
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) }}
Kazak answered 28/4, 2013 at 9:43 Comment(0)
A
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.

Amorous answered 19/8, 2013 at 21:45 Comment(0)
K
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) }}
Kazak answered 28/4, 2013 at 9:43 Comment(0)
A
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.

Asceticism answered 18/6, 2014 at 11:49 Comment(3)
There is a reason to use macros... saying "don't use this" in a question that specific ask it, it's kind of pointlessEngineman
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.Asceticism
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.Engineman
M
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.

Modicum answered 9/4, 2014 at 12:53 Comment(0)
V
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)
Vries answered 18/6, 2014 at 22:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.