Current Path : /data/web/virtuals/51568/virtual/www/subdom/srps/src/form/ |
Current File : /data/web/virtuals/51568/virtual/www/subdom/srps/src/form/CheckboxField.php |
<?php namespace app\form; use app\Model; use app\Application; class CheckboxField { public const BOX_CHECKED = true; public const BOX_UNCHECKED = false; public $model; public $attribute; public $state; public function __construct(Model $model, string $attribute, bool $state) { $this->model = $model; $this->attribute = $attribute; $this->state = $state; } public function __toString(): string { if (Application::$app->request->getMethod() === 'get') { $checked = $this->state; } else { $checked = !empty($this->model->{$this->attribute}); } return sprintf('<div id="field-%s" class="form-check"> <input type="checkbox" id="%s" name="%s" value="1" class="form-check-input rounded-0 %s" %s> <label class="form-check-label" for="%s"> %s </label> <div id="invalid-%s" class="invalid-feedback">%s</div> </div>', $this->attribute, $this->attribute, $this->attribute, $this->model->hasError($this->attribute) ? 'is-invalid' : '', $checked ? "checked" : "", $this->attribute, $this->model->getLabel($this->attribute), $this->attribute, $this->model->getFirstError($this->attribute) ); } }