-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
simple signup form with mongodb recipe #438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
weaverryan
merged 2 commits into
symfony:master
from
fspillner:simple_signup_form_with_mongodb2
Jun 28, 2011
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,246 @@ | ||
How to implement simple Sign up Form with MongoDB | ||
================================================= | ||
|
||
Some forms have extra fields which value are not needed to be stored into | ||
database. In this example we create sign up form with some extra fields and | ||
embed the form for storing the account information. We use MongoDB for | ||
storing data. | ||
|
||
This explains how to integrate two types of domain model into the form, too. | ||
|
||
.. tip:: | ||
|
||
If you are not familiar with Doctrine MongoDB Bundle, you should read | ||
this :doc:`file</cookbook/doctrine/mongodb>` recipe first to learn | ||
how to setup the MongoDB Bundle to be able to work with MongoDB. | ||
|
||
The simple Account model | ||
------------------------ | ||
|
||
So, in this tutorial we begin with the model for the ``Account``:: | ||
|
||
// src/Acme/AccountBundle/Document/Account.php | ||
namespace Acme\AccountBundle\Document; | ||
use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
use Symfony\Bundle\DoctrineMongoDBBundle\Validator\Constraints\Unique as MongoDBUnique; | ||
|
||
/** | ||
* @MongoDB\Document(collection="accounts") | ||
* @MongoDBUnique(path="email") | ||
*/ | ||
class Account | ||
{ | ||
/** | ||
* @MongoDB\Id | ||
*/ | ||
protected $id; | ||
|
||
/** | ||
* @MongoDB\Field(type="string") | ||
* @Assert\NotBlank() | ||
* @Assert\Email() | ||
*/ | ||
protected $email; | ||
|
||
/** | ||
* @MongoDB\Field(type="string") | ||
* @Assert\NotBlank() | ||
*/ | ||
protected $password; | ||
|
||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
|
||
public function getEmail() | ||
{ | ||
return $this->email; | ||
} | ||
|
||
public function setEmail($email) | ||
{ | ||
$this->email = $email; | ||
} | ||
|
||
public function getPassword() | ||
{ | ||
return $this->password; | ||
} | ||
|
||
// stupid simple encryption (please don't copy it!) | ||
public function setPassword($password) | ||
{ | ||
$this->password = sha1($password); | ||
} | ||
} | ||
|
||
This ``Account`` document contains three fields and two of them (email and | ||
password) should display on the form. The email property must be unique | ||
on the database, so we've added this validation at the top of the class. | ||
|
||
Create form for the model | ||
------------------------- | ||
|
||
Now, you need to create form for this ``Account`` model:: | ||
|
||
// src/Acme/AccountBundle/Form/Account.php | ||
namespace Acme\AccountBundle\Form; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; | ||
use Symfony\Component\Form\FormBuilder; | ||
|
||
class AccountType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilder $builder, array $options) | ||
{ | ||
$builder->add('email', 'email'); | ||
$builder->add('password', 'repeated', array( | ||
'first_name' => 'password', | ||
'second_name' => 'confirm', | ||
'type' => 'password' | ||
)); | ||
} | ||
|
||
public function getDefaultOptions(array $options) | ||
{ | ||
return array('data_class' => 'Acme\AccountBundle\Document\Account'); | ||
} | ||
} | ||
|
||
We just added two fields: email and password (repeated to confirm the entered | ||
password). The ``data_class`` option tells the form the name of data class and | ||
this is your ``Account`` document and the form is able to create the data model. | ||
|
||
.. tip:: | ||
|
||
To explore more things about form component, | ||
read this documentation :doc:`file</book/forms>`. | ||
|
||
Embedding Account form into Signup form | ||
--------------------------------------- | ||
|
||
The form for sign up is not the same as the form for Account. | ||
It contains further fields like accepting the terms which value is not needed | ||
to be stored into database. So, now we need to create own form for this purpose | ||
and embed the existing ``Account`` form. For validation and creation of Account | ||
data we need simple domain model for the sign up form:: | ||
|
||
// src/Acme/AccountBundle/Form/Signup.php | ||
namespace Acme\AccountBundle\Form; | ||
|
||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
use Acme\AccountBundle\Document\Account; | ||
|
||
class Signup | ||
{ | ||
/** | ||
* @Assert\Type(type="Acme\AccountBundle\Document\Account") | ||
*/ | ||
protected $account; | ||
|
||
/** | ||
* @Assert\NotBlank() | ||
* @Assert\True() | ||
*/ | ||
protected $termsAccepted; | ||
|
||
public function setAccount(Account $account) | ||
{ | ||
$this->account = $account; | ||
} | ||
|
||
public function getAccount() | ||
{ | ||
return $this->account; | ||
} | ||
|
||
public function getTermsAccepted() | ||
{ | ||
return $this->termsAccepted; | ||
} | ||
|
||
public function setTermsAccepted($termsAccepted) | ||
{ | ||
$this->termsAccepted = (boolean)$termsAccepted; | ||
} | ||
} | ||
|
||
And the form for this ``Signup`` model:: | ||
|
||
// src/Acme/AccountBundle/Form/SignupType.php | ||
namespace Acme\AccountBundle\Form; | ||
|
||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\Extension\Core\Type\RepeatedType; | ||
use Symfony\Component\Form\FormBuilder; | ||
|
||
class SignupType extends AbstractType | ||
{ | ||
public function buildForm(FormBuilder $builder, array $options) | ||
{ | ||
$builder->add('account', new AccountType()); | ||
$builder->add('terms', 'checkbox', array('property_path' => 'termsAccepted')); | ||
} | ||
} | ||
|
||
We added two fields into the form. You don't need to use special method | ||
for embedding form. A form is a field, too - so you can add this like the fields, | ||
with the expectation that you need to instance the class ``AccountType``. | ||
|
||
Handling the Form Submission | ||
---------------------------- | ||
|
||
Now we need controller to handle the form actions, first we create | ||
simple controller for displaying the sign up form:: | ||
|
||
namespace Acme\AccountBundle\Controller; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\Response; | ||
|
||
use Acme\AccountBundle\Form; | ||
|
||
class AccountController extends Controller | ||
{ | ||
public function signupAction() | ||
{ | ||
$form = $this->createForm(new Form\SignupType(), new Form\Signup()); | ||
|
||
return $this->render('AcmeAccountBundle:Account:signup.html.twig', array('form' => $form->createView())); | ||
} | ||
} | ||
|
||
and it's template:: | ||
|
||
<form action="{{ path('create')}}" method="post" {{ form_enctype(form) }}> | ||
{{ form_widget(form) }} | ||
|
||
<input type="submit" /> | ||
</form> | ||
|
||
At least we need the controller which handles the form submission. | ||
This performs the validation and saves the data into the database:: | ||
|
||
public function createAction() | ||
{ | ||
$dm = $this->get('doctrine.odm.mongodb.default_document_manager'); | ||
|
||
$form = $this->createForm(new Form\SignupType(), new Form\Signup()); | ||
|
||
$form->bindRequest($this->get('request')); | ||
|
||
if ($form->isValid()) { | ||
$signup = $form->getData(); | ||
|
||
$dm->persist($signup->getAccount()); | ||
$dm->flush(); | ||
|
||
return $this->redirect($this->generateUrl('welcome', array('id' => $signup->getAccount()->getId()))); | ||
} | ||
|
||
return $this->render('AcmeAccountBundle:Account:signup.html.twig', array('form' => $form->createView())); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, if a developer is creating a registration form, then they'll likely want these users to also be able to login with their new account. In other words, they'll likely need to integrate this
Account
entity with the security system. So, I'm wondering if we should renameAccount
toUser
(since that's what you most commonly see with security) and also make it implement theUserInterface
, just since this will be a common requirement. You could just mention that this entry isn't concerned with security, but how theUser
class is already setup to be easily integrated with the security system.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right. I added a note about this and changed the class name.