Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix ChoiceType terminology #6144

Closed
Closed
Labels
DXFormactionableClear and specific issues ready for anyone to take them.hasPRA Pull Request has already been submitted for this issue.⭐️ EU-FOSSA Hackathonhttps://symfony.com/blog/the-symfony-and-api-platform-hackathon-is-coming
@webmozart

Description

@webmozart

Currently, the ChoiceType documentation is IMO not clearly worded. I know that readability is important, but since this type is quite flexible and complex, being precise is even more important. All code examples and descriptions should be updated to use the following terms:

  • choice: The model value of thechoices array. E.g.:true,false,object (Category), ...
  • key: The key of thechoices array.
  • value: The HTML value. The value is always a string. E.g.:'0','1','yes','no', ... Values must be free of duplicates, since they are used to identify the corresponding choice when submitting the form.

This naming is important since those values are passed from one callable to another:

  1. Thechoice_value callable with signaturefunction ($choice) receives thechoice and returns the stringvalue.
  2. Thechoice_label callable with signaturefunction ($choice, $key, $value) receives thechoice, thekey and thevalue and returns a string label. The default value isfunction ($choice, $key, $value) => $key, i.e. the key of thechoices array is used as label by default.
  3. Thechoice_name callable has the same signature aschoice_label and outputs the form name used for checkboxes/radio buttons.
  4. Thechoice_attr callable has the same signature aschoice_label and outputs the HTML attributes.

Furthermore, thechoice_loader is not completely accurate:

The choice_loader can be used to only partially load the choices in cases where a fully-loaded list is not necessary. This is only needed in advanced cases and would replace the choices option.

I would rephrase this. The choice loader is used to load choices from a data source that can be queried with a query language, such as a database or a search engine. When the choice field is displayed, the full list is loaded, but when it is submitted, only the submitted value is looked up. A second benefit is that different fields that use the sameChoiceLoader instance use the same cached choice list, reducing N queries to 1.

Before/After code examples:

Before:

useSymfony\Component\Form\Extension\Core\Type\ChoiceType;// ...$builder->add('attending', ChoiceType::class,array('choices' =>array('yes' =>true,'no' =>false,'maybe' =>null,    ),'choices_as_values' =>true,'choice_label' =>function ($value,$key,$index) {if ($value ==true) {return'Definitely!';        }returnstrtoupper($key);// or if you want to translate some key//return 'form.choice.'.$key;    },));

After:

useSymfony\Component\Form\Extension\Core\Type\ChoiceType;// ...$builder->add('attending', ChoiceType::class,array('choices' =>array('yes' =>true,'no' =>false,'maybe' =>null,    ),'choices_as_values' =>true,'choice_label' =>function ($choice,$key,$value) {returntrue ===$choice ?'Definitely!' :strtoupper($key);// or if you want to translate some key//return 'form.choice.'.$key;    },));

Before:

useSymfony\Component\Form\Extension\Core\Type\ChoiceType;// ...$builder->add('attending', ChoiceType::class,array('choices' =>array('Yes' =>true,'No' =>false,'Maybe' =>null,    ),'choices_as_values' =>true,'choice_attr' =>function($val,$key,$index) {// adds a class like attending_yes, attending_no, etcreturn ['class' =>'attending_'.strtolower($key)];    },));

After:

useSymfony\Component\Form\Extension\Core\Type\ChoiceType;// ...$builder->add('attending', ChoiceType::class,array('choices' =>array('Yes' =>true,'No' =>false,'Maybe' =>null,    ),'choices_as_values' =>true,'choice_attr' =>function($choice,$key,$value) {// adds a class like attending_yes, attending_no, etcreturn ['class' =>'attending_'.strtolower($key)];    },));

I also recommend to add type hints in the advanced example:

Before:

useSymfony\Component\Form\Extension\Core\Type\ChoiceType;useAppBundle\Entity\Category;// ...$builder->add('category', ChoiceType::class, ['choices' => [newCategory('Cat1'),newCategory('Cat2'),newCategory('Cat3'),newCategory('Cat4'),    ],'choices_as_values' =>true,'choice_label' =>function($category,$key,$index) {/** @var Category $category */returnstrtoupper($category->getName());    },'choice_attr' =>function($category,$key,$index) {return ['class' =>'category_'.strtolower($category->getName())];    },'group_by' =>function($category,$key,$index) {// randomly assign things into 2 groupsreturnrand(0,1) ==1 ?'Group A' :'Group B'    },'preferred_choices' =>function($category,$key,$index) {return$category->getName() =='Cat2' ||$category->getName() =='Cat3';    },]);

After:

useSymfony\Component\Form\Extension\Core\Type\ChoiceType;useAppBundle\Entity\Category;// ...$builder->add('category', ChoiceType::class, ['choices' => [newCategory('Cat1'),newCategory('Cat2'),newCategory('Cat3'),newCategory('Cat4'),    ],'choices_as_values' =>true,'choice_label' =>function(Category$category,$key,$value) {returnstrtoupper($category->getName());    },'choice_attr' =>function(Category$category,$key,$value) {return ['class' =>'category_'.strtolower($category->getName())];    },'group_by' =>function(Category$category,$key,$value) {// randomly assign things into 2 groupsreturnrand(0,1) ==1 ?'Group A' :'Group B'    },'preferred_choices' =>function(Category$category,$key,$index) {return$category->getName() =='Cat2' ||$category->getName() =='Cat3';    },]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    DXFormactionableClear and specific issues ready for anyone to take them.hasPRA Pull Request has already been submitted for this issue.⭐️ EU-FOSSA Hackathonhttps://symfony.com/blog/the-symfony-and-api-platform-hackathon-is-coming

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp