1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126:
<?php
/**
* MvcCore
*
* This source file is subject to the BSD 3 License
* For the full copyright and license information, please view
* the LICENSE.md file that are distributed with this source code.
*
* @copyright Copyright (c) 2016 Tom Flidr (https://github.com/mvccore)
* @license https://mvccore.github.io/docs/mvccore/5.0.0/LICENCE.md
*/
namespace MvcCore\Ext\Forms\Fields;
/**
* Responsibility: init, pre-dispatch and render `<input>` HTML element with
* type `checkbox`. `checkbox` field has it's own validator
* `SafeString` to clean string from base ASCII chars and
* some control chars by default. But validator `SafeString`
* doesn't prevent SQL injects and more.
*/
class Checkbox
extends \MvcCore\Ext\Forms\Field
implements \MvcCore\Ext\Forms\Fields\IVisibleField,
\MvcCore\Ext\Forms\Fields\ILabel,
\MvcCore\Ext\Forms\Fields\IChecked {
use \MvcCore\Ext\Forms\Field\Props\VisibleField;
use \MvcCore\Ext\Forms\Field\Props\Label;
use \MvcCore\Ext\Forms\Field\Props\Checked;
/**
* Possible values: `checkbox`.
* @var string
*/
protected $type = 'checkbox';
/**
* Render label on right side from `<input type="checkbox" />` element.
* @var string
*/
#protected $labelSide = \MvcCore\Ext\Forms\IField::LABEL_SIDE_RIGHT;
/**
* Validators:
* - `SafeString` - remove from submitted value base ASCII characters from 0 to 31 incl.
* (first column) and escape special characters: `& " ' < > | = \ %`.
* This validator is not prevent SQL inject attacks!
* @var string[]|\Closure[]
*/
protected $validators = ['SafeString'];
/**
* Standard field template strings for natural
* rendering - `control`, `togetherLabelLeft` and `togetherLabelRight`.
* @var string
*/
protected static $templates = [
'control' => '<input id="{id}" name="{name}" type="checkbox" value="{value}"{attrs} />',
'togetherLabelLeft' => '<label for="{id}"{attrs}><span>{label}</span>{control}</label>',
'togetherLabelRight'=> '<label for="{id}"{attrs}>{control}<span>{label}</span></label>',
];
/**
* Create new form control instance.
* @param array $cfg Config array with public properties and it's
* values which you want to configure, presented
* in camel case properties names syntax.
* @throws \InvalidArgumentException
* @return void
*/
public function __construct(array $cfg = []) {
$this->labelSide = \MvcCore\Ext\Forms\IField::LABEL_SIDE_RIGHT;
parent::__construct($cfg);
static::$templates = (object) array_merge(
(array) parent::$templates,
(array) self::$templates
);
}
/**
* This INTERNAL method is called from `\MvcCore\Ext\Form` just before
* field is naturally rendered. It sets up field for rendering process.
* Do not use this method event if you don't develop any form field.
* - Set up field render mode if not defined.
* - Translate label text if necessary.
* - Set up tab-index if necessary.
* @return void
*/
public function PreDispatch () {
parent::PreDispatch();
$this->preDispatchTabIndex();
}
/**
* This INTERNAL method is called from `\MvcCore\Ext\Forms\Field\Rendering`
* in rendering process. Do not use this method even if you don't develop any form field.
*
* Render control tag only without label or specific errors.
* @return string
*/
public function RenderControl () {
$attrsStr = $this->renderControlAttrsWithFieldVars();
if (!$this->form->GetFormTagRenderingStatus())
$attrsStr .= (strlen($attrsStr) > 0 ? ' ' : '')
. 'form="' . $this->form->GetId() . '"';
$viewClass = $this->form->GetViewClass();
if ($this->checked === NULL)
$this->checked = static::GetCheckedByValue($this->value);
$valueStr = htmlspecialchars_decode(htmlspecialchars($this->value, ENT_QUOTES), ENT_QUOTES);
if (!$valueStr)
$valueStr = 'true';
if ($this->checked)
$valueStr .= '" checked="checked';
/** @var $templates \stdClass */
$templates = static::$templates;
return $viewClass::Format($templates->control, [
'id' => $this->id,
'name' => $this->name,
'value' => $valueStr,
'attrs' => strlen($attrsStr) > 0 ? ' ' . $attrsStr : '',
]);
}
}