StringToFloatTransformer example


    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->addViewTransformer(new NumberToLocalizedStringTransformer(
            $options['scale'],
            $options['grouping'],
            $options['rounding_mode'],
            $options['html5'] ? 'en' : null
        ));

        if ('string' === $options['input']) {
            $builder->addModelTransformer(new StringToFloatTransformer($options['scale']));
        }
    }

    /** * @return void */
    public function buildView(FormView $view, FormInterface $form, array $options)
    {
        if ($options['html5']) {
            $view->vars['type'] = 'number';

            
'1.', 1.],
            ['1.0', 1.],
            ['1.23', 1.23],
        ];
    }

    /** * @dataProvider provideTransformations */
    public function testTransform($from$to)
    {
        $transformer = new StringToFloatTransformer();

        $this->assertSame($to$transformer->transform($from));
    }

    public function testFailIfTransformingANonString()
    {
        $this->expectException(TransformationFailedException::class);
        $transformer = new StringToFloatTransformer();
        $transformer->transform(1.0);
    }

    
Home | Imprint | This part of the site doesn't use cookies.