问题
I'd like to know if there is a way to customize the look of a submit button (to be an Image instead) in Laravel 3.
Currently, my submit button code looks like this :
{{ Form::open('project/delete', 'DELETE') }}
{{ Form::hidden('id', $project->id) }}
{{ Form::submit('Delete project', array('class'=>'btn')); }}
{{ Form::close() }}
And it's doing his job correctly. But I don't see how I could customize the submit button and put it as a bootstrap icon for example with ; <i class="icon-trash"></i>
I tried using :
{{ HTML::decode(HTML::link_to_route('project_delete', '<i class="icon-trash"></i>', array($project->id))); }}
But then I have a problem with my route/ function call.
回答1:
You can't use HTML for the value of an input
. If you tried <input type="submit" value='<i class="icon-trash"></i>'>
you would see it didn't work. Furthermore, using a link like your second approach won't work because it doesn't actually submit the form.
Your best bet would be to use a button.
<button type="submit"><i class="icon-trash"></i></button>
回答2:
You can't use HTML
class to generate a link in this way and it's (HTML
) been removed from L4
as best practice, it'll be easier if you use raw HTML
markup for this, though there are alternative ways like (bootstrapper, I didn't tried tho) for this in L3
but it's overwhelming in (IMO).
Check this forum link.
Alternatively you can use a custom macro, just create a new file (myMacros.php) in app\libraries
, it should be as app\libraries\myMacros.php
and put following code in this file
HTML::macro('link_nested', function($route, $title = null, $attributes = array(), $secure = null, $nested = null, $params = array())
{
$url = URL::to_route($route, $params, $secure);
$title = $title ?: $url;
if (empty($attributes)) {
$attributes = null;
}
return '<a href="'.$url.'"'.HTML::attributes($attributes).'>'.$nested.''.HTML::entities($title).'</a>';
});
Then, include it in your start.php
like
require path('app').'/libraries/myMacros.php';
Finally, use it ln your template like
HTML::link_nested('user.accountview', 'Delete', array('class'=>'btn'), '', '<i class="icon-trash"></i>', array($project->id));
For a submit
button add this in your myMacros.php
HTML::macro('submit_nested', function($title = null, $attributes = array(), $nested = null)
{
$title = $title ?: 'Submit';
if (empty($attributes)) {
$attributes = null;
}
return '<button type="submit" ' . HTML::attributes($attributes).'>' . $nested .' '. HTML::entities($title).'</button>';
});
Finally, use it like
HTML::submit_nested('Search', array('class'=>'someClass', 'name' => 'submit'), '<i class="icon-trash"></i>');
来源:https://stackoverflow.com/questions/18648329/laravel-image-submit-button