2015-11-03 7 views
8

Ho un'entità che ha questi campi.Emetti un vincolo di entità nel modulo con il messaggio in Symfony 2

class User implements UserInterface, \Serializable 
{ 
    /** 
    * @var string 
    * 
    * @ORM\Column(name="first_name", type="string", length=64) 
    * @Assert\NotBlank(message="First name cannot be blank") 
    * @Assert\Length(max=64, maxMessage="First name cannot more than {{ limit }} characters long") 
    */ 
    private $firstName; 

    ..... 

} 

Ora vorrei esprimere questi vincoli in forma un po 'come questo.

<input type="text" required="required" data-required-msg="First name cannot be blank" name="firstname" data-max-length="64" data-max-length-msg="First name cannot be more than 64 characters long"> 

C'è qualche cosa che posso raggiungere questo obiettivo in Symfony 2 senza creare manualmente questi messaggi e gli attributi dei dati in forma di nuovo.

+0

Sembra che tu voglia fare qualcosa del genere: http://stackoverflow.com/questions/15573935/symfony2-get-all-validation-constraints-on-an-entity-yml-xml-annotations –

risposta

1

È possibile ottenere ciò utilizzando il seguente snippet di codice.

Qui sto iniettando un servizio di convalida per leggere i metadati (annotazione) di una classe. Nel nostro caso la classe User. Quindi, sulla funzione prepareConstraints, iterando su ogni vincolo di proprietà e aggiungendoli a un array il cui valore è key. Quindi su buildForm, la funzione aggiunge vincoli come valori di campo attr.

Sul constroller

$user = new User(); 
$form = $this->createForm(new UserType($this->get('validator'),$this->get('translator')), $user); 

SulUserType classe:

class UserType extends AbstractType 
{ 
    private $metaData; 
    private $constraintMessages; 
    private $translator; 

public function __construct(ValidatorInterface $validatorInterface,TranslatorInterface $translator) 
{ 
    $this->metaData = $validatorInterface->getMetadataFor('AppBundle\Entity\User'); 
    $this->translator = $translator; 
    $this->prepareConstraints(); 
} 

private function prepareConstraints() 
{ 

    foreach ($this->metaData->properties as $property) { 
     foreach ($property->constraints as $constraint) { 
      $class = get_class($constraint); 
      $constraintName = substr($class, strrpos($class, '\\') + 1, strlen($class)); 
$message = property_exists($class, 'message') ? $constraint->message : $constraint->maxMessage;; 
      $this->constraintMessages[$property->name]['data-'.$constraintName] = $this->translator->trans($message,array('{{limit}}'=>...)) 
     } 
    } 
} 

/** 
* {@inheritdoc} 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder 
     ->add(
      'name', 
      null, 
      array(
       'label' => 'label.name', 
       'attr' => $this->constraintMessages['name'], 
      ) 
     ) 
     ... 
} 

}

Risultato

<input type="text" id="app_user_name" name="app_user[name]" required="required" data-notblank="This value should not be blank." class="form-control" value=""> 
+0

Grazie molto per la risposta. Ma la tua soluzione risolve solo metà del problema. Non formatta i messaggi né li traduce. Se si guarda da vicino il messaggio ha il segnaposto {{limit}} che deve essere compilato. – nicholasnet

+0

Ora che è risolvibile, è possibile ottimizzare il servizio di traduzione come si desidera, –

Problemi correlati