Skip to content

Commit

Permalink
Automatically add css class
Browse files Browse the repository at this point in the history
  • Loading branch information
Ross Keatinge committed Jul 29, 2014
1 parent 4c60d85 commit d53623e
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
6 changes: 4 additions & 2 deletions Form/DataTransformer/EntitiesToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

namespace Tetranz\Select2EntityBundle\Form\DataTransformer;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\EntityManager;
use Symfony\Component\Form\DataTransformerInterface;

Expand All @@ -30,6 +31,7 @@ public function transform($entities)
return '';
}

// return an array of initial values as html encoded json
$data = array();

foreach($entities as $entity) {
Expand All @@ -49,11 +51,11 @@ public function transform($entities)

public function reverseTransform($values)
{
// $values has a leading comma
// remove the 'magic' non-blank value added in fields.html.twig
$values = ltrim($values, 'x,');

if (null === $values || '' === $values) {
return array();
return new ArrayCollection();
}

$ids = explode(',', $values);
Expand Down
2 changes: 2 additions & 0 deletions Form/DataTransformer/EntityToPropertyTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public function transform($entity)
return '';
}

// return the initial values as html encoded json

$text = is_null($this->textProperty)
? (string) $entity
: $entity->{'get' . $this->textProperty}();
Expand Down
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ Here's an example:
$builder
->add('country', 'tetranz_select2entity', [
'multiple' => true,
'attr' => ['class' => 'select2entity'],
'remote_route' => 'tetranz_test_default_countryquery',
'class' => '\Tetranz\TestBundle\Entity\Country',
'text_property' => 'Name',
Expand All @@ -88,8 +87,6 @@ $builder
])
```

You must include `'attr' => ['class' => 'select2entity']` to give it the required class for jQuery to find it. (That will probably be changed soon so it gets added automatically).

##Options##
* `class` is your entity class. Required
* `text_property` This is the entity property used to retrieve the text for existing data. A getter method is created by prepending this with `get` so it probably needs to be proper case. e.g., `Name`.
Expand Down
8 changes: 8 additions & 0 deletions Resources/views/Form/fields.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
'data-placeholder':placeholder
}) %}

{# Add class for jQuery for find it #}
{% set attr = attr|merge({ 'class': (attr.class|default('') ~ ' select2entity')|trim }) %}

{# A bit of a kludge here.
It seems to work more smoothly if we put the initial value into data-value rather than the value attribute.
But ... value needs to be non-blank for initSelection in the js to fire.
We need to remove this value in EntitiesToPropertyTransformer #}
{% set value = 'x' %}

{% set type = type|default('hidden') %}
{{ block('form_widget_simple') }}
{% endblock %}

0 comments on commit d53623e

Please sign in to comment.