Skip to content

Commit

Permalink
added addColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Oct 31, 2023
1 parent 24cc848 commit 040fd18
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Forms/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
54 changes: 54 additions & 0 deletions tests/Forms/Controls.TextInput.color.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/**
* Test: Nette\Forms\Controls\TextInput & color.
*/

declare(strict_types=1);

use Nette\Forms\Form;
use Nette\Utils\Html;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


before(function () {
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST = $_FILES = [];
$_COOKIE[Nette\Http\Helpers::STRICT_COOKIE_NAME] = '1';
Form::initialize(true);
});


test('loadData empty string', function () {
$_POST = ['color' => ''];

$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('<input type="color" name="color" id="frm-color" data-nette-rules=\'[{"op":":pattern","msg":"Invalid color","arg":"#[0-9a-fA-F]{6}"}]\' value="#1020AB">', (string) $input->getControl());
});

0 comments on commit 040fd18

Please sign in to comment.