Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Form: Treat only whitespace value as empty #141

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Form.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php

Check failure on line 1 in src/Form.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \.\.\.\$arrays of function array_merge_recursive expects array, array\|object\|null given\.$# in path /home/runner/work/ipl-html/ipl-html/src/Form.php was not matched in reported errors.

Check failure on line 1 in src/Form.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Parameter \#1 \.\.\.\$arrays of function array_merge_recursive expects array, array\|object\|null given\.$# in path /home/runner/work/ipl-html/ipl-html/src/Form.php was not matched in reported errors.

namespace ipl\Html;

@@ -55,7 +55,7 @@
*/
public static function isEmptyValue($value): bool
{
return $value === null || $value === '' || $value === [];
return $value === null || $value === [] || (is_string($value) && trim($value) === '');
}

/**
@@ -225,7 +225,7 @@
$params = [];
}

$params = array_merge_recursive($params, $request->getUploadedFiles());

Check failure on line 228 in src/Form.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $arr1 of function array_merge_recursive expects array, array|object|null given.

Check failure on line 228 in src/Form.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $arr1 of function array_merge_recursive expects array, array|object|null given.
$this->populate($params);

// Assemble after populate in order to conditionally provide form elements
1 change: 1 addition & 0 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
@@ -11,6 +11,7 @@ public function testIsEmptyValue()
$this->assertTrue(Form::isEmptyValue(''), "`''` is not empty");
$this->assertTrue(Form::isEmptyValue(null), '`null` is not empty');
$this->assertTrue(Form::isEmptyValue([]), '`[]` is not empty');
$this->assertTrue(Form::isEmptyValue(' '), '` ` is not empty');

$this->assertFalse(Form::isEmptyValue(0), '`0` is empty');
$this->assertFalse(Form::isEmptyValue('0'), "`'0'` is empty");

Unchanged files with check annotations Beta

if ($value instanceof Attribute) {
$this->addAttribute($value);
} elseif (is_string($key)) {
$this->add($key, $value);

Check failure on line 50 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 50 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 50 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 50 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 50 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.
} elseif (is_array($value) && count($value) === 2) {
$this->add(array_shift($value), array_shift($value));

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $attribute of method ipl\Html\Attributes::add() expects array|ipl\Html\Attribute|ipl\Html\Attributes|string, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $attribute of method ipl\Html\Attributes::add() expects array|ipl\Html\Attribute|ipl\Html\Attributes|string, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $attribute of method ipl\Html\Attributes::add() expects array|ipl\Html\Attribute|ipl\Html\Attributes|string, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $attribute of method ipl\Html\Attributes::add() expects array|ipl\Html\Attribute|ipl\Html\Attributes|string, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $attribute of method ipl\Html\Attributes::add() expects array|ipl\Html\Attribute|ipl\Html\Attributes|string, mixed given.

Check failure on line 52 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.
}
}
}
if (is_array($attribute)) {
foreach ($attribute as $name => $value) {
$this->set($name, $value);

Check failure on line 202 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Ignored error pattern #^Parameter \#2 \$value of method ipl\\Html\\Attributes\:\:set\(\) expects array\|bool\|string\|null, mixed given\.$# in path /home/runner/work/ipl-html/ipl-html/src/Attributes.php is expected to occur 1 time, but occurred 2 times.

Check failure on line 202 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Ignored error pattern #^Parameter \#2 \$value of method ipl\\Html\\Attributes\:\:set\(\) expects array\|bool\|string\|null, mixed given\.$# in path /home/runner/work/ipl-html/ipl-html/src/Attributes.php is expected to occur 1 time, but occurred 2 times.

Check failure on line 202 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Ignored error pattern #^Parameter \#2 \$value of method ipl\\Html\\Attributes\:\:set\(\) expects array\|bool\|string\|null, mixed given\.$# in path /home/runner/work/ipl-html/ipl-html/src/Attributes.php is expected to occur 1 time, but occurred 2 times.

Check failure on line 202 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Ignored error pattern #^Parameter \#2 \$value of method ipl\\Html\\Attributes\:\:set\(\) expects array\|bool\|string\|null, mixed given\.$# in path /home/runner/work/ipl-html/ipl-html/src/Attributes.php is expected to occur 1 time, but occurred 2 times.

Check failure on line 202 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Ignored error pattern #^Parameter \#2 \$value of method ipl\\Html\\Attributes\:\:set\(\) expects array\|bool\|string\|null, mixed given\.$# in path /home/runner/work/ipl-html/ipl-html/src/Attributes.php is expected to occur 1 time, but occurred 2 times.
}
return $this;
if (is_array($attribute)) {
foreach ($attribute as $name => $value) {
$this->add($name, $value);

Check failure on line 250 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 250 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 250 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 250 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.

Check failure on line 250 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::add() expects array|bool|string|null, mixed given.
}
return $this;
*/
public function offsetSet($name, $value): void
{
$this->set($name, $value);

Check failure on line 489 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::set() expects array|bool|string|null, mixed given.

Check failure on line 489 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::set() expects array|bool|string|null, mixed given.

Check failure on line 489 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::set() expects array|bool|string|null, mixed given.

Check failure on line 489 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::set() expects array|bool|string|null, mixed given.

Check failure on line 489 in src/Attributes.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #2 $value of method ipl\Html\Attributes::set() expects array|bool|string|null, mixed given.
}
/**
$callback = $this->callback;
if ($this->escaped) {
return $callback();

Check failure on line 109 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Method ipl\Html\DeferredText::render() should return string but returns mixed.

Check failure on line 109 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Method ipl\Html\DeferredText::render() should return string but returns mixed.

Check failure on line 109 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Method ipl\Html\DeferredText::render() should return string but returns mixed.

Check failure on line 109 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Method ipl\Html\DeferredText::render() should return string but returns mixed.

Check failure on line 109 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Method ipl\Html\DeferredText::render() should return string but returns mixed.
} else {
return Html::escape($callback());

Check failure on line 111 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Parameter #1 $content of static method ipl\Html\Html::escape() expects string, mixed given.

Check failure on line 111 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Parameter #1 $content of static method ipl\Html\Html::escape() expects string, mixed given.

Check failure on line 111 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Parameter #1 $content of static method ipl\Html\Html::escape() expects string, mixed given.

Check failure on line 111 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Parameter #1 $content of static method ipl\Html\Html::escape() expects string, mixed given.

Check failure on line 111 in src/DeferredText.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Parameter #1 $content of static method ipl\Html\Html::escape() expects string, mixed given.
}
}
}
<?php

Check failure on line 1 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Ignored error pattern #^PHPDoc tag @param has invalid value \(string\)\: Unexpected token "\\n \* ", expected variable at offset 24$# in path /home/runner/work/ipl-html/ipl-html/src/Error.php was not matched in reported errors.

Check failure on line 1 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Ignored error pattern #^PHPDoc tag @param has invalid value \(string\)\: Unexpected token "\\n \* ", expected variable at offset 24$# in path /home/runner/work/ipl-html/ipl-html/src/Error.php was not matched in reported errors.

Check failure on line 1 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Ignored error pattern #^PHPDoc tag @param has invalid value \(string\)\: Unexpected token "\\n \* ", expected variable at offset 24$# in path /home/runner/work/ipl-html/ipl-html/src/Error.php was not matched in reported errors.

Check failure on line 1 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Ignored error pattern #^PHPDoc tag @param has invalid value \(string\)\: Unexpected token "\\n \* ", expected variable at offset 24$# in path /home/runner/work/ipl-html/ipl-html/src/Error.php was not matched in reported errors.

Check failure on line 1 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Ignored error pattern #^PHPDoc tag @param has invalid value \(string\)\: Unexpected token "\\n \* ", expected variable at offset 24$# in path /home/runner/work/ipl-html/ipl-html/src/Error.php was not matched in reported errors.
namespace ipl\Html;
}
/**
* @param string

Check failure on line 95 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

PHPDoc tag @param has invalid value (string): Unexpected token "\n * ", expected variable at offset 24 on line 2

Check failure on line 95 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

PHPDoc tag @param has invalid value (string): Unexpected token "\n * ", expected variable at offset 24 on line 2

Check failure on line 95 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

PHPDoc tag @param has invalid value (string): Unexpected token "\n * ", expected variable at offset 24 on line 2

Check failure on line 95 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

PHPDoc tag @param has invalid value (string): Unexpected token "\n * ", expected variable at offset 24 on line 2

Check failure on line 95 in src/Error.php

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

PHPDoc tag @param has invalid value (string): Unexpected token "\n * ", expected variable at offset 24 on line 2
* @return HtmlDocument
*/
protected static function renderErrorMessage($message)
$this
->getValidators()
->clearValidators()
->addValidators($validators);

