How to use ChoiceList in Symfony 2.1?

I have a file containing a list of US states.
Alabama
Alaska
etc.

In symfony 2.0, I used ChoiceListInterface.php to use it in my form. I just wrote this:

<?php namespace MyBundle\Form; use Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface; class StateChoiceList implements ChoiceListInterface { public function getChoices() { $lines = file('listes/us_states.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // fill the array $arr = array(); foreach ($lines as $line) { $arr[$line] = $line; } return $arr; } } 

But now in ChoiceListInterface 7 more functions are implemented:

 public function getValues(); public function getPreferredViews(); public function getRemainingViews(); public function getValuesForChoices(array $choices); public function getIndicesForChoices(array $choices); public function getIndicesForValues(array $values); 

I read the documentation http://api.symfony.com/2.1/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.html , but in my case I find this unclear and I really don’t understand how to implement them.

Can someone help? Thank you very much

+4
source share
1 answer

You can extend LazyChoiceList and implement loadChoiceList () , where you can return a new ChoiceList object filled with the values ​​read from the file.

+5
source

Source: https://habr.com/ru/post/1444435/


All Articles