Skip to content

Instantly share code, notes, and snippets.

@Jakobud
Last active August 29, 2015 14:01
Show Gist options
  • Save Jakobud/715970aa2b08d54b340d to your computer and use it in GitHub Desktop.
Save Jakobud/715970aa2b08d54b340d to your computer and use it in GitHub Desktop.
Laravel 4 Foundation 5 Form Macros
<?php
// Foundation 5 form label element wrapper
Form::macro('foundationLabelWrapper', function($name, $label = null, $element)
{
$label = is_null($label) ? ucwords(str_replace(array('-', '_'), ' ', $name)) : $label;
$errors = View::shared('errors');
$out = '';
if ( $errors->has($name) )
$out .= "<label class='error'>";
else
$out .= "<label>";
$out .= $label;
$out .= $element;
$out .= "</label>";
if ( $errors->has($name) )
$out .= "<small class='error'>".$errors->first($name)."</small>";
return $out;
});
// Foundation 5 text field
Form::macro('foundationText', function($name, $label=null, $value=null, $attributes=array())
{
$attributes['id'] = $name;
$element = Form::text($name, Input::old($name, $value), $attributes);
return Form::foundationLabelWrapper($name, $label, $element);
});
// Foundation 5 password field
Form::macro('foundationPassword', function($name, $label=null, $attributes=array())
{
$attributes['id'] = $name;
$element = Form::password($name, $attributes);
return Form::foundationLabelWrapper($name, $label, $element);
});
// Foundation 5 checkbox field
Form::macro('foundationCheckbox', function($name, $label=null, $checked=false, $attributes=array())
{
$label = is_null($label) ? ucwords(str_replace(array('-', '_'), ' ', $name)) : $label;
$attributes['id'] = $name;
$out = Form::checkbox($name, null, $checked, $attributes);
$out .= "<label for='$name'>$label</label>";
return $out;
});
Form::macro('foundationSelect', function($name, $label=null, $options=array(), $selected=null, $attributes=array())
{
$label = is_null($label) ? ucwords(str_replace(array('-', '_'), ' ', $name)) : $label;
$attributes['id'] = $name;
$element = Form::select($name, $options, $selected, $attributes);
return Form::foundationLabelWrapper($name, $label, $element);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment