Last active
August 29, 2015 14:01
-
-
Save Jakobud/31b35e82f7996ce272fd to your computer and use it in GitHub Desktop.
Laravel 4 Bootstrap 3 Form Macros
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Bootstrap 3 text field input | |
Form::macro('bootstrapText', function($name, $label=null, $value=null, $attr=array(), $help=null) | |
{ | |
$attr['id'] = isset($attr['id']) ? $attr['id'] : $name; | |
if ( !isset($attr['class']) ) | |
$attr['class'] = "form-control"; | |
else { | |
$array = explode(" ", $attr['class']); | |
$array[] = "form-control"; | |
$attr['class'] = implode(" ", $array); | |
} | |
$element = Form::text($name, Input::old($name, $value), $attr); | |
return Form::bootstrapFormGroup($name, $label, $element, $help); | |
}); | |
// Bootstrap 3 password input | |
Form::macro('bootstrapPassword', function($name, $label=null, $value=null, $attr=array(), $help=null) | |
{ | |
$attr['id'] = isset($attr['id']) ? $attr['id'] : $name; | |
if ( !isset($attr['class']) ) | |
$attr['class'] = "form-control"; | |
else { | |
$array = explode(" ", $attr['class']); | |
$array[] = "form-control"; | |
$attr['class'] = implode(" ", $array); | |
} | |
$element = Form::password($name, $attr); | |
return Form::bootstrapFormGroup($name, $label, $element, $help); | |
}); | |
// Bootstrap 3 static text | |
Form::macro('bootstrapStaticText', function($name, $label=null, $value=null, $attr=array(), $help=null) | |
{ | |
if ( !isset($attr['class']) ) | |
$attr['class'] = "form-control-static"; | |
else { | |
$array = explode(" ", $attr['class']); | |
$array[] = "form-control-static"; | |
$attr['class'] = implode(" ", $array); | |
} | |
// $element = Form::text($name, Input::old($name, $value), $attr); | |
$element = "<p class='".$attr['class']."'>$value</p>"; | |
return Form::bootstrapFormGroup($name, $label, $element, $help); | |
}); | |
// Bootstrap 3 text field input | |
Form::macro('bootstrapTextarea', function($name, $label=null, $value=null, $attr=array(), $help=null) | |
{ | |
$attr['id'] = isset($attr['id']) ? $attr['id'] : $name; | |
if ( !isset($attr['class']) ) | |
$attr['class'] = "form-control"; | |
else { | |
$array = explode(" ", $attr['class']); | |
$array[] = "form-control"; | |
$attr['class'] = implode(" ", $array); | |
} | |
$element = Form::textarea($name, Input::old($name, $value), $attr); | |
return Form::bootstrapFormGroup($name, $label, $element, $help); | |
}); | |
// Boostrap 3 checkbox input | |
Form::macro('bootstrapCheckbox', function($name, $label=null, $checked=false, $attr=array(), $help=null) | |
{ | |
$label = is_null($label) ? ucwords(str_replace(array('-', '_'), ' ', $name)) : $label; | |
$attr['id'] = isset($attr['id']) ? $attr['id'] : $name; | |
$out = "<div class='checkbox'><label>"; | |
$out .= Form::checkbox($name, null, $checked, $attr); | |
$out .= "$label</label></div>"; | |
if ( !is_null($help) ) | |
$out .= "<span class='help-block'><small>$help</small></span>"; | |
return $out; | |
}); | |
// Bootstrap 3 select dropdown | |
Form::macro('bootstrapSelect', function($name, $label=null, $options=array(), $selected=null, $attr=array(), $help=null) | |
{ | |
$label = is_null($label) ? ucwords(str_replace(array('-', '_'), ' ', $name)) : $label; | |
$attr['id'] = isset($attr['id']) ? $attr['id'] : $name; | |
if ( !isset($attr['class']) ) | |
$attr['class'] = "form-control"; | |
else { | |
$array = explode(" ", $attr['class']); | |
$array[] = "form-control"; | |
$attr['class'] = implode(" ", $array); | |
} | |
$element = Form::select($name, $options, $selected, $attr); | |
return Form::bootstrapFormGroup($name, $label, $element, $help); | |
}); | |
// Bootstrap 3 form group wrapper | |
Form::macro('bootstrapFormGroup', function($name, $label=null, $element, $help=null) | |
{ | |
$label = is_null($label) ? ucwords(str_replace(array('-', '_'), ' ', $name)) : $label; | |
$errors = View::shared('errors'); | |
$class = 'form-group'; | |
$feedback = ""; | |
if ( $errors->has($name) ) { | |
$class.=' has-error has-feedback'; | |
$feedback = "<span class='glyphicon glyphicon-remove form-control-feedback'></span>"; | |
$help = $errors->first($name); | |
} | |
$out = "<div class='$class'>"; | |
$out .= "<label class='control-label' for='$name'>$label</label>"; | |
$out .= $element; | |
$out .= $feedback; | |
if ( !is_null($help) ) | |
$out .= "<span class='help-block'><small>$help</small></span>"; | |
$out .= "</div>"; | |
return $out; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment