Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8337981
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:27:20+00:00 2026-06-09T04:27:20+00:00

I have a created a custom form field dropdown list for filtering by year.

  • 0

I have a created a custom form field dropdown list for filtering by year. One of the things I want to do is to allow the user to filter by all years, which is the default option. I am adding this as an empty_value. However, when I render the form, it defaults on the first item that’s not the empty value. The empty value is there, just above it in the list. How do I make the page default to, in my case ‘All’ when the page initially loads? Code is below.

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class YearType extends AbstractType
{

  private $yearChoices;

  public function __construct()
  {
      $thisYear = date('Y');
      $startYear = '2012';

      $this->yearChoices = range($thisYear, $startYear);
  }

  public function getDefaultOptions(array $options)
  {
    return array(
        'empty_value' => 'All',
        'choices' => $this->yearChoices,
    );
  }

  public function getParent(array $options)
  {
    return 'choice';
  }

  public function getName()
  {
    return 'year';
  }
}

I’m rendering my form in twig with a simple {{ form_widget(filter_form) }}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T04:27:22+00:00Added an answer on June 9, 2026 at 4:27 am

    Try adding empty_data option to null, so it comes first. I have many fields of this type and it’s working, for example:

    class GenderType extends \Symfony\Component\Form\AbstractType
    {
    
        public function getDefaultOptions(array $options)
        {
            return array(
                'empty_data'  => null,
                'empty_value' => "Non specificato",
                'choices'     => array('m' => 'Uomo', 'f' => 'Donna'),
                'required'    => false,
            );
        }
    
        public function getParent(array $options) { return 'choice'; }
    
        public function getName() { return 'gender'; }
    
    }
    

    EDIT: Another possibility (i suppose) would be setting preferred_choices. This way you’ll get “All” option to the top. But i don’t know if it can work with null empty_data, but you can change empty_data to whatever you want:

      public function getDefaultOptions(array $options)
      {
        return array(
            'empty_value'       => 'All',
            'empty_data'        => null,
            'choices'           => $this->yearChoices,
            'preferred_choices' => array(null)         // Match empty_data
        );
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created custom posts and I want one page in my site to
I have created custom registration form in drupal 6. i have used changed the
I have created a custom form class and template for my form by following
I have created a custom list. I am using the same list in sharepoint
I have a custom form... I'd like to auto save the author (authenticated user)
i have created a custom form in my custom drupal6 module and the form
I have created a custom list with columns. 1. Location 2. Location_MMYY(Not visible on
I am using CCK on a custom node type. I have created a field
I have created a custom date_Select field using 3 separate select fields: <%= f.select
On a custom form of mine I want to display a custom Guid field.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.