Add TypeSwitch class and enhance attribute handling

Introduced a new `TypeSwitch` class to manage input type switch fields for forms, allowing simplified creation of toggle switches or binary options. Enhanced attribute handling in `formattributes.php` to conditionally remove the `checked` attribute only if not explicitly specified. Updated framework to include the new `TypeSwitch` class.
This commit is contained in:
stephan.kasdorf
2025-07-14 12:20:09 +02:00
parent 17166b8b6c
commit 157016ff35
4 changed files with 70 additions and 2 deletions

View File

@@ -59,7 +59,12 @@ class FormAttributes
$this->_element = str_replace(' ID', '', $this->_element);
$this->_element = str_replace(' CLASS', '', $this->_element);
$this->_element = str_replace(' enctype="ENCTYPE"', '', $this->_element);
$this->_element = str_replace(' checked="CHECKED"', '', $this->_element);
// Only remove checked attribute if it's not set in the attributes
if (!isset($attributes['checked']) || empty($attributes['checked']))
{
$this->_element = str_replace(' checked="CHECKED"', '', $this->_element);
$this->_element = str_replace(' CHECKED', '', $this->_element);
}
$this->_element = str_replace(' onsubmit="ONSUBMIT"', '', $this->_element);
$this->_element = str_replace(' onclick="ONCLICK"', '', $this->_element);
$this->_element = str_replace(' action="ACTION"', '', $this->_element);
@@ -85,8 +90,8 @@ class FormAttributes
$this->_element = str_replace(' method="METHOD"', '', $this->_element);
$this->_element = str_replace(' data-toggle="DATA-TOGGLE"', '', $this->_element);
$this->_element = str_replace(' SELECTED', '', $this->_element);
$this->_element = str_replace(' MULTIPLE', '', $this->_element);
$this->_element = str_replace(' CONTEXT', '', $this->_element);
$this->_element = str_replace(' CHECKED', '', $this->_element);
$this->_element = str_replace(' VALUE', '', $this->_element);
$this->_element = str_replace(' PATTERN', '', $this->_element);
$this->_element = str_replace('ANY', '', $this->_element);

44
core/c/typeswitch.php Normal file
View File

@@ -0,0 +1,44 @@
<?php
namespace Nibiru\Form;
use Nibiru\Adapter;
/**
* Created by PhpStorm.
* User: mithril
* Date: 26.01.18
* Time: 21:42
*/
class TypeSwitch extends FormAttributes implements IForm
{
private $_attributes = array(
self::FORM_VALUE => '',
self::FORM_ATTRIBUTE_ID => '',
self::FORM_ATTRIBUTE_CLASS => '',
self::FORM_NAME => '',
self::FORM_ATTRIBUTE_CHECKED
);
public function loadElement($attributes)
{
parent::__construct($this->_attributes);
$this->_setElement();
$this->_setAttributes(self::loadAttributeValues($attributes));
return $this->getElement();
}
/**
* @param mixed $element
*/
private function _setElement()
{
$this->_element = '<div class="media-body icon-state switch-outline">' . "\n" .
'<label class="switch">' . "\n" .
'<input type="checkbox" name="NAME" CHECKED ID CLASS value="VALUE">' . "\n" .
'<span class="switch-state bg-primary"></span>' . "\n" .
'</label>' . "\n" .
'</div>';
}
}