kunicmarko/color-picker-bundle
A small Symfony bundle that adds a color picker form type to Symfony forms, making it easy to render color input fields with minimal setup. Lightweight and simple to integrate, but with very low adoption and few stars, so check maintenance and compatibility before relying on it.
This bundle gives Symfony's Form component a color-picker field type — essentially an HTML5 <input type="color"> wrapped in a reusable form type, with a themed Twig block so you can drop it into any form.
Minimal setup (it targets Symfony 3.x/4.x, pre-Flex and Flex era):
composer require kunicmarko/color-picker-bundle
Enable the bundle:
new KunicMarko\ColorPickerBundle\KunicMarkoColorPickerBundle() (check the README for the exact FQCN — the repo is small and the bundle class name is the one thing you cannot guess from the package name) to registerBundles().config/bundles.php.Then use it in a form — this is the whole first use case:
use KunicMarko\ColorPickerBundle\Form\Type\ColorType;
$builder->add('themeColor', ColorType::class);
Look first at the package README (it is short) and the Resources/views/Form directory for the Twig block, then at Form/Type/ColorType.php to confirm the class name and any options it exposes. Since the bundle is archived (last release Dec 2017, 0 dependents), treat the source as the documentation of record rather than expecting updated docs.
Entity storage. The field submits a hex string (#rrggbb) from the browser. Store it as a string column:
/**
* @ORM\Column(type="string", length=7, nullable=true)
*/
private $themeColor;
Validation belongs to you, not the bundle. The bundle only renders and hydrates a string, so add a constraint so bots/curl requests can't write garbage:
use Symfony\Component\Validator\Constraints as Assert;
/**
* @Assert\Regex(pattern="/^#[0-9a-fA-F]{6}$/", message="Enter a hex color.")
*/
private $themeColor;
Reusable type wrapper. For a domain-specific field, extend or compose rather than repeating options across forms:
class ThemeColorType extends AbstractType
{
public function getParent(): string
{
return ColorType::class;
}
}
Then $builder->add('themeColor', ThemeColorType::class) everywhere.
Rendering. In Twig, standard form rendering works once the bundle's form theme is active:
{{ form_row(form.themeColor) }}
If the widget doesn't render as a color swatch, the bundle's theme block isn't loaded — see Gotchas.
Default fallback. HTML5 color inputs cannot be empty in most browsers — an empty value defaults to #000000. If an unset color is meaningful, either show a separate "no color" checkbox or make the field required => false and normalize ''/null in a form event:
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $e) {
$data = $e->getData();
if ($data === '') { $e->setData(null); }
});
Integration tips. Use it anywhere you already use Symfony forms — admin panels (Sonata/EasyAdmin), settings pages, tag/category color coding. Because it is just a form type, it composes with CollectionType, RepeatedType (for "confirm color"), and ChoiceType-driven defaults without extra glue.
getParent(): TextType::class, getBlockPrefix() returning a block that renders type="color"). The bundle buys you almost nothing you can't write yourself, and you keep control of compatibility.#a1b2c3). Don't assume uppercase or short hex (#fff); normalize on save if your downstream code expects a specific format.twig.yaml:
twig:
form_themes:
- '@KunicMarkoColorPicker/Form/fields.html.twig'
(Verify the exact path in Resources/views.) A symptom of a missing theme is a plain <input type="text"> instead of a color swatch.bin/console debug:form App\Form\YourType lists registered types and their parents — use it to confirm the bundle's type is actually registered and to see which parent it delegates to. If the type isn't found, the bundle wasn't enabled.getParent() to preset options, (b) overriding the Twig block to add attributes or a JS data-attribute hook, and (c) a FormTypeExtension if you want to apply the color widget globally to matching fields. There is no configuration tree worth configuring.composer.json constraints likely pin to symfony/form ^3|^4. On Symfony 5+, install will fail or need --with-all-dependencies gymnastics; a custom type is a lower-risk path than forking a 4-star, zero-dependent bundle.How can I help you explore Laravel packages today?