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);
// 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>';
}
}

View File

@@ -24,6 +24,7 @@ use Nibiru\Form\TypeReset;
use Nibiru\Form\TypeSearch;
use Nibiru\Form\TypeSelect;
use Nibiru\Form\TypeSubmit;
use Nibiru\Form\TypeSwitch;
use Nibiru\Form\TypeTelefon;
use Nibiru\Form\TypeTextarea;
use Nibiru\Form\TypeText;
@@ -240,6 +241,23 @@ class Form
self::assemble( self::getElement()->loadElement( $attributes ) );
}
/**
* @desc adds an input type switch field to the form; this method is useful for
* adding toggle switches or binary options.
* @param $attributes an array of attributes, such as ("NAME", "ID", "CLASS", "ONCHANGE", "ONBLUR", "ONFOCUS", "REQUIRED", "CHECKED")
* @param $div optional parameter to specify a div wrapping element; if provided, the div will be set for the component.
* @return void
*/
public static function addInputTypeSwitch( $attributes, $div = false )
{
if($div)
{
self::setDiv( $div );
}
self::setElement( new TypeSwitch() );
self::assemble( self::getElement()->loadElement( $attributes ) );
}
/**
* @desc adds a input field for password entry
* @param $attributes

View File

@@ -75,6 +75,7 @@ require_once __DIR__ . '/c/typenumber.php';
require_once __DIR__ . '/c/typereset.php';
require_once __DIR__ . '/c/typefileupload.php';
require_once __DIR__ . '/c/typecheckbox.php';
require_once __DIR__ . '/c/typeswitch.php';
require_once __DIR__ . '/c/typecolor.php';
require_once __DIR__ . '/c/typeoption.php';
require_once __DIR__ . '/c/typeselect.php';