Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Form] Fix #11694 - Enforce options value type check in some form types #11696

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 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$resolver->setDefaults(array(
'years' => range(date('Y') - 120, date('Y')),
));

$resolver->setAllowedTypes(array(
'years' => 'array',
));
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/DateType.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,9 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)

$resolver->setAllowedTypes(array(
'format' => array('int', 'string'),
'years' => 'array',
'months' => 'array',
'days' => 'array',
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'second_name' => 'second',
'error_bubbling' => false,
));

$resolver->setAllowedTypes(array(
'options' => 'array',
'first_options' => 'array',
'second_options' => 'array',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add it for options too

));
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/TimeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
'choice',
),
));

$resolver->setAllowedTypes(array(
'hours' => 'array',
'minutes' => 'array',
'seconds' => 'array',
));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/UrlType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
$resolver->setDefaults(array(
'default_protocol' => 'http',
));

$resolver->setAllowedTypes(array(
'default_protocol' => array('null', 'string'),
));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* 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;

/**
* @author Stepan Anchugov <[email protected]>
*/
class BirthdayTypeTest extends BaseTypeTest
{
/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testSetInvalidYearsOption()
{
$this->factory->create('birthday', null, array(
'years' => 'bad value',
));
}

protected function getTestedType()
{
return 'birthday';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,36 @@ public function testThrowExceptionIfFormatIsInvalid()
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfYearsIsInvalid()
{
$this->factory->create('date', null, array(
'years' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfMonthsIsInvalid()
{
$this->factory->create('date', null, array(
'months' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfDaysIsInvalid()
{
$this->factory->create('date', null, array(
'days' => 'bad value',
));
}

public function testSetDataWithDifferentTimezones()
{
$form = $this->factory->create('date', null, array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,39 @@ public function testSetRequired()
$this->assertFalse($form['second']->isRequired());
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testSetInvalidOptions()
{
$this->factory->create('repeated', null, array(
'type' => 'text',
'options' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testSetInvalidFirstOptions()
{
$this->factory->create('repeated', null, array(
'type' => 'text',
'first_options' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testSetInvalidSecondOptions()
{
$this->factory->create('repeated', null, array(
'type' => 'text',
'second_options' => 'bad value',
));
}

public function testSetErrorBubblingToTrue()
{
$form = $this->factory->create('repeated', null, array(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,4 +673,34 @@ public function testInitializeWithSecondsAndWithoutMinutes()
'with_seconds' => true,
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfHoursIsInvalid()
{
$this->factory->create('time', null, array(
'hours' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfMinutesIsInvalid()
{
$this->factory->create('time', null, array(
'minutes' => 'bad value',
));
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfSecondsIsInvalid()
{
$this->factory->create('time', null, array(
'seconds' => 'bad value',
));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,14 @@ public function testSubmitAddsNoDefaultProtocolIfSetToNull()
$this->assertSame('www.domain.com', $form->getData());
$this->assertSame('www.domain.com', $form->getViewData());
}

/**
* @expectedException \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException
*/
public function testThrowExceptionIfDefaultProtocolIsInvalid()
{
$this->factory->create('url', null, array(
'default_protocol' => array(),
));
}
}