Check failure on line 196 in src/FormElement/BaseFormElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $validators of method ipl\Validator\ValidatorChain::addValidators() expects Traversable<int|string, mixed>, iterable given.

Check failure on line 196 in src/FormElement/BaseFormElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $validators of method ipl\Validator\ValidatorChain::addValidators() expects Traversable<int|string, mixed>, iterable given.
return $this;
}
*/
public function addValidators($validators)
{
$this->getValidators()->addValidators($validators);

Check failure on line 210 in src/FormElement/BaseFormElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #1 $validators of method ipl\Validator\ValidatorChain::addValidators() expects Traversable<int|string, mixed>, iterable given.

Check failure on line 210 in src/FormElement/BaseFormElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #1 $validators of method ipl\Validator\ValidatorChain::addValidators() expects Traversable<int|string, mixed>, iterable given.
return $this;
}
$callbacksProperty->setAccessible(true);
$callbacks = $callbacksProperty->getValue($attributes);
if (isset($callbacks['name'])) {

Check failure on line 378 in src/FormElement/BaseFormElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Cannot access offset 'name' on mixed\.$# in path /home/runner/work/ipl-html/ipl-html/src/FormElement/BaseFormElement.php is expected to occur 2 times, but occurred only 1 time.

Check failure on line 378 in src/FormElement/BaseFormElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Cannot access offset 'name' on mixed\.$# in path /home/runner/work/ipl-html/ipl-html/src/FormElement/BaseFormElement.php is expected to occur 2 times, but occurred only 1 time.
$name = $callbacks['name']();
if ($name instanceof Attribute) {
$element->getAttributes()->registerAttributeCallback('name', function () use ($element) {
$multiple = false;
if (in_array(MultipleAttribute::class, class_uses($element), true)) {
$multiple = $element->isMultiple();

Check failure on line 113 in src/FormElement/FieldsetElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().

Check failure on line 113 in src/FormElement/FieldsetElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().
}
/**
// In Chrome, if the seconds are set to 00, DateTime::createFromFormat() returns false.
// Create DateTime without seconds in format
if ($value === false) {
$format = substr(static::FORMAT, 0, strrpos(static::FORMAT, ':') ?: null);

Check failure on line 28 in src/FormElement/LocalDateTimeElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #3 $length of function substr expects int, int<1, max>|null given.

Check failure on line 28 in src/FormElement/LocalDateTimeElement.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #3 $length of function substr expects int, int<1, max>|null given.
$value = DateTime::createFromFormat($format, $originalVal);
}
<?php

Check failure on line 1 in src/FormattedString.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Ignored error pattern #^Parameter \#2 \$values of function vsprintf expects array\<bool\|float\|int\|string\|null\>, array\<ipl\\Html\\ValidHtml\> given\.$# in path /home/runner/work/ipl-html/ipl-html/src/FormattedString.php was not matched in reported errors.

Check failure on line 1 in src/FormattedString.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Ignored error pattern #^Parameter \#2 \$values of function vsprintf expects array\<bool\|float\|int\|string\|null\>, array\<ipl\\Html\\ValidHtml\> given\.$# in path /home/runner/work/ipl-html/ipl-html/src/FormattedString.php was not matched in reported errors.
namespace ipl\Html;
{
return vsprintf(
$this->format->render(),
$this->args

Check failure on line 98 in src/FormattedString.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Parameter #2 $args of function vsprintf expects array<bool|float|int|string|null>, array<ipl\Html\ValidHtml> given.

Check failure on line 98 in src/FormattedString.php

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Parameter #2 $args of function vsprintf expects array<bool|float|int|string|null>, array<ipl\Html\ValidHtml> given.
);
}
}