Laravel 4 Form Creator Custom Fields Macro

I am trying to create a custom HTML 5 date field for use in a laravel 4 structure 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 that the value is not populated, and I don't know how to do it.

Im using this form to create and edit a model called Event.

How can I fill in the value of this field?

+4
source share
5 answers

Here is what I did:

in my opinion i added the following macro

 <?php Form::macro('datetime', function($value) { return '<input type="datetime" name="my_custom_datetime_field" value="'.$value.'"/>'; }); ... ... // here how I use the macro and pass a value to it {{ Form::datetime($datetime) }} 
+6
source

I added the following to 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โ€ is to extend the Form class and implement your methods.

+10
source

Using a macro is not required. Just use the Laravel built-in Form::input method, defining date as the 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 does not seem to be part of the main docs, but is in the API docs as above.

+4
source

I found another way to do this, which places my macros in a file called macros.php , then puts it in the app/ directory along with filters.php and routs.php , and then in app/start/global.php I added the following line in the end

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

this will load your macros after starting the application and before the view is built. it smoothes more accurately and follows the Laravel convention because it is the same that Laravel uses to load the filters.php file.

+3
source

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) 
+2
source

All Articles