Current Path : /data/web/virtuals/51568/virtual/www/subdom/srps/src/form/ |
Current File : /data/web/virtuals/51568/virtual/www/subdom/srps/src/form/RadiogroupField.php |
<?php namespace app\form; use app\Model; use app\Application; class RadiogroupField { public $model; public $attribute; public $radios = []; public $inline; public $value; public function __construct(Model $model, string $attribute, array $radios, string $value, bool $inline) { $this->model = $model; $this->attribute = $attribute; $this->radios = $radios; $this->inline = $inline; $this->value = $value; } public function __toString(): string { $hasError = $this->model->hasError($this->attribute) ? 'is-invalid' : ''; $inlineClass = ($this->inline) ? "form-check-inline" : ""; $radioGroup = ''; $id = 0; foreach ($this->radios as $radio) { $id++; if (Application::$app->request->getMethod() === 'get') { $checked = $this->value == $radio['value']; } else { $checked = $this->model->{$this->attribute} == $radio['value']; } $radioGroup .= sprintf('<div id="field-%s" class="form-check mb-2 %s"> <input class="form-check-input %s" type="radio" name="%s" id="%s" value="%s" %s> <label class="form-check-label" for="%s">%s</label> </div> ', $this->attribute, $inlineClass, $hasError, $this->attribute, $this->attribute . $id, $radio['value'], $checked ? "checked" : "", $this->attribute . $id, $radio['label'] ); } $invalidFeedback = sprintf('<div id="invalid-radio-%s" class="text-danger small">%s</div>', $this->attribute, $this->model->getFirstError($this->attribute)); return "<div> $radioGroup <div> $invalidFeedback </div> </div>"; } }