symfony 2.2 - How to overwrite all symfony2 form types and add some property (in version 2.2)? -



symfony 2.2 - How to overwrite all symfony2 form types and add some property (in version 2.2)? -

i want form types have width property, , utilize this:

public function buildform(formbuilderinterface $builder, array $options) { $builder->add('product_name', 'text', array('width' => "small")); } public function buildview(formview $view, forminterface $form, array $options) { parent::buildview($view, $form, $options); if (array_key_exists(self::option_width, $options)) { $view->vars["attr"]["class"] .= " class_1 class_2 " } }

a more generic solution create new form type extension , register types.

the symfony documentation describes how create new form type extension: http://symfony.com/doc/current/cookbook/form/create_form_type_extension.html

but fails mention how register extension all types.

the "trick" specify "field" (or "form" symfony 2.3 , next) extended type (as result in getextendedtype() , alias in service configuration)

// src/acme/demobundle/form/extension/specialwidthtypeextension.php namespace acme\demobundle\form\extension; utilize symfony\component\form\abstracttypeextension; utilize symfony\component\form\formview; utilize symfony\component\form\forminterface; utilize symfony\component\propertyaccess\propertyaccess; utilize symfony\component\optionsresolver\optionsresolverinterface; class specialwidthtypeextension extends abstracttypeextension { /** * returns name of type beingness extended. * * @return string name of type beingness extended */ public function getextendedtype() { homecoming 'field'; // extend types } /** * add together width alternative * * @param optionsresolverinterface $resolver */ public function setdefaultoptions(optionsresolverinterface $resolver) { $resolver->setoptional(array('width')); } /** * pass parameters view * * @param formview $view * @param forminterface $form * @param array $options */ public function buildview(formview $view, forminterface $form, array $options) { if (array_key_exists('width', $options)) { // set "width" variable available when rendering field $view->vars['width'] = $options['width']; } } }

the service configuration:

services: acme_demo_bundle.special_width_type_extension: class: acme\demobundle\form\extension\specialwidthtypeextension tags: - { name: form.type_extension, alias: field }

in twig must check whether new alternative set or not:

{% if width not null %} <div class='col-sm-{{ width }}'> {% endif %}

symfony2 symfony-2.2

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -