0

I have this piece of code:

->add('breedingPairSeason', 'choice', array(
    'placeholder' => 'Choose a season',
    'choices' => array(
        'early2010'   => 'Early 2010 (Jan - June)',
        'late2010' => 'Late 2010 (July - December)',
    ),
));

What I would like to achieve is to automatically add more choices based on the year. So ideally I would like it to start in 2010, and then finish in the current year, like this:

->add('breedingPairSeason', 'choice', array(
    'placeholder' => 'Choose a season',
    'choices' => array(
        'early2010'   => 'Early 2010 (Jan - June)',
        'late2010' => 'Late 2010 (July - December)',
        'early2011'   => 'Early 2011 (Jan - June)',
        'late2011' => 'Late 2011 (July - December)',
        'early2012'   => 'Early 2012 (Jan - June)',
        'late2012' => 'Late 2012 (July - December)',
        'early2013'   => 'Early 2013 (Jan - June)',
        'late2013' => 'Late 2013 (July - December)',
        'early2014'   => 'Early 2014 (Jan - June)',
        'late2014' => 'Late 2014 (July - December)',
        'early2015'   => 'Early 2015 (Jan - June)',
        'late2015' => 'Late 2015 (July - December)',
    ),
));

Is this possible?

rafaelc
  • 57,686
  • 15
  • 58
  • 82
Andy Holmes
  • 7,817
  • 10
  • 50
  • 83
  • The question is unclear for me. – felipsmartins May 18 '15 at 18:52
  • @felipsmartins i dont know how to make it clearer. Ermmmm, so i want the values you see automatically added for each year time goes on. Starting at 2010, through to the current year, like you see in the second block of code. Is it possible to run a loop inside Symfony's FormType to automatically update this? – Andy Holmes May 18 '15 at 18:54
  • 1
    yeah! Is's possible. See my answer bellow. – felipsmartins May 18 '15 at 19:05

2 Answers2

1

You can create simple file for enums or use a private method in your form to achieve this. I'll show you a quick example, but overall, generating the array is up to you if you find a better/cleaner way.

Let's say you create a private method in your form type:

private function _getSeasonChoices() {
    $intervalStart = 2010;
    $intervalEnd = (new \DateTime('now'))->format('Y');

    $choices = array();
    for($year = $intervalStart; $year <= $intervalEnd; $year++) {

        $choices[ sprintf('early%d', $year) ] = sprintf('Early %d (January - June)', $year);
        $choices[ sprintf('late%d', $year) ] = sprintf('Late %d (July - December)', $year);

    }

    return $choices;
}

Then all you have to do is call the method

'choices' => $this->_getSeasonChoices()

and your select input should be filled with the correct values.

Again, generating the array is entirely up to you, if you're not satisfied with this solution. Hope you got the idea.

Artamiel
  • 3,652
  • 2
  • 19
  • 24
1

I would do something like this:

# namespace SomeBundle\Form\Type
/**
 * Generate a choice array from start year to end year
 *
 * @param int $startYear The start year
 * @param int $endYear If null, current year is taken
 * @return array
 */
protected function generateChoices($startYear, $endYear=null)
{
    $endYear = $endYear ? $endYear : date('Y');
    $years   = range($startYear, $endYear);
    $choices = array();

    foreach($years as $year) {
        $choices['early' . $year] = sprintf('Early %d (Jan - June)', $year);
        $choices['late'  . $year] = sprintf('Late %d (July - December)', $year);
    }

    return $choices;
}

And on builder:

//...
->add('breedingPairSeason', 'choice', array(
    'placeholder' => 'Choose a season',
    'choices' => $this->generateChoices(2010)
));
felipsmartins
  • 13,269
  • 4
  • 48
  • 56
  • 1
    Oh, I remember you: http://stackoverflow.com/questions/30292517/show-only-results-equal-to-current-user-id-in-symfony2 – felipsmartins May 18 '15 at 19:07