2

嗨,我正在处理一个 Sf2 项目,并且我在整个应用程序中重复使用了一个包含美国州邮政编码的数组——在控制器、验证和表单类中。将这个和类似的“全局”数据存储在一个地方的最佳位置是什么?我正在考虑扩展 Base Controller 类并将其放在那里,但我想我会看到社区的想法。谢谢!

4

2 回答 2

2

对于美国州邮政编码,我会创建一个自定义的 FormType,例如Acme\YourBundle\Form\UsPostalCodeType,它可以用于使用 Symfony 的 FormBuilder 创建一个下拉框。

我有一个用于英格兰县列表的。

<?php

namespace Acme\SomethingBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

class EnglandCountyType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'choices' => self::getCounties()
        ));
    }

    public function getParent()
    {
        return 'choice';
    }

    public function getName()
    {
        return 'england_county';
    }

    /**
     * A custom method to apply the values of the array as the keys also and return that array
     *
     * @return array
     */
    public static function getCounties()
    {
        $choices = array(
            "Greater London",
            "West Midlands",
            "Greater Manchester",
            "West Yorkshire",
            "Kent",
            "Essex",
            "Merseyside",
            "South Yorkshire",
            "Hampshire",
            "Lancashire",
            "Surrey",
            "Hertfordshire",
            "Tyne and Wear",
            "Norfolk",
            "Staffordshire",
            "West Sussex",
            "Nottinghamshire",
            "Derbyshire",
            "Devon",
            "Suffolk",
            "Lincolnshire",
            "Northamptonshire",
            "Oxfordshire",
            "Leicestershire",
            "Cambridgeshire",
            "North Yorkshire",
            "Gloucestershire",
            "Worcestershire",
            "Warwickshire",
            "Cornwall",
            "Somerset",
            "East Sussex",
            "County Durham",
            "Buckinghamshire",
            "Cumbria",
            "Wiltshire",
            "Bristol",
            "Dorset",
            "Cheshire East",
            "East Riding of Yorkshire",
            "Leicester",
            "Cheshire West and Chester",
            "Northumberland",
            "Shropshire",
            "Nottingham",
            "Brighton & Hove",
            "Medway",
            "South Gloucestershire",
            "Plymouth",
            "Hull",
            "Central Bedfordshire",
            "Milton Keynes",
            "Derby",
            "Stoke-on-Trent",
            "Southampton",
            "Swindon",
            "Portsmouth",
            "Luton",
            "North Somerset",
            "Warrington",
            "York",
            "Stockton-on-Tees",
            "Peterborough",
            "Herefordshire",
            "Bournemouth",
            "Bath and North East Somerset",
            "Southend-on-Sea",
            "North Lincolnshire",
            "Telford and Wrekin",
            "North East Lincolnshire",
            "Thurrock",
            "Bedford",
            "Reading",
            "Wokingham",
            "West Berkshire",
            "Poole",
            "Blackburn with Darwen",
            "Windsor and Maidenhead",
            "Blackpool",
            "Slough",
            "Middlesbrough",
            "Isle of Wight",
            "Redcar and Cleveland",
            "Torbay",
            "Halton",
            "Bracknell Forest",
            "Darlington",
            "Hartlepool",
            "Rutland",
            "Isles of Scilly",
        );

        asort($choices);

        return array_combine($choices, $choices);
    }
}

要对其进行验证,您可以调用将选项数组作为回调返回的静态方法

# src/Acme/SomethingBundle/Resources/config/validation.yml
Acme\SomethingBundle\Entity\Person:
    properties:
        county:
            - Choice: { callback: [\Acme\SomethingBundle\Form\EnglandCountyType, getCounties] }

或者如果在您的实体中使用注释

use Symfony\Component\Validator\Constraints as Assert;

//....

/**
 * @Assert\Choice(callback={"\Acme\SomethingBundle\Form\EnglandCountyType","getCounties"})
 */
protected $county;

如果您将其与验证组一起使用,这将变为

/**
 * @Assert\Choice(callback={"\Acme\SomethingBundle\Form\EnglandCountyType","getCounties"})
 * @Assert\NotBlank(groups={"group1"})
 */
protected $county;

如果您想在控制器中使用它,例如 foreach 循环

use Acme\SomethingBundle\Form\EnglandCountyType;

$counties = EnglandCountyType::getCounties();
foreach ($counties as $county) {
    //...
}
于 2013-06-08T17:17:52.923 回答
0

我可能会为它创建一个服务,你可以将它注入到任何需要它的地方。

于 2013-06-08T17:56:44.780 回答