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

Skip to content

[2.3] Added currency locale data, form type and validator #6566

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 1 commit 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
3 changes: 3 additions & 0 deletions src/Symfony/Bundle/FrameworkBundle/Resources/config/form.xml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@
<service id="form.type.reset" class="Symfony\Component\Form\Extension\Core\Type\ResetType">
<tag name="form.type" alias="reset" />
</service>
<service id="form.type.currency" class="Symfony\Component\Form\Extension\Core\Type\CurrencyType">
<tag name="form.type" alias="currency" />
</service>

<!-- FormTypeHttpFoundationExtension -->
<service id="form.type_extension.form.http_foundation" class="Symfony\Component\Form\Extension\HttpFoundation\Type\FormTypeHttpFoundationExtension">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected function loadTypes()
new Type\ButtonType(),
new Type\SubmitType(),
new Type\ResetType(),
new Type\CurrencyType(),
);
}
}
46 changes: 46 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/Type/CurrencyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?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\Extension\Core\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Intl\Intl;
use Symfony\Component\Locale\Locale;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

Copy link
Contributor

Choose a reason for hiding this comment

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

Missing doc block

Copy link
Author

Choose a reason for hiding this comment

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

@bschussek All form types are without it. do you still want me to add it?

Copy link
Contributor

Choose a reason for hiding this comment

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

bad examples are still bad examples :)

class CurrencyType extends AbstractType
{
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'choices' => Intl::getCurrencyBundle()->getCurrencyNames(),
));
}

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

/**
* {@inheritdoc}
*/
public function getName()
{
return 'currency';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?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;

use Symfony\Component\Form\Extension\Core\View\ChoiceView;
use Symfony\Component\Intl\Util\IntlTestHelper;

class CurrencyTypeTest extends TypeTestCase
{
protected function setUp()
{
IntlTestHelper::requireIntl($this);

parent::setUp();
}

public function testCurrenciesAreSelectable()
{
$form = $this->factory->create('currency');
$view = $form->createView();
$choices = $view->vars['choices'];

$this->assertContains(new ChoiceView('EUR', 'EUR', 'Euro'), $choices, '', false, false);
$this->assertContains(new ChoiceView('USD', 'USD', 'US Dollar'), $choices, '', false, false);
$this->assertContains(new ChoiceView('SIT', 'SIT', 'Slovenian Tolar'), $choices, '', false, false);
}

}
26 changes: 26 additions & 0 deletions src/Symfony/Component/Validator/Constraints/Currency.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
* @Annotation
*
* @author Miha Vrhovnik <[email protected]>
*
* @api
*/
class Currency extends Constraint
{
public $message = 'This value is not a valid currency.';
}
48 changes: 48 additions & 0 deletions src/Symfony/Component/Validator/Constraints/CurrencyValidator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Validator\Constraints;

use Symfony\Component\Intl\Intl;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;

/**
* Validates whether a value is a valid currency
*
* @author Miha Vrhovnik <[email protected]>
*
* @api
*/
class CurrencyValidator extends ConstraintValidator
{
/**
* {@inheritDoc}
*/
public function validate($value, Constraint $constraint)
{
if (null === $value || '' === $value) {
return;
}

if (!is_scalar($value) && !(is_object($value) && method_exists($value, '__toString'))) {
throw new UnexpectedTypeException($value, 'string');
}

$value = (string) $value;
$currencies = Intl::getCurrencyBundle()->getCurrencyNames();

if (!isset($currencies[$value])) {
$this->context->addViolation($constraint->message, array('{{ value }}' => $value));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,10 @@
<source>This value is not a valid ISSN.</source>
<target>This value is not a valid ISSN.</target>
</trans-unit>
<trans-unit id="64">
<source>This value is not a valid currency.</source>
<target>This value is not a valid currency.</target>
</trans-unit>
</body>
</file>
</xliff>
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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\Validator\Tests\Constraints;

use Symfony\Component\Intl\Util\IntlTestHelper;
use Symfony\Component\Validator\Constraints\Currency;
use Symfony\Component\Validator\Constraints\CurrencyValidator;

class CurrencyValidatorTest extends \PHPUnit_Framework_TestCase
{
protected $context;
protected $validator;

protected function setUp()
{
IntlTestHelper::requireIntl($this);

$this->context = $this->getMock('Symfony\Component\Validator\ExecutionContext', array(), array(), '', false);
$this->validator = new CurrencyValidator();
$this->validator->initialize($this->context);
}

protected function tearDown()
{
$this->context = null;
$this->validator = null;
}

public function testNullIsValid()
{
$this->context->expects($this->never())
->method('addViolation');

$this->validator->validate(null, new Currency());
}

public function testEmptyStringIsValid()
{
$this->context->expects($this->never())
->method('addViolation');

$this->validator->validate('', new Currency());
}

/**
* @expectedException \Symfony\Component\Validator\Exception\UnexpectedTypeException
*/
public function testExpectsStringCompatibleType()
{
$this->validator->validate(new \stdClass(), new Currency());
}

/**
* @dataProvider getValidCurrencies
*/
public function testValidCurrencies($currency)
{
$this->context->expects($this->never())
->method('addViolation');

$this->validator->validate($currency, new Currency());
}

public function getValidCurrencies()
{
return array(
array('EUR'),
array('USD'),
array('SIT'),
array('AUD'),
array('CAD'),
);
}

/**
* @dataProvider getInvalidCurrencies
*/
public function testInvalidCurrencies($currency)
{
$constraint = new Currency(array(
'message' => 'myMessage'
));

$this->context->expects($this->once())
->method('addViolation')
->with('myMessage', array(
'{{ value }}' => $currency,
));

$this->validator->validate($currency, $constraint);
}

public function getInvalidCurrencies()
{
return array(
array('EN'),
array('foobar'),
);
}
}