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

Skip to content

[Form] Adding attributes to ChoiceView #9694

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 @@ -96,7 +96,7 @@
{{ block('choice_widget_options') }}
</optgroup>
{% else %}
<option value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
<option {{ block('option_data_attributes') }} value="{{ choice.value }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
{% endif %}
{% endfor %}
{% endspaceless %}
Expand Down Expand Up @@ -423,3 +423,16 @@
{%- endfor -%}
{% endspaceless %}
{% endblock button_attributes %}

{% block option_data_attributes %}
{% spaceless %}
{%- for attrname, attrvalue in choice.attr -%}
{{- " " -}}
data-{{attrname}}="{{attrvalue}}"
{%- endfor -%}

{% if choice.prototype %}
data-prototype="{{ form_row(choice.prototype)|e }}"
{% endif %}
{% endspaceless %}
{% endblock option_data_attributes %}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
<?php echo $formHelper->block($form, 'choice_widget_options', array('choices' => $choice)) ?>
</optgroup>
<?php else: ?>
<option value="<?php echo $view->escape($choice->value) ?>"<?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape($translatorHelper->trans($choice->label, array(), $translation_domain)) ?></option>
<option <?php echo $formHelper->block($form, 'option_data_attributes', array('choice' => $choice, 'formHelper' => $formHelper)) ?>value="<?php echo $view->escape($choice->value) ?>"<?php if ($is_selected($choice->value, $value)): ?> selected="selected"<?php endif?>><?php echo $view->escape($translatorHelper->trans($choice->label, array(), $translation_domain)) ?></option>
<?php endif ?>
<?php endforeach ?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?php foreach ($choice->attr as $k => $v): ?>
data-<?php echo $k ?>="<?php echo $v ?>"
<?php endforeach ?>
<?php if ($choice->prototype): ?>
data-prototype="<?php echo $view->escape($formHelper->block($choice->prototype, 'form_row')) ?>"
<?php endif ?>
57 changes: 56 additions & 1 deletion src/Symfony/Component/Form/Extension/Core/Type/ChoiceType.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\Form\Exception\LogicException;
use Symfony\Component\Form\Exception\UnexpectedTypeException;
use Symfony\Component\Form\Extension\Core\ChoiceList\SimpleChoiceList;
use Symfony\Component\Form\Extension\Core\EventListener\FixRadioInputListener;
use Symfony\Component\Form\Extension\Core\EventListener\FixCheckboxInputListener;
Expand All @@ -26,6 +27,7 @@
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToValuesTransformer;
use Symfony\Component\Form\Extension\Core\DataTransformer\ChoicesToBooleanArrayTransformer;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ChoiceType extends AbstractType
Expand All @@ -36,6 +38,11 @@ class ChoiceType extends AbstractType
*/
private $choiceListCache = array();

/**
* @var array
*/
private $choicesPrototypeForm = array();

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -88,6 +95,23 @@ public function buildForm(FormBuilderInterface $builder, array $options)
// transformation is merged back into the original collection
$builder->addEventSubscriber(new MergeCollectionListener(true, true));
}

if ($options['choices_prototypes']) {
foreach ($options['choices_prototypes'] as $prototype) {
$resoler = new OptionsResolver();
$resoler
->setDefaults(array('options' => array()))
->setRequired(array('name', 'type'))
->setAllowedTypes(array(
'name' => array('string'),
'type' => array('string', 'Symfony\Component\Form\FormTypeInterface'),
'options' => array('array'),
))
;
$prototype = $resoler->resolve($prototype);
$this->choicesPrototypeForm[] = $builder->create($prototype['name'], $prototype['type'], $prototype['options'])->getForm();
}
}
}

/**
Expand Down Expand Up @@ -132,6 +156,18 @@ public function buildView(FormView $view, FormInterface $form, array $options)
// POST request.
$view->vars['full_name'] = $view->vars['full_name'].'[]';
}

if (!empty($options['choices_attributes']) || !empty($options['choices_prototypes'])) {
foreach ($view->vars['choices'] as $key => $choiceView) {
if (array_key_exists($key, $options['choices_attributes'])) {
$choiceView->attr = $options['choices_attributes'][$key];
}

if (array_key_exists($key, $this->choicesPrototypeForm) && $this->choicesPrototypeForm[$key] instanceof FormInterface) {
$choiceView->prototype = $this->choicesPrototypeForm[$key]->createView($options['choices_prototypes_view']($view));
}
}
}
}

/**
Expand Down Expand Up @@ -203,6 +239,14 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
return $emptyValue;
};

$choicesClosureNormalizer = function (Options $options, $values) {
if ($values instanceof \Closure && !is_array($values = $values($options['choice_list']))) {
throw new UnexpectedTypeException($values, 'array');
}

return $values;
};

$compound = function (Options $options) {
return $options['expanded'];
};
Expand All @@ -221,14 +265,25 @@ public function setDefaultOptions(OptionsResolverInterface $resolver)
// is manually set to an object.
// See https://github.com/symfony/symfony/pull/5582
'data_class' => null,
// All options related to choices extra data
'choices_attributes' => array(),
'choices_prototypes' => array(),
'choices_prototypes_view' => function ($view) {
return $view->parent;
}
));

$resolver->setNormalizers(array(
'empty_value' => $emptyValueNormalizer,
'choices_attributes' => $choicesClosureNormalizer,
'choices_prototypes' => $choicesClosureNormalizer
));

$resolver->setAllowedTypes(array(
'choice_list' => array('null', 'Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface'),
'choice_list' => array('null', 'Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface'),
'choices_attributes' => array('array', '\Closure'),
'choices_prototypes' => array('array', '\Closure'),
'choices_prototypes_view' => array('\Closure'),
));
}

Expand Down
14 changes: 14 additions & 0 deletions src/Symfony/Component/Form/Extension/Core/View/ChoiceView.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,20 @@
*/
class ChoiceView
{
/**
* Attributes which can be used in view.
*
* @var array
*/
public $attr = array();

/**
* A prototype who can be used in view.
*
* @var FormView
*/
public $prototype;

/**
* The original choice value.
*
Expand Down