Skip to content

[Form] Add "range" type #11979 #14289

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
[Form] Add range type - finished
[Form] Add range type #11979 - registered RangeType in CoreExtension

[Form] Add range type #11979 - added tests for rendering the form field and validating options

[Form] Add range type #11979 - added copyright lines in RangeTypeTest class

[Form] Add range type #11979 - minor formatting fixes, added author block
  • Loading branch information
Kryniol committed Apr 14, 2015
commit dd08aeb280ab412be60bee9d82c36dc7e9ae7660
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ protected function loadTypes()
new Type\EmailType(),
new Type\HiddenType(),
new Type\IntegerType(),
new Type\RangeType(),
new Type\LanguageType(),
new Type\LocaleType(),
new Type\MoneyType(),
Expand Down
37 changes: 30 additions & 7 deletions src/Symfony/Component/Form/Extension/Core/Type/RangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,56 @@

namespace Symfony\Component\Form\Extension\Core\Type;

use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* A range form element.
*
* @author Carlos Revillo <crevillo@gmail.com>
* @author Pawel Krynicki <pawel.krynicki@hotmail.com>
*/
class RangeType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
public function buildView(FormView $view, FormInterface $form, array $options)
{
$resolver->setDefaults(array(
'min' => null,
'max' => null,
));
$view->vars['attr']['min'] = $options['min'];
$view->vars['attr']['max'] = $options['max'];

if (isset($options['step'])) {
$view->vars['attr']['step'] = $options['step'];
}
}

/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setRequired(array('min', 'max'));

$resolver->setDefined('step');

$resolver->setAllowedTypes('min', 'numeric');
$resolver->setAllowedTypes('max', 'numeric');
$resolver->setAllowedTypes('step', 'numeric');

$resolver->setAllowedValues('step', function ($value) {
return $value > 0;
});
}

/**
* {@inheritdoc}
*/
public function getParent()
{
return 'integer';
return 'number';
}

/**
Expand Down
21 changes: 21 additions & 0 deletions src/Symfony/Component/Form/Tests/AbstractBootstrap3LayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1388,6 +1388,27 @@ public function testInteger()
);
}

public function testRange()
{
$form = $this->factory->createNamed('name', 'range', 123.45, array(
'min' => 1.25,
'max' => 234.56,
'step' => 0.25,
));

$this->assertWidgetMatchesXpath($form->createView(), array('attr' => array('class' => 'my&class')),
'/input
[@type="range"]
[@name="name"]
[@min="1.25"]
[@max="234.56"]
[@step="0.25"]
[@class="my&class form-control"]
[@value="123.45"]
'
);
}

public function testLanguage()
{
$form = $this->factory->createNamed('name', 'language', 'de');
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Extension\Core\Type;

use Symfony\Component\Form\Test\TypeTestCase as TestCase;

/**
* @author Pawel Krynicki <pawel.krynicki@hotmail.com>
*/
class RangeTypeTest extends TestCase
{
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfStepOptionNotPositive()
{
$this->factory->create('range', null, array('min' => 0, 'max' => 100, 'step' => -5));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
*/
public function testThrowExceptionIfMaxOptionsMissing()
{
$this->factory->create('range', null, array('min' => 0));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\MissingOptionsException
*/
public function testThrowExceptionIfMinOptionsMissing()
{
$this->factory->create('range', null, array('max' => 0));
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy