diff --git a/src/Forms/Container.php b/src/Forms/Container.php index db6f5871d..16e7a529d 100644 --- a/src/Forms/Container.php +++ b/src/Forms/Container.php @@ -522,6 +522,19 @@ public function addMultiSelect( } + /** + * Adds color picker. + * @param string|object|null $label + */ + public function addColor(string $name, $label = null): Controls\TextInput + { + return $this[$name] = (new Controls\TextInput($label)) + ->setNullable() + ->setHtmlType('color') + ->addRule(Form::Pattern, 'Invalid color', '#[0-9a-fA-F]{6}'); + } + + /** * Adds button used to submit form. * @param string|object|null $caption diff --git a/tests/Forms/Controls.TextInput.color.phpt b/tests/Forms/Controls.TextInput.color.phpt new file mode 100644 index 000000000..4770d09cd --- /dev/null +++ b/tests/Forms/Controls.TextInput.color.phpt @@ -0,0 +1,54 @@ + '']; + + $form = new Form; + $input = $form->addColor('color'); + + Assert::null($input->getValue()); + Assert::false($input->isFilled()); +}); + + +test('loadData valid string', function () { + $_POST = ['color' => '#1020aa']; + + $form = new Form; + $input = $form->addColor('color'); + + Assert::same('#1020aa', $input->getValue()); + Assert::true($input->isFilled()); +}); + + + +test('', function () { + $form = new Form; + $input = $form->addColor('color') + ->setValue('#1020AB'); + + Assert::type(Html::class, $input->getControl()); + Assert::same('', (string) $input->getControl()); +});