Skip to content
This repository has been archived by the owner on Sep 30, 2021. It is now read-only.

Commit

Permalink
Add support for HTML5 color input type (#427)
Browse files Browse the repository at this point in the history
  • Loading branch information
kunicmarko20 authored and greg0ire committed Sep 10, 2017
1 parent 25a89bd commit 16c7611
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 5 deletions.
11 changes: 11 additions & 0 deletions Form/Type/ColorSelectorType.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

@trigger_error(
'The '.__NAMESPACE__.'\ColorSelectorType class is deprecated since version 3.4 and will be removed in 4.0.'
.' Use '.__NAMESPACE__.'\ColorType instead.',
E_USER_DEPRECATED
);

/**
* NEXT_MAJOR: remove this class.
*
* @deprecated since version 3.4, to be removed in 4.0. Use ColorType instead
*/
class ColorSelectorType extends AbstractType
{
/**
Expand Down
44 changes: 44 additions & 0 deletions Form/Type/ColorType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\CoreBundle\Form\Type;

use Symfony\Component\Form\AbstractType;

final class ColorType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function getParent()
{
return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') ?
'Symfony\Component\Form\Extension\Core\Type\TextType' :
'text' // SF <2.8 BC
;
}

/**
* {@inheritdoc}
*/
public function getBlockPrefix()
{
return 'sonata_type_color';
}

/**
* {@inheritdoc}
*/
public function getName()
{
return $this->getBlockPrefix();
}
}
3 changes: 3 additions & 0 deletions Resources/config/form_types.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,8 @@
<service id="sonata.core.form.type.color_selector" class="Sonata\CoreBundle\Form\Type\ColorSelectorType">
<tag name="form.type" alias="sonata_type_color_selector"/>
</service>
<service id="sonata.core.form.type.color" class="Sonata\CoreBundle\Form\Type\ColorType">
<tag name="form.type" alias="sonata_type_color"/>
</service>
</services>
</container>
Binary file added Resources/doc/images/color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Resources/doc/images/colorpicker.png
Binary file not shown.
10 changes: 5 additions & 5 deletions Resources/doc/reference/form_types.rst
Original file line number Diff line number Diff line change
Expand Up @@ -487,12 +487,12 @@ Example with ``doctrine_orm_date_range`` filter:
// ...
}
sonata_type_color_picker
sonata_type_color
------------------------

This type a simple color picker from AdminLTE colors. Its available as service, and inherit from ``choice`` default form types.
This is HTML5 input type color.

.. image:: ../images/colorpicker.png
.. image:: ../images/color.png

In order to use it, you'll need to perform a bit of setup:

Expand All @@ -503,7 +503,7 @@ In order to use it, you'll need to perform a bit of setup:
# app/config/config.yml
twig:
form_themes:
- 'SonataCoreBundle:Form:colorpicker.html.twig'
- 'SonataCoreBundle:Form:color.html.twig'
Finally, in your form, you may use the form type as follows:

Expand All @@ -517,7 +517,7 @@ Finally, in your form, you may use the form type as follows:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('color', 'sonata_type_color_selector')
->add('color', 'sonata_type_color')
// ...
;
}
Expand Down
15 changes: 15 additions & 0 deletions Resources/views/Form/color.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{#
This file is part of the Sonata package.
(c) Thomas Rabaix <[email protected]>
For the full copyright and license information, please view the LICENSE
file that was distributed with this source code.
#}
{% block sonata_type_color_widget %}
{% spaceless %}
<input type="color" {{ block('widget_attributes') }} {% if value is not empty %}value="{{ value }}" {% endif %}/>
{% endspaceless %}
{% endblock sonata_type_color_widget %}
92 changes: 92 additions & 0 deletions Tests/Form/Type/ColorTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\CoreBundle\Tests\Form\Type;

use Sonata\CoreBundle\Form\Type\ColorType;

class ColorTypeTest extends TypeTestCase
{
public function testBuildForm()
{
// NEXT_MAJOR: Hack for php 5.3 only, remove it when requirement of PHP is >= 5.4
$that = $this;

$formBuilder = $this->getMockBuilder('Symfony\Component\Form\FormBuilder')->disableOriginalConstructor()->getMock();
$formBuilder
->expects($this->any())
->method('add')
->will($this->returnCallback(function ($name, $type = null) use ($that) {
// NEXT_MAJOR: Remove this "if" (when requirement of Symfony is >= 2.8)
if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
if (null !== $type) {
$isFQCN = class_exists($type);
if (!$isFQCN && method_exists('Symfony\Component\Form\AbstractType', 'getName')) {
// 2.8
@trigger_error(
sprintf(
'Accessing type "%s" by its string name is deprecated since version 2.8 and will be removed in 3.0.'
.' Use the fully-qualified type class name instead.',
$type
),
E_USER_DEPRECATED)
;
}

$that->assertTrue($isFQCN, sprintf('Unable to ensure %s is a FQCN', $type));
}
}
}));

$type = new ColorType();
$type->buildForm($formBuilder, array());
}

public function testSubmitValidData()
{
$data = '#556b2f';

if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
$type = 'Sonata\CoreBundle\Form\Type\ColorType';
} else {
$type = new ColorType();
}

$form = $this->factory->create($type);
$form->submit($data);
$this->assertTrue($form->isSynchronized());
}

public function testGetParent()
{
$form = new ColorType();

// NEXT_MAJOR: Remove this "if" (when requirement of Symfony is >= 2.8)
if (method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix')) {
$parentRef = $form->getParent();

$isFQCN = class_exists($parentRef);
if (!$isFQCN && method_exists('Symfony\Component\Form\AbstractType', 'getName')) {
// 2.8
@trigger_error(
sprintf(
'Accessing type "%s" by its string name is deprecated since version 2.8 and will be removed in 3.0.'
.' Use the fully-qualified type class name instead.',
$parentRef
),
E_USER_DEPRECATED)
;
}

$this->assertTrue($isFQCN, sprintf('Unable to ensure %s is a FQCN', $parentRef));
}
}
}

0 comments on commit 16c7611

Please sign in to comment.