fix input element

This commit is contained in:
joyqi 2023-12-04 17:23:06 +08:00
parent a1f62e1bcc
commit a2c1fbf4a8
11 changed files with 105 additions and 91 deletions

View File

@ -217,7 +217,7 @@ abstract class Element extends Layout
public function value($value): Element
{
$this->value = $value;
$this->inputValue($value ?? '');
$this->inputValue($value);
return $this;
}

View File

@ -61,7 +61,7 @@ class Checkbox extends Element
*/
protected function inputValue($value)
{
$values = is_null($value) ? [] : (is_array($value) ? $value : [$value]);
$values = isset($value) ? (is_array($value) ? $value : [$value]) : [];
foreach ($this->options as $option) {
$option->removeAttribute('checked');

View File

@ -3,7 +3,6 @@
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
@ -19,6 +18,8 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
*/
class Hidden extends Element
{
use TextInputTrait;
/**
* 自定义初始函数
*
@ -31,28 +32,19 @@ class Hidden extends Element
}
/**
* 初始化当前输入项
*
* @access public
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout|null
* @param string $value
* @return string
*/
public function input(?string $name = null, ?array $options = null): ?Layout
protected function filterValue(string $value): string
{
$input = new Layout('input', ['name' => $name, 'type' => 'hidden']);
$this->container($input);
$this->inputs[] = $input;
return $input;
return htmlspecialchars($value);
}
/**
* 设置表单项默认值
*
* @param mixed $value 表单项默认值
* @return string
*/
protected function inputValue($value)
protected function getType(): string
{
$this->input->setAttribute('value', htmlspecialchars($value));
return 'hidden';
}
}

View File

@ -3,7 +3,6 @@
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
@ -19,30 +18,22 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
*/
class Password extends Element
{
use TextInputTrait;
/**
* 初始化当前输入项
*
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout|null
* @param string $value
* @return string
*/
public function input(?string $name = null, ?array $options = null): ?Layout
protected function filterValue(string $value): string
{
$input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId,
'name' => $name, 'type' => 'password', 'class' => 'password']);
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
$this->container($input);
$this->inputs[] = $input;
return $input;
return htmlspecialchars($value);
}
/**
* 设置表单项默认值
*
* @param mixed $value 表单项默认值
* @return string
*/
protected function inputValue($value)
protected function getType(): string
{
$this->input->setAttribute('value', htmlspecialchars($value));
return 'password';
}
}

View File

@ -65,7 +65,7 @@ class Radio extends Element
$option->removeAttribute('checked');
}
if (isset($this->options[$value])) {
if (isset($value) && isset($this->options[$value])) {
$this->value = $value;
$this->options[$value]->setAttribute('checked', 'true');
$this->input = $this->options[$value];

View File

@ -60,7 +60,7 @@ class Select extends Element
$option->removeAttribute('selected');
}
if (isset($this->options[$value])) {
if (isset($value) && isset($this->options[$value])) {
$this->options[$value]->setAttribute('selected', 'true');
}
}

View File

@ -43,6 +43,6 @@ class Submit extends Element
*/
protected function inputValue($value)
{
$this->input->html($value);
$this->input->html($value ?? 'Submit');
}
}

View File

@ -3,7 +3,6 @@
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
@ -19,35 +18,22 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
*/
class Text extends Element
{
/**
* 初始化当前输入项
*
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout|null
*/
public function input(?string $name = null, ?array $options = null): ?Layout
{
$input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId,
'name' => $name, 'type' => 'text', 'class' => 'text']);
$this->container($input);
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
$this->inputs[] = $input;
use TextInputTrait;
return $input;
/**
* @param string $value
* @return string
*/
protected function filterValue(string $value): string
{
return htmlspecialchars($value);
}
/**
* 设置表单项默认值
*
* @param mixed $value 表单项默认值
* @return string
*/
protected function inputValue($value)
protected function getType(): string
{
if (isset($value)) {
$this->input->setAttribute('value', htmlspecialchars($value));
} else {
$this->input->removeAttribute('value');
}
return 'text';
}
}

View File

@ -0,0 +1,59 @@
<?php
namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
trait TextInputTrait
{
/**
* 初始化当前输入项
*
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout|null
*/
public function input(?string $name = null, ?array $options = null): ?Layout
{
$input = new Layout('input', [
'id' => $name . '-0-' . self::$uniqueId,
'name' => $name,
'type' => $this->getType(),
'class' => 'text'
]);
$this->container($input);
$this->inputs[] = $input;
if (isset($this->label)) {
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
}
return $input;
}
/**
* 设置表单项默认值
*
* @param mixed $value 表单项默认值
*/
protected function inputValue($value)
{
if (isset($value)) {
$this->input->setAttribute('value', $this->filterValue($value));
} else {
$this->input->removeAttribute('value');
}
}
/**
* @param string $value
* @return string
*/
abstract protected function filterValue(string $value): string;
/**
* @return string
*/
abstract protected function getType(): string;
}

View File

@ -43,6 +43,6 @@ class Textarea extends Element
*/
protected function inputValue($value)
{
$this->input->html(htmlspecialchars($value));
$this->input->html(htmlspecialchars($value ?? ''));
}
}

View File

@ -4,7 +4,6 @@ namespace Typecho\Widget\Helper\Form\Element;
use Typecho\Common;
use Typecho\Widget\Helper\Form\Element;
use Typecho\Widget\Helper\Layout;
if (!defined('__TYPECHO_ROOT_DIR__')) {
exit;
@ -20,35 +19,22 @@ if (!defined('__TYPECHO_ROOT_DIR__')) {
*/
class Url extends Element
{
/**
* 初始化当前输入项
*
* @param string|null $name 表单元素名称
* @param array|null $options 选择项
* @return Layout|null
*/
public function input(?string $name = null, ?array $options = null): ?Layout
{
$input = new Layout('input', ['id' => $name . '-0-' . self::$uniqueId,
'name' => $name, 'type' => 'url', 'class' => 'text']);
$this->container($input);
$this->label->setAttribute('for', $name . '-0-' . self::$uniqueId);
$this->inputs[] = $input;
use TextInputTrait;
return $input;
/**
* @param string $value
* @return string
*/
protected function filterValue(string $value): string
{
return htmlspecialchars(Common::idnToUtf8($value));
}
/**
* 设置表单项默认值
*
* @param mixed $value 表单项默认值
* @return string
*/
protected function inputValue($value)
protected function getType(): string
{
if (isset($value)) {
$this->input->setAttribute('value', htmlspecialchars(Common::idnToUtf8($value)));
} else {
$this->input->removeAttribute('value');
}
return 'url';
}
}