Worlor
Worlor

Reputation: 73

Symfony 2 : How to concatenate multiple fields into one in a form

I have searched through the internet to find a way to concatenate multiple fields into one for user input, and then break down before updating database.

I work on a project where a doctrine entity have these fields :

/**
 * @var integer
 */
private $disponibilite;

/**
 * @var integer
 */
private $integrite;

/**
 * @var integer
 */
private $confidentialite;

/**
 * @var integer
 */
private $preuve;

Each of these integers should be between 1 and 5. Actually the form looks like this : 4 fields

I want something like this : 1 field

I noticed that Symfony offered a tool that would solve my problem: data converters. But I do not see how they can afford to insert 4 values in the database from a single field.

Do you know another way to customize the form?

Upvotes: 2

Views: 2537

Answers (1)

john Smith
john Smith

Reputation: 17906

you could add a new property to your class e.g

private $singleField

/* a getter that builds up the unified value */    
public function getSingleField(){
  return $this->disponibilite.$this->integrite.$this->confidentialite.$this->preuve
} 


/* a setter that sets the properties by unified value */
public function setSingleField($value){

 $arr=str_split((string)$value);
 if(is_array($arr) && count($arr) == 3){

    $this->disponibilite = $arr[0];
    $this->integrite = $arr[1];
    ...

 }else{
   return false
 }

}

and in your formbuilder you only add the property "singleField"

Upvotes: 3

Related Questions