user1098965
user1098965

Reputation:

Accessing the original untransformed data after applying a DataTransformer?

Would be possibile to get the original data type after applying the ArrayToStringTransformer to the form field? I can't find any help in Symfony2 documentation here.

That is, i need the original array data type in my Twing template. {{ value }} contains only the already transformed data.

class SMSType extends AbstractType
{

    public function buildForm(FormBuilder $builder, array $options)
    {

        $builder
            ->add('recipient', 'text', array('property_path' => false));
            ->add('tags',      'text');

        $builder->get('tags')
            ->appendClientTransformer(new ArrayToStringTransformer());
    }

    public function getDefaultOptions(array $options)
    {
        return array('required' => false);
    }

    public function getName() { return 'sms'; }

}

The transform is just an array explode/implode:

class ArrayToStringTransformer implements DataTransformerInterface
{

    public function transform($val)
    {
        if (null === $val) return '';

        return implode(',', $val);
    }

    public function reverseTransform($val)
    {
        if (!$val) return null;

        return explode(',', $val);
    }
}

Upvotes: 0

Views: 173

Answers (1)

Cerad
Cerad

Reputation: 48865

Nope. The transformed value is what ends up being passed to your template as part of form. I suppose you could explicitly pass the original tags directly to your template.

Upvotes: 1

Related Questions