Support the ongoing development of Laravel.io →
Security Forms Validation

Hi guys, I have a problem with my own Macros.

I made my own file that is call (/app/macros/macros.php).

This is the code inside:

<?php

/*
 * BUTTON SUBMIT
 */
Form::macro('submit_ajax_icon', function($dom_id,$label,$icon='',$btn_class='btn btn-primary')
{
	
	if($icon!='')
	{
		$icon = '<i class="'.$icon.'"></i> ';
	}
	return '<button type="submit" class="'.$btn_class .' ladda-button"
					data-style="expand-right"
					data-size="medium"
					data-spinner-size="40"
					data-spinner-color="#ff0000">
					<span class="ladda-label">'.$label.'</span>
					</button>';
});


/*
 * INPUT LEFT ICON
 */
Form::macro('input_left_icon',function($dom_id,$value='',$icon,$aAttributes=array(),$type='')
{
	$attributes = '';
	
	if(count($aAttributes)>0)
	{
		foreach($aAttributes as $attribute => $attribute_value)
		{
			$attributes .= $attribute.'="'.$attribute_value.'"';
		}
	}
	
	return '<div class="input-group">
	          <div class="input-group-addon"> <i class="'.$icon.'"></i></div>
	          <input id="'.$dom_id.'" name="'.$dom_id.'" type="'.($type!=''?$type:'text').'" value="'.($value!=''?$value:'').'" '.$attributes.' class="form-control">
	        </div>';
});

/*
 * SELECT LEFT ICON
 */
Form::macro('select_left_icon',function($dom_id,$aValues,$oldValue='',$icon,$placeholder='')
{
	$select= '<div class="input-group">
	          <div class="input-group-addon"> <i class="'.$icon.'"></i></div>
	          <select class="form-control" id="'.$dom_id.'" name="'.$dom_id.'">';
			  
			  if( ($placeholder!='') && ($oldValue=='') )
			  {
					$select.='<option value="" selected="selected">'.$placeholder.'</option>';
			  }
				
			  foreach($aValues as $key => $value)
			  {
			  	
			  	if( ($oldValue!='') && ($oldValue==$key) )
				{
					$class='selected="selected"';
				} 
				else
				{
					$class = '';
				}
	
				
			  	$select.='<option value="'.$key.'" '.$class.'>'.$value.'</option>';
			  }  
			  
	          	
	$select.= '</select>
	         </div>';
			
	return $select;
});

I included the macro file from /app/bootstrap/start.php just before return $app.

The problem is that when I include my own macro file, {{ Form::open() }} doesn't generate crsf token, it only create an empty input hidden.

<input type="hidden" name="_token">

I tried to change the include position in start.php file but It doesn`t work.

Just one more thing, Macro works perfectly.

Coul anyone help me please?

Kind regards.

Last updated 2 years ago.
0

Just another questio, How to populate my own macros automatically with Form::model? Best regards

Last updated 2 years ago.
0

Did you use

{{ Form::token() }}

??

Last updated 2 years ago.
0

Yes I used It, but the problem only occurs when I included my own Macros

Last updated 2 years ago.
0

Did you ever figure this one out?

Last updated 2 years ago.
0

Happen to have the same problem. Using Laravel 5 and having the form macros in my routes.php file, which I required. Routes are working, but the value of the csrf input field is empty. When I delete the form macros everything is working again.

Last updated 2 years ago.
0

This is how you fix it. Macros must be loaded after HTMLServiceProvider, that is why csrf tokens are not generating. After this, just add the service provider to your config/app.php. You can remove HTMLServiceProvider from the list since we are loading it here.


<?php namespace App\Providers;

use Illuminate\Html\HtmlServiceProvider;

class MacroServiceProvider extends HtmlServiceProvider
{
	/**
	 * Register the application services.
	 *
	 * @return void
	 */
	public function register()
	{
        // Macros must be loaded after the HTMLServiceProvider's
        // register method is called. Otherwise, csrf tokens
        // will not be generated
		parent::register();

        // Load macros
        require base_path() . '/path/to/your//macros.php';
	}
}

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

sgsrra sgsrra Joined 15 Aug 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.