Created
January 16, 2014 05:04
-
-
Save JacobBennett/8450065 to your computer and use it in GitHub Desktop.
Laravel FormMacros
This file contains 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 | |
Form::macro('textField', function($name, $label = null, $value = null, $attributes = array()) | |
{ | |
$element = Form::text($name, $value, fieldAttributes($name, $attributes)); | |
return fieldWrapper($name, $label, $element); | |
}); | |
Form::macro('passwordField', function($name, $label = null, $attributes = array()) | |
{ | |
$element = Form::password($name, fieldAttributes($name, $attributes)); | |
return fieldWrapper($name, $label, $element); | |
}); | |
Form::macro('textareaField', function($name, $label = null, $value = null, $attributes = array()) | |
{ | |
$element = Form::textarea($name, $value, fieldAttributes($name, $attributes)); | |
return fieldWrapper($name, $label, $element); | |
}); | |
Form::macro('selectField', function($name, $label = null, $options, $value = null, $attributes = array()) | |
{ | |
$element = Form::select($name, $options, $value, fieldAttributes($name, $attributes)); | |
return fieldWrapper($name, $label, $element); | |
}); | |
Form::macro('selectState', function($name, $label = null, $value = null, $attributes = array()) | |
{ | |
$states = array("" => "Please Select", "AL" => "Alabama", "AK" => "Alaska", "AZ" => "Arizona", "AR" => "Arkansas", "CA" => "California", "CO" => "Colorado", "CT" => "Connecticut", "DE" => "Delaware", "DC" => "District Of Columbia", "FL" => "Florida", "GA" => "Georgia", "HI" => "Hawaii", "ID" => "Idaho", "IL" => "Illinois", "IN" => "Indiana", "IA" => "Iowa", "KS" => "Kansas", "KY" => "Kentucky", "LA" => "Louisiana", "ME" => "Maine", "MD" => "Maryland", "MA" => "Massachusetts", "MI" => "Michigan", "MN" => "Minnesota", "MS" => "Mississippi", "MO" => "Missouri", "MT" => "Montana", "NE" => "Nebraska", "NV" => "Nevada", "NH" => "New Hampshire", "NJ" => "New Jersey", "NM" => "New Mexico", "NY" => "New York", "NC" => "North Carolina", "ND" => "North Dakota", "OH" => "Ohio", "OK" => "Oklahoma", "OR" => "Oregon", "PA" => "Pennsylvania", "RI" => "Rhode Island", "SC" => "South Carolina", "SD" => "South Dakota", "TN" => "Tennessee", "TX" => "Texas", "UT" => "Utah", "VT" => "Vermont", "VA" => "Virginia", "WA" => "Washington", "WV" => "West Virginia", "WI" => "Wisconsin", "WY" => "Wyoming"); | |
$element = Form::select($name, $states, $value, fieldAttributes($name, $attributes)); | |
return fieldWrapper($name, $label, $element); | |
}); | |
Form::macro('selectMultipleField', function($name, $label = null, $options, $value = null, $attributes = array()) | |
{ | |
$attributes = array_merge($attributes, ['multiple' => true]); | |
$element = Form::select($name, $options, $value, fieldAttributes($name, $attributes)); | |
return fieldWrapper($name, $label, $element); | |
}); | |
Form::macro('checkboxField', function($name, $label = null, $value = 1, $checked = null, $attributes = array()) | |
{ | |
$attributes = array_merge(['id' => 'id-field-' . $name], $attributes); | |
$out = '<div class="checkbox'; | |
$out .= fieldError($name) . '">'; | |
$out .= '<label>'; | |
$out .= Form::checkbox($name, $value, $checked, $attributes) . ' ' . $label; | |
$out .= '</div>'; | |
return $out; | |
}); | |
Form::macro('radioField', function($name, $label = null, $value = 1, $checked = null, $attributes = array()) | |
{ | |
$attributes = array_merge(['id' => 'id-field-' . $name], $attributes); | |
$out = '<div class="radio'; | |
$out .= fieldError($name) . '">'; | |
$out .= '<label>'; | |
$out .= Form::radio($name, $value, $checked, $attributes) . ' ' . $label; | |
$out .= '</div>'; | |
return $out; | |
}); | |
function fieldWrapper($name, $label, $element) | |
{ | |
$out = '<div class="form-group'; | |
$out .= fieldError($name) . '">'; | |
$out .= fieldLabel($name, $label); | |
$out .= $element; | |
$out .= '</div>'; | |
return $out; | |
} | |
function fieldError($field) | |
{ | |
$error = ''; | |
if ($errors = Session::get('errors')) | |
{ | |
$error = $errors->first($field) ? ' has-error' : ''; | |
} | |
return $error; | |
} | |
function fieldLabel($name, $label) | |
{ | |
if (is_null($label)) return ''; | |
$name = str_replace('[]', '', $name); | |
$out = '<label for="id-field-' . $name . '" class="control-label">'; | |
$out .= $label . '</label>'; | |
return $out; | |
} | |
function fieldAttributes($name, $attributes = array()) | |
{ | |
$name = str_replace('[]', '', $name); | |
return array_merge(['class' => 'form-control', 'id' => 'id-field-' . $name], $attributes); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment