Skip to content

Commit

Permalink
actual statue
Browse files Browse the repository at this point in the history
  • Loading branch information
PoisonousJohn committed Jun 19, 2012
1 parent 1ec6316 commit b2281b1
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 3 deletions.
22 changes: 22 additions & 0 deletions Form/Core/ChoiceList/AjaxArrayChoiceList.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,26 @@ public function getIntersect(array $values)

return $intersect;
}

/**
* Get intersection $choices to $values
* including all freeValues
* @param array $values
*
* @return array $intersect
*/

public function getIntersectFreeValues(array $values)
{
$intersect = array();

foreach ($values as $value) {
$intersect[] = array(
'value' => $value,
'label' => $value
);
}

return $intersect;
}
}
11 changes: 9 additions & 2 deletions Form/Core/DataTransformer/ChoiceToJsonTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,15 @@ class ChoiceToJsonTransformer implements DataTransformerInterface
* @param string $widget
* @param boolean $multiple
* @param boolean $ajax
* @param boolean $freeValues
*/
public function __construct(ArrayChoiceList $choiceList, $widget = 'choice', $multiple = false, $ajax = false)
public function __construct(ArrayChoiceList $choiceList, $widget = 'choice', $multiple = false, $ajax = false, $freeValues = false)
{
$this->choiceList = $choiceList;
$this->multiple = $multiple;
$this->widget = $widget;
$this->ajax = $ajax;
$this->freeValues = $freeValues;
}

/**
Expand All @@ -60,7 +62,12 @@ public function transform($choices)
throw new UnexpectedTypeException($choices, 'array');
}

$json = $this->choiceList->getIntersect($choices);
if (false === $this->freeValues) {
$json = $this->choiceList->getIntersect($choices);
} else {
$json = $this->choiceList->getIntersectFreeValues($choices);
}


if (false === $this->multiple) {
$json = current($json);
Expand Down
5 changes: 4 additions & 1 deletion Form/JQuery/Type/AutocompleterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ public function buildForm(FormBuilder $builder, array $options)
$options['choice_list'],
$options['widget'],
$options['multiple'],
$options['ajax']
$options['ajax'],
$options['freeValues']
))
->setAttribute('choice_list', $options['choice_list'])
->setAttribute('widget', $options['widget'])
->setAttribute('route_name', $options['route_name'])
->setAttribute('freeValues', $options['freeValues'])
->setAttribute('ids', $options['ids'])
;
}
Expand All @@ -68,6 +70,7 @@ public function buildView(FormView $view, FormInterface $form)
$view
->set('autocompleter_value', $value)
->set('route_name', $form->getAttribute('route_name'))
->set('freeValues', $form->getAttribute('freeValues'))
->set('ids', $form->getAttribute('ids'))
->set('form_name', $form->getRoot()->getName())
;
Expand Down
11 changes: 11 additions & 0 deletions Resources/views/Form/jquery_layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,17 @@
$autocompleter.autocomplete($configs);
{% if freeValues %}
$autocompleter.keyup(function(){
var val ={}
var fieldval = $(this).val();
val['value'] = fieldval;
val['label'] = fieldval;
$field.val(JSON.stringify(val));
});
{% endif %}
{% if multiple %}
var $source = $autocompleter.data('autocomplete').source;
Expand Down

0 comments on commit b2281b1

Please sign in to comment.