137 lines
4.0 KiB
PHP
137 lines
4.0 KiB
PHP
<?php
|
|
/**
|
|
* FuzeWorks Forms Library
|
|
*
|
|
* The FuzeWorks PHP FrameWork
|
|
*
|
|
* Copyright (C) 2013-${YEAR} i15
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
* in the Software without restriction, including without limitation the rights
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
* furnished to do so, subject to the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
* copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
* SOFTWARE.
|
|
*
|
|
* @author i15
|
|
* @copyright Copyright (c) 2013 - ${YEAR}, i15. (http://i15.nl)
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
|
*
|
|
* @link https://i15.nl
|
|
* @since Version 1.3.0
|
|
*
|
|
* @version Version 1.3.0
|
|
*/
|
|
|
|
namespace FuzeWorks\Forms\Fields;
|
|
use FuzeWorks\Forms\Exceptions\InputException;
|
|
use FuzeWorks\Forms\Field;
|
|
|
|
class RadioField extends Field
|
|
{
|
|
|
|
protected array $options = [];
|
|
protected array $classNames = ['form-group'];
|
|
|
|
/**
|
|
* Add an option to the RadioField
|
|
*
|
|
* @param string $value
|
|
* @param string|null $label
|
|
* @return $this
|
|
* @throws InputException
|
|
*/
|
|
public function addOption(string $value, string $label = null): self
|
|
{
|
|
if (isset($this->options[$value]))
|
|
throw new InputException("Could not add value to field. Value already exists.");
|
|
|
|
// Add to the values
|
|
if (is_null($label))
|
|
$this->options[$value] = $value;
|
|
else
|
|
$this->options[$value] = $label;
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Bulk add options to the RadioField
|
|
*
|
|
* @param array $options Associative array [name => label, name => label]
|
|
* @return $this
|
|
* @throws InputException
|
|
*/
|
|
public function addOptions(array $options): self
|
|
{
|
|
foreach ($options as $key => $val)
|
|
$this->addOption($key, $val);
|
|
|
|
return $this;
|
|
}
|
|
|
|
/**
|
|
* Pre-check one of the options
|
|
*
|
|
* @param string $optionName Name of the option to check
|
|
* @return $this
|
|
*/
|
|
public function check(string $optionName): self
|
|
{
|
|
$this->setValue($optionName);
|
|
return $this;
|
|
}
|
|
|
|
public function getOptions(): array
|
|
{
|
|
return $this->options;
|
|
}
|
|
|
|
protected function validateField(): bool
|
|
{
|
|
if (isset($this->options[$this->value]))
|
|
{
|
|
$this->valid = true;
|
|
return $this->valid;
|
|
}
|
|
|
|
$this->valid = false;
|
|
$this->errors[] = $this->getLabel() . " was provided an illegal option.";
|
|
return $this->valid;
|
|
}
|
|
|
|
public function generateHtml(): string
|
|
{
|
|
$name = "name='".$this->getName()."'";
|
|
$lock = $this->lock ? "disabled" : "";
|
|
$class = "class='".implode(" ", $this->classNames)."'";
|
|
|
|
$out = "<div $class>";
|
|
foreach ($this->options as $key => $val)
|
|
{
|
|
$uni_id = $this->getId() . "_" . $key;
|
|
$id = "id='" . $uni_id . "'";
|
|
$value = "value='".$key."'";
|
|
$checked = $key === $this->getValue() ? "checked" : "";
|
|
|
|
$out .= "<div class='form-check'>";
|
|
$out .= "<input class='form-check-input' type='radio' $id $name $value $lock $checked>";
|
|
$out .= "<label class='form-check-label' for='$uni_id'>$val</label>";
|
|
$out .= "</div>";
|
|
}
|
|
|
|
$out .= "</div>";
|
|
return $out;
|
|
}
|
|
} |