bootstrap 3 Form macro
Submitted by river -
등록 10 years ago - 수정 10 years ago
bootstrap 3
폼 마크업을 도와주는 폼 매크로.
매크로는 app/start/global.php
아래부분에 추가를 하면 되고, 따로 파일로 빼서 추가해도 된다.
출처: http://laravelsnippets.com/snippets/bootstrap-3-form-macros
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('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;
});
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);
}
Stats
-
0 likes
- 2515 views