@@ -83,11 +83,9 @@ to override one of the following methods:
83
83
84
84
* ``buildView() ``
85
85
86
- * ``getDefaultOptions () ``
86
+ * ``setDefaultOptions () ``
87
87
88
- * ``getAllowedOptionValues() ``
89
-
90
- * ``buildViewBottomUp() ``
88
+ * ``finishView() ``
91
89
92
90
For more information on what those methods do, you can refer to the
93
91
:doc: `Creating Custom Field Types</cookbook/form/create_custom_field_type> `
@@ -174,7 +172,7 @@ database)::
174
172
Your form type extension class will need to do two things in order to extend
175
173
the ``file `` form type:
176
174
177
- #. Override the ``getDefaultOptions `` method in order to add an image_path
175
+ #. Override the ``setDefaultOptions `` method in order to add an image_path
178
176
option;
179
177
#. Override the ``buildForm `` and ``buildView `` methods in order to pass the image
180
178
url to the view.
@@ -192,6 +190,7 @@ it in the view::
192
190
use Symfony\Component\Form\FormView;
193
191
use Symfony\Component\Form\FormInterface;
194
192
use Symfony\Component\Form\Util\PropertyPath;
193
+ use Symfony\Component\OptionsResolver\OptionsResolverInterface;
195
194
196
195
class ImageTypeExtension extends AbstractTypeExtension
197
196
{
@@ -208,24 +207,11 @@ it in the view::
208
207
/**
209
208
* Add the image_path option
210
209
*
211
- * @param array $options
210
+ * @param \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver
212
211
*/
213
- public function getDefaultOptions(array $options )
212
+ public function setDefaultOptions(OptionsResolverInterface $resolver )
214
213
{
215
- return array('image_path' => null);
216
- }
217
-
218
- /**
219
- * Store the image_path option as a builder attribute
220
- *
221
- * @param \Symfony\Component\Form\FormBuilder $builder
222
- * @param array $options
223
- */
224
- public function buildForm(FormBuilder $builder, array $options)
225
- {
226
- if (null !== $options['image_path']) {
227
- $builder->setAttribute('image_path', $options['image_path']);
228
- }
214
+ $resolver->setOptional(array('image_path'));
229
215
}
230
216
231
217
/**
@@ -236,10 +222,10 @@ it in the view::
236
222
*/
237
223
public function buildView(FormView $view, FormInterface $form)
238
224
{
239
- if ($form->hasAttribute ('image_path')) {
225
+ if (array_key_exists ('image_path', $options )) {
240
226
$parentData = $form->getParent()->getData();
241
227
242
- $propertyPath = new PropertyPath($form->getAttribute( 'image_path') );
228
+ $propertyPath = new PropertyPath($options[ 'image_path'] );
243
229
$imageUrl = $propertyPath->getValue($parentData);
244
230
// set an "image_url" variable that will be available when rendering this field
245
231
$view->set('image_url', $imageUrl);
@@ -269,7 +255,7 @@ Specifically, you need to override the ``file_widget`` block:
269
255
{% block file_widget %}
270
256
{% spaceless %}
271
257
272
- {{ block('field_widget ') }}
258
+ {{ block('form_widget ') }}
273
259
{% if image_url is not null %}
274
260
<img src="{{ asset(image_url) }}"/>
275
261
{% endif %}
@@ -303,11 +289,11 @@ next to the file field. For example::
303
289
namespace Acme\DemoBundle\Form\Type;
304
290
305
291
use Symfony\Component\Form\AbstractType;
306
- use Symfony\Component\Form\FormBuilder ;
292
+ use Symfony\Component\Form\FormBuilderInterface ;
307
293
308
294
class MediaType extends AbstractType
309
295
{
310
- public function buildForm(FormBuilder $builder, array $options)
296
+ public function buildForm(FormBuilderInterface $builder, array $options)
311
297
{
312
298
$builder
313
299
->add('name', 'text')
0 commit comments