DCSSelectCityFormFieldBundle

DCSSelectCityFormFieldBundle 是一个用于 Symfony2 的捆绑包,它增加了一个新的表单字段类型 select_city,可以在你的表单中呈现三个选择(国家,地区,城市)。「The DCSSelectCityFormFieldBundle is a bundle for Symfony2 that adds a new form field type select_city that renders three selects (Country, Region, City) in your forms」

  • Owner: damianociarla/DCSSelectCityFormFieldBundle
  • Platform: BSD, Linux, Mac, Windows
  • License:: MIT License
  • Category::
  • Topic:
  • Like:
    0
      Compare:

Github stars Tracking Chart

DCSSelectCityFormFieldBundle

DCSSelectCityFormFieldBundle 是 Symfony2 的一个包,它添加了一个新的表单字段类型 select_city,它在表单中呈现三个选择(Country,Region,City)。功能包括:

  • 不使用外部库的 Ajax 支持
  • 您可以设置最低安全角色以保护 api
  • 在提交表单时,您有一个代表三个选择的默认对象

1)安装

A)下载并安装 DCSSelectCityFormFieldBundle

要安装 DCSSelectCityFormFieldBundle,请运行以下命令

  bash $ php composer.phar需要damianociarla / select-city-form-field-bundle

B)启用捆绑包

要启用它,请在内核中添加 bundle 实例:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new DCS\Form\SelectCityFormFieldBundle\DCSFormSelectCityFormFieldBundle(),
    );
}

2)创建您的国家,地区和城市类

在第一个版本中,DCSSelectCityFormFieldBundle 仅支持 Doctrine ORM。您必须提供具体的国家、地区和城市类。您必须扩展捆绑包提供的抽象实体并创建适当的映射。

国家

<?php
// src/MyProject/MyBundle/Entity/Country.php

namespace MyProject\MyBundle\Entity;

use DCS\Form\SelectCityFormFieldBundle\Entity\Country as CountryBase;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Country extends CountryBase
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

区域

<?php
// src/MyProject/MyBundle/Entity/Region.php

namespace MyProject\MyBundle\Entity;

use DCS\Form\SelectCityFormFieldBundle\Entity\Region as RegionBase;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Region extends RegionBase
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

城市

<?php
// src/MyProject/MyBundle/Entity/City.php

namespace MyProject\MyBundle\Entity;

use DCS\Form\SelectCityFormFieldBundle\Entity\City as CityBase;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class City extends CityBase
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

3)配置您的应用程序

# app/config/config.yml

dcs_form_select_city_form_field:
    db_driver: orm
    api_security: IS_AUTHENTICATED_ANONYMOUSLY
    model:
        country: MyProject\MyBundle\Entity\Country
        region: MyProject\MyBundle\Entity\Region
        city: MyProject\MyBundle\Entity\City

4)导入 DCSRatingBundle 路由

Import the bundle routing:

dcs_form_select_city_form_field:
    resource: "@DCSFormSelectCityFormFieldBundle/Resources/config/routing.xml"
    prefix:   /

5)在模板中导入 javascript

要导入 javascript,请运行以下命令:

  bash $ php app/console assets:install

并在模板中加入 javascript:

 <script type="text/javascript" src="{{ asset('bundles/dcsformselectcityformfield/js/select_city.js') }}"></script>

6)使用新表单字段

要在表单中包含新字段,请使用以下代码:

  $builder->add('address', 'select_city')

您有以下选择:

  $builder->add('address', 'select_city', array(
    'country_required'  => true,
    'region_required'   => true,
    'city_required'     => true,
))

提交后

提交表单时,bundle 提供了一个表示三个选择的对象。对象是: DCS\Form\SelectCityFormFieldBundle\Model\SelectData

一个完整的例子

实体示例和表示新表单字段返回的信息的表单:

地址实体

<?php
// src/MyProject/MyBundle/Entity/Address.php

namespace MyProject\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Address
{
    /**
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\Country")
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
     */
    protected $country;

    /**
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\Region")
     * @ORM\JoinColumn(name="region_id", referencedColumnName="id", nullable=true)
     */
    protected $region;

    /**
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\City")
     * @ORM\JoinColumn(name="city_id", referencedColumnName="id", nullable=true)
     */
    protected $city;

    /**
     * @var \DCS\Form\SelectCityFormFieldBundle\Model\SelectData
     */
    protected $selectData;

    /**
     * Set country
     *
     * @param \DCS\Form\SelectCityFormFieldBundle\Model\CountryInterface $country
     * @return Address
     */
    public function setCountry(\DCS\Form\SelectCityFormFieldBundle\Model\CountryInterface $country = null)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return \DCS\Form\SelectCityFormFieldBundle\Model\CountryInterface
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Set region
     *
     * @param \DCS\Form\SelectCityFormFieldBundle\Model\RegionInterface $region
     * @return Address
     */
    public function setRegion(\DCS\Form\SelectCityFormFieldBundle\Model\RegionInterface $region = null)
    {
        $this->region = $region;

        return $this;
    }

    /**
     * Get region
     *
     * @return \DCS\Form\SelectCityFormFieldBundle\Model\RegionInterface
     */
    public function getRegion()
    {
        return $this->region;
    }

    /**
     * Set city
     *
     * @param \DCS\Form\SelectCityFormFieldBundle\Model\CityInterface $city
     * @return StandardAddress
     */
    public function setCity(\DCS\Form\SelectCityFormFieldBundle\Model\CityInterface $city = null)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city
     *
     * @return \DCS\Form\SelectCityFormFieldBundle\Model\CityInterface
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set selectData
     *
     * @param \DCS\Form\SelectCityFormFieldBundle\Model\SelectData $selectData
     * @return Address
     */
    public function setSelectData(\DCS\Form\SelectCityFormFieldBundle\Model\SelectData $selectData)
    {
        $this->setCountry($selectData->getCountry());
        $this->setRegion($selectData->getRegion());
        $this->setCity($selectData->getCity());

        $this->selectData = $selectData;

        return $this;
    }

    /**
     * Get selectData
     *
     * @return \DCS\Form\SelectCityFormFieldBundle\Model\SelectData
     */
    public function getSelectData()
    {
        $selectData = new \DCS\Form\SelectCityFormFieldBundle\Model\SelectData();
        $selectData->setCountry($this->getCountry());
        $selectData->setRegion($this->getRegion());
        $selectData->setCity($this->getCity());

        return $selectData;
    }
}

地址表单

<?php
// src/MyProject/MyBundle/Form/Type/Address.php

namespace MyProject\MyBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;

class AddressFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('selectData', 'select_city', array(
                'country_required'  => $options['country_required'],
                'state_required'    => $options['state_required'],
                'city_required'     => $options['city_required'],
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'                => 'MyProject\MyBundle\Entity\Address',
            'country_required'          => true,
            'state_required'            => true,
            'city_required'             => true,
        ));

        $resolver->setAllowedTypes(array(
            'country_required'          => 'bool',
            'state_required'            => 'bool',
            'city_required'             => 'bool',
        ));
    }

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


Main metrics

Overview
Name With Ownerdamianociarla/DCSSelectCityFormFieldBundle
Primary LanguagePHP
Program languagePHP (Language Count: 2)
PlatformBSD, Linux, Mac, Windows
License:MIT License
所有者活动
Created At2014-01-04 13:11:56
Pushed At2017-07-22 13:12:08
Last Commit At2015-04-28 17:40:00
Release Count3
Last Release Namev1.0.2 (Posted on )
First Release Namev1.0 (Posted on )
用户参与
Stargazers Count10
Watchers Count1
Fork Count3
Commits Count13
Has Issues Enabled
Issues Count2
Issue Open Count2
Pull Requests Count0
Pull Requests Open Count0
Pull Requests Close Count1
项目设置
Has Wiki Enabled
Is Archived
Is Fork
Is Locked
Is Mirror
Is Private

DCSSelectCityFormFieldBundle

The DCSSelectCityFormFieldBundle is a bundle for Symfony2 that adds a new form field type select_city that renders three selects (Country, Region, City) in your forms. Features include:

  • Ajax support without the use of external libraries
  • You can set a minimum security role to protect api
  • At the submit of form you have a default object that represent the three select

1) Installation

A) Download and install DCSSelectCityFormFieldBundle

To install DCSSelectCityFormFieldBundle run the following command

bash $ php composer.phar require damianociarla/select-city-form-field-bundle

B) Enable the bundle

To enable it add the bundle instance in the kernel:

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
    	// ...
    	new DCS\Form\SelectCityFormFieldBundle\DCSFormSelectCityFormFieldBundle(),
	);
}

2) Create your Country, Region and City classes

In this first release DCSSelectCityFormFieldBundle supports only Doctrine ORM. You must provide a concrete Country, Region and City class. You must extend the abstract entities provided by the bundle and creating the appropriate mapping.

Country

<?php
// src/MyProject/MyBundle/Entity/Country.php

namespace MyProject\MyBundle\Entity;

use DCS\Form\SelectCityFormFieldBundle\Entity\Country as CountryBase;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Country extends CountryBase
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

Region

<?php
// src/MyProject/MyBundle/Entity/Region.php

namespace MyProject\MyBundle\Entity;

use DCS\Form\SelectCityFormFieldBundle\Entity\Region as RegionBase;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Region extends RegionBase
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

City

<?php
// src/MyProject/MyBundle/Entity/City.php

namespace MyProject\MyBundle\Entity;

use DCS\Form\SelectCityFormFieldBundle\Entity\City as CityBase;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class City extends CityBase
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
}

3) Configure your application

# app/config/config.yml

