1 | <?php |
||
18 | class TeamFormCreator extends ModelFormCreator |
||
19 | { |
||
20 | /** |
||
21 | * {@inheritdoc} |
||
22 | */ |
||
23 | protected function build($builder) |
||
24 | { |
||
25 | $builder->add('name', 'text', array( |
||
26 | 'constraints' => array( |
||
27 | new NotBlank(), new Length(array( |
||
28 | 'min' => 2, |
||
29 | 'max' => 32, // default BZFlag motto length |
||
30 | )) |
||
31 | ) |
||
32 | ))->add('description', 'textarea', array( |
||
33 | 'required' => false |
||
34 | ))->add('avatar', 'file', array( |
||
35 | 'constraints' => new Image(array( |
||
36 | 'minWidth' => 60, |
||
37 | 'minHeight' => 60, |
||
38 | 'maxSize' => '4M' |
||
39 | )), |
||
40 | 'required' => false |
||
41 | )); |
||
42 | |||
43 | if ($this->editing) { |
||
44 | // We are editing the team, not creating it |
||
45 | $team = $this->editing; |
||
46 | |||
47 | $builder->add('delete_avatar', 'submit'); |
||
48 | |||
49 | // Let the user appoint a different leader |
||
50 | $builder->add('leader', new ModelType('Player', false, function ($query) use ($team) { |
||
51 | // Only list players belonging in that team |
||
52 | return $query->where('team')->is($team); |
||
53 | }), array( |
||
54 | 'constraints' => new NotBlank() |
||
55 | )); |
||
56 | } |
||
57 | |||
58 | return $builder->add('status', 'choice', array( |
||
59 | 'choices' => array( |
||
60 | 'open' => 'Open', |
||
61 | 'closed' => 'Closed', |
||
62 | ), |
||
63 | )) |
||
64 | ->add('submit', 'submit', [ |
||
65 | 'attr' => [ |
||
66 | 'class' => 'c-button--blue pattern pattern--downward-stripes', |
||
67 | ], |
||
68 | ]) |
||
69 | ; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function fill($form, $team) |
||
82 | |||
83 | /** |
||
84 | * {@inheritdoc} |
||
85 | */ |
||
86 | public function enter($form) |
||
96 | |||
97 | /** |
||
98 | * {@inheritdoc} |
||
99 | */ |
||
100 | public function update($form, $team) |
||
122 | } |
||
123 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: