Current Path : /data/web/virtuals/51568/virtual/www/subdom/srps/src/form/ |
Current File : /data/web/virtuals/51568/virtual/www/subdom/srps/src/form/SelectField.php |
<?php namespace app\form; use app\Model; use app\Application; class SelectField { public $model; public $attribute; public $select = []; public $value; public function __construct(Model $model, string $attribute, array $select, string $value) { $this->model = $model; $this->attribute = $attribute; $this->select = $select; $this->value = $value; } public function __toString(): string { $selectField = sprintf('<div id="field-%s" class="form-group mb-2"> <label>%s</label> <select id="%s" name="%s" class="form-select rounded-0 %s" > <option value="" disabled selected hidden>... zvol položku ...</option> ', $this->attribute, $this->model->getLabel($this->attribute), $this->attribute, $this->attribute, $this->model->hasError($this->attribute) ? 'is-invalid' : ''); foreach ($this->select as $select) { if (Application::$app->request->getMethod() === 'get') { $selected = $this->value == $select['value']; } else { $selected = $this->model->{$this->attribute} == $select['value']; } $selectField .= sprintf('<option value="%s" %s>%s</option> ', $select['value'], $selected ? "selected" : "", $select['label'] ); } $selectField .= "</select>"; $invalidFeedback = sprintf('<div id="invalid-%s" class="invalid-feedback">%s</div> </div>', $this->attribute, $this->model->getFirstError($this->attribute)); return "$selectField $invalidFeedback"; } }