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 8196177
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:23:46+00:00 2026-06-07T05:23:46+00:00

I am building a form class in Symfony2. In my class, I have a

  • 0

I am building a form class in Symfony2. In my class, I have a choice field. I built a function to return my choice array:

public function getCardTypes()  {
    return array('visa' => 'Visa', 'mc' => 'MasterCard', 'amex' => 'American Express');
}

Later, I add a choice field to my form with this array:

$builder->add('PaymentCCType', 'choice', array('choices' => $this->getCardTypes()));

And then in getDefaultOptions function I have a choice constraint for this field:

'PaymentCCType' => new Choice(array('choices' => $this->getCardTypes())),

I seem to be having a problem with this validator. When I submit this form, I get the following error underneath my select box: “The value you selected is not a valid choice”. Of course, I am using one of the choices in my array.

What am I doing wrong?

/* edit */

I have noticed that of the 4 fields I have like this, I only get the error on 3 of them. the one where the choice is month (simple 1-12), validation works.

/* edit 2 */

the issue appears to occur when the array key does not match the value. i switched my array to array('Visa' => 'Visa', 'MasterCard' => 'MasterCard', 'American Express' => 'American Express') and now it works.
Is there any way around this? I feel like I can’t be the only one with this issue. it occurs even when you have a regular (non-associative) array like array('Visa', 'MasterCard', 'American Express')

  • 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-07T05:23:49+00:00Added an answer on June 7, 2026 at 5:23 am

    edit
    In response to my issue, the Symfony team pointed out the choice validator accepts an array of possible values (not possible choices like the choice field). the easiest way to do this is to use the array_keys function:

    'PaymentCCType' => new Choice(array('choices' => array_keys($this->getCardTypes()))),

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

//--form timesheettype---it is not entity class class TimeSheetType extends AbstractType { public function buildForm(FormBuilder
I have a query building form built in PHP. The results of the query
I'm building a form which contains a category field. I need a choice list
I'm building a form class in python for producing and validating HTML forms. Each
I'm building my first CodeIgniter app - and have a registration form. I'm using
I'm building a form wizard. On the first step I have a form with
I'm building a contact form and I need help with the jQuery validator. function
If I create a new PHP class e.g. to simplify form building (yes I
I'm building a Zend_Form.Having: $file=new Zend_Form_Element_File('file'); $file->setDescription('upload a picture:') ->setDestination(myPath); $file->class=media[]; $file->setDecorators(array( 'File', array('Description',array('placement'=>'PREPEND','tag'=>'')),
I'm building a form for a :has_many :through relationship: class Account < ActiveRecord::Base has_many

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.