dcs_form_select_city_form_field:
    db_driver: orm
    api_security: IS_AUTHENTICATED_ANONYMOUSLY
    model:
        country: MyProject\MyBundle\Entity\Country
        region: MyProject\MyBundle\Entity\Region
        city: MyProject\MyBundle\Entity\City

4) Import DCSRatingBundle routing

Import the bundle routing:

dcs_form_select_city_form_field:
    resource: "@DCSFormSelectCityFormFieldBundle/Resources/config/routing.xml"
    prefix:   /

5) Import javascript in your template

To import the javascript run the following command:

bash $ php app/console assets:install

and include the javascript in your template:

<script type="text/javascript" src="{{ asset('bundles/dcsformselectcityformfield/js/select_city.js') }}"></script>

6) Use the new form field

To include the new field in a form use the following code:

$builder->add('address', 'select_city')

You have the following options:

$builder->add('address', 'select_city', array(
	'country_required'  => true,
    'region_required'   => true,
    'city_required'     => true,
))

After submit

When the form is submitted the bundle provides an object that represent the three selects. The object is: DCS\Form\SelectCityFormFieldBundle\Model\SelectData

A complete example

Example of an entity and a form that represents the information returned by the new form field:

Address entity

<?php
// src/MyProject/MyBundle/Entity/Address.php

namespace MyProject\MyBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 */
class Address
{
    /**
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\Country")
     * @ORM\JoinColumn(name="country_id", referencedColumnName="id", nullable=true)
     */
    protected $country;

    /**
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\Region")
     * @ORM\JoinColumn(name="region_id", referencedColumnName="id", nullable=true)
     */
    protected $region;

    /**
     * @ORM\ManyToOne(targetEntity="MyProject\MyBundle\Entity\City")
     * @ORM\JoinColumn(name="city_id", referencedColumnName="id", nullable=true)
     */
    protected $city;

    /**
     * @var \DCS\Form\SelectCityFormFieldBundle\Model\SelectData
     */
    protected $selectData;

    /**
     * Set country
     *
     * @param \DCS\Form\SelectCityFormFieldBundle\Model\CountryInterface $country
     * @return Address
     */
    public function setCountry(\DCS\Form\SelectCityFormFieldBundle\Model\CountryInterface $country = null)
    {
        $this->country = $country;

        return $this;
    }

    /**
     * Get country
     *
     * @return \DCS\Form\SelectCityFormFieldBundle\Model\CountryInterface
     */
    public function getCountry()
    {
        return $this->country;
    }

    /**
     * Set region
     *
     * @param \DCS\Form\SelectCityFormFieldBundle\Model\RegionInterface $region
     * @return Address
     */
    public function setRegion(\DCS\Form\SelectCityFormFieldBundle\Model\RegionInterface $region = null)
    {
        $this->region = $region;

        return $this;
    }

    /**
     * Get region
     *
     * @return \DCS\Form\SelectCityFormFieldBundle\Model\RegionInterface
     */
    public function getRegion()
    {
        return $this->region;
    }

    /**
     * Set city
     *
     * @param \DCS\Form\SelectCityFormFieldBundle\Model\CityInterface $city
     * @return StandardAddress
     */
    public function setCity(\DCS\Form\SelectCityFormFieldBundle\Model\CityInterface $city = null)
    {
        $this->city = $city;

        return $this;
    }

    /**
     * Get city
     *
     * @return \DCS\Form\SelectCityFormFieldBundle\Model\CityInterface
     */
    public function getCity()
    {
        return $this->city;
    }

    /**
     * Set selectData
     *
     * @param \DCS\Form\SelectCityFormFieldBundle\Model\SelectData $selectData
     * @return Address
     */
    public function setSelectData(\DCS\Form\SelectCityFormFieldBundle\Model\SelectData $selectData)
    {
        $this->setCountry($selectData->getCountry());
        $this->setRegion($selectData->getRegion());
        $this->setCity($selectData->getCity());

        $this->selectData = $selectData;

        return $this;
    }

    /**
     * Get selectData
     *
     * @return \DCS\Form\SelectCityFormFieldBundle\Model\SelectData
     */
    public function getSelectData()
    {
        $selectData = new \DCS\Form\SelectCityFormFieldBundle\Model\SelectData();
        $selectData->setCountry($this->getCountry());
        $selectData->setRegion($this->getRegion());
        $selectData->setCity($this->getCity());

        return $selectData;
    }
}

Address form

<?php
// src/MyProject/MyBundle/Form/Type/Address.php

namespace MyProject\MyBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormBuilderInterface;

class AddressFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('selectData', 'select_city', array(
                'country_required'  => $options['country_required'],
                'state_required'    => $options['state_required'],
                'city_required'     => $options['city_required'],
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class'                => 'MyProject\MyBundle\Entity\Address',
            'country_required'          => true,
            'state_required'            => true,
            'city_required'             => true,
        ));

        $resolver->setAllowedTypes(array(
            'country_required'          => 'bool',
            'state_required'            => 'bool',
            'city_required'             => 'bool',
        ));
    }

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