2012-06-08 9 views
28

Ich arbeite mit Symfony2 und möchte ein Registrierungsformular erstellen. Ich möchte FOSUserBundle nicht verwenden.Symfony 2: Hinzufügen eines benutzerdefinierten Formularelements, nicht in einer Entität

So erstelle ich ein Entity-Konto (mit Feldern: Benutzername, Passwort, E-Mail ...) und ich schaff' das Formular:

$account = new Account(); 

$form = $this->createFormBuilder($account) 
    ->add('username',   'text', array('label' => 'Nom de compte :')) 
    ->add('password', 'password', array('label' => 'Mot de passe :')) 
    ->add('email',   'email', array('label' => 'Adresse email :')) 
    ->getForm(); 

Nun mag ich ein Bestätigungsfeld für das Kennwort hinzuzufügen. Aber, wenn ich versuche, ein Feld mit add() Methode, zum Beispiel "password_confirmation" hinzufügen Ich habe dies:

Weder Eigenschaft "password_confirmation" noch Methode "getPasswordConfirmation()" noch Methode „isPasswordConfirmation() "existiert in der Klasse" App \ FrontBundle \ Entity \ Account "

Wie kann ich ein benutzerdefiniertes Feld hinzufügen? Und danach, wie kann man es anwenden?

Vielen Dank. BR.

Antwort

49

In einer normalen Situation müssen Sie mit der Option property_path explizit angeben, dass * password_confirmation * nicht Teil der Entität ist.

->add('password_confirmation', 'password', array('property_path' => false)) 

Und dann, um es mit einem CallBackValidator zu validieren.

Aber, in diesem speziellen Fall, wo Sie ein Feld wiederholen möchten, kann das Widget repeated das für Sie tun.

->add('password_confirmation', 'repeated', array(
    // See the docs :) 
)); 
+0

Vielen Dank;) –

+0

Eine perfekte Antwort. – rjmunro

+0

Vielen Dank. – iizno

61

Ein Update für Symfony 2.1:

property_path ist veraltet und stattdessen sollten Sie mapped verwenden. Die Syntax bleibt gleich:

->add('password_confirmation', 'password', array('mapped' => false)) 
+5

Es funktioniert auch für Symfony 3.0 – yeouuu

Verwandte Themen