/app/macros.php
PHP | 125 lines | 94 code | 31 blank | 0 comment | 3 complexity | 31e3ae098d1318e74f8f01881d66116b MD5 | raw file
- <?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('selectMultipleField', function($name, $label = null, $options, $value = null, $attributes = array())
- {
- $attributes = array_merge($attributes, array('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(array('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 = Input::old('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(array('class' => 'form-control', 'id' => 'id-field-' . $name), $attributes);
- }
- Validator::extend('time24', function($attribute, $value, $parameters)
- {
- return preg_match('/(2[0-3]|[0-1]?[0-9]):[0-5]?[0-9](:[0-5]?[0-9])?/', $value);
- });
- Validator::extend('phone_ru', function($attribute, $value, $parameters)
- {
- return preg_match('/^\+7 \(\d{3}\) \d{3}\-\d{4}( x\d{1,6})?$/', $value);
- });
- Validator::extend('site', function($attribute, $value, $parameters)
- {
- return preg_match('/^(((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|\d|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.)+(([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])*([a-z]|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)|[\x{E000}-\x{F8FF}]|\/|\?)*)?(#((([a-z]|\d|-|\.|_|~|[\x{00A0}-\x{D7FF}\x{F900}-\x{FDCF}\x{FDF0}-\x{FFEF}])|(%[\da-f]{2})|[!\$&\'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/ui', $value);
- });
- Validator::extend('ceil', function($attribute, $value, $parameters)
- {
- return $value % $parameters[0] == 0;
- });
- Password::validator(function($credentials)
- {
- return strlen($credentials['password']) >= 4;
- });
- Str::macro('prep_input', function($str) {
- return strip_tags(trim(strval($str)));
- });
- Str::macro('ucfirst', function($str, $enc = 'utf-8') {
- return mb_strtoupper(mb_substr($str, 0, 1, $enc), $enc).mb_substr($str, 1, mb_strlen($str, $enc), $enc);
- });