Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9054259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T13:38:39+00:00 2026-06-16T13:38:39+00:00

I created a custom form type and a transformer that goes with it. Data

  • 0

I created a custom form type and a transformer that goes with it.
Data in my database is saved in 1 Text field. “<EN>English text</EN><FR>Frenc text</FR>“

So I created a formtype that added separate text for each input and the transformer is suposed to put the right value in between the tags inside the input.

the problem is I echoed the value that should be passed in the transform function but it’s empty and I can’t figure out why.

here is my multilang type

class MultiLangType extends AbstractType
{
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'type' => 'text',
            'compound' => 'true'
        ));
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $langs = array('EN', 'FR', 'DE');
        $transformer = new StringToMultiTransformer($langs);
        $builder->prependClientTransformer($transformer);
        //$builder->addModelTransformer(new StringToMultiTransformer($langs));
        foreach($langs as $l)
        {
            $builder->add($l, 'text', $options);
        }

    }

    public function getParent()
    {
        return 'field';
    }

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

and now my transformer

class StringToMultiTransformer implements DataTransformerInterface
{
    private $langs;

    public function __construct(array $langs)
    {
        $this->langs = $langs;
    }

    public function transform($value)
    {
        // var_dump($value); exit;
        $result = array();

        foreach ($this->langs as $l) {
            $ret = preg_match("/<$l>(.*?)<\/$l>/", $value);
            $result[$l] = $ret[1];
        }
        return $result;
    }

    public function reverseTransform($array)
    {
        if (!is_array($array)) {
            throw new UnexpectedTypeException($array, 'array');
        }

        $result = "";
        $emptyKeys = array();

        foreach ($this->langs as $l) {
            $val = $array[$l];
            $result .= "<$l>$val</$l>";
        }

        return $result;
    }

Thanks in advance for any suggestion

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T13:38:40+00:00Added an answer on June 16, 2026 at 1:38 pm

    Your transformer is called multiple times. So, I think you should use early return by checking if the passed $value is null.

    Add at the top of your transform() method logic,

    if (!$value) {
        return null;
    }
    

    You should also do the same within your reverseTransform() method to avoid taking into account empty and null values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm implementing a custom form type that provides an autocomplete field to select a
I've created a custom form element that allows me to place text in an
I've a custom form (created with form API) that need send an uploaded file
I'd like to create a custom input form that collects employees hours. This data
I have used custom form field tutorial http://symfony.com/doc/current/cookbook/form/create_custom_field_type.html and it created a gender selection
I've created my own cck boolean field 'Show author' to a custom node type.
I am using CCK on a custom node type. I have created a field
I have a form that uses ajax to submit data to a mysql database,
I've created a custom field type, I think there are no mistakes, becouse it's
I'm actually developping a custom form field type which extends a file form field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.