This repository has been archived by the owner on Sep 30, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for HTML5 color input type (#427)
- Loading branch information
1 parent
25a89bd
commit 16c7611
Showing
8 changed files
with
170 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |