概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:40:35 | 137 次阅读 | 评论: 0 | 来源: 网络整理

Some forms have extra fields whose values don’t need to be stored in the database. For example, you may want to create a registration form with some extra fields (like a “terms accepted” checkbox field) and embed the form that actually stores the account information.

The simple User Model

You have a simple User entity mapped to the database:

// src/Acme/AccountBundle/Entity/User.php
namespace AcmeAccountBundleEntity;

use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;

/**
 * @ORMEntity
 * @UniqueEntity(fields="email", message="Email already taken")
 */
class User
{
    /**
     * @ORMId
     * @ORMColumn(type="integer")
     * @ORMGeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank()
     * @AssertEmail()
     */
    protected $email;

    /**
     * @ORMColumn(type="string", length=255)
     * @AssertNotBlank()
     * @AssertLength(max = 4096)
     */
    protected $plainPassword;

    public function getId()
    {
        return $this->id;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }

    public function getPlainPassword()
    {
        return $this->plainPassword;
    }

    public function setPlainPassword($password)
    {
        $this->plainPassword = $password;
    }
}

This User entity contains three fields and two of them (email and plainPassword) should display on the form. The email property must be unique in the database, this is enforced by adding this validation at the top of the class.

注解

If you want to integrate this User within the security system, you need to implement the UserInterface of the Security component.

Create a Form for the Model

Next, create the form for the User model:

// src/Acme/AccountBundle/Form/Type/UserType.php
namespace AcmeAccountBundleFormType;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolver;

class UserType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('email', 'email');
        $builder->add('plainPassword', 'repeated', array(
           'first_name'  => 'password',
           'second_name' => 'confirm',
           'type'        => 'password',
        ));
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'AcmeAccountBundleEntityUser'
        ));
    }

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

There are just two fields: email and plainPassword (repeated to confirm the entered password). The data_class option tells the form the name of the underlying data class (i.e. your User entity).

小技巧

To explore more things about the Form component, read 表单.

Embedding the User Form into a Registration Form

The form that you’ll use for the registration page is not the same as the form used to simply modify the User (i.e. UserType). The registration form will contain further fields like “accept the terms”, whose value won’t be stored in the database.

Start by creating a simple class which represents the “registration”:

// src/Acme/AccountBundle/Form/Model/Registration.php
namespace AcmeAccountBundleFormModel;

use SymfonyComponentValidatorConstraints as Assert;

use AcmeAccountBundleEntityUser;

class Registration
{
    /**
     * @AssertType(type="AcmeAccountBundleEntityUser")
     * @AssertValid()
     */
    protected $user;

    /**
     * @AssertNotBlank()
     * @AssertTrue()
     */
    protected $termsAccepted;

    public function setUser(User $user)
    {
        $this->user = $user;
    }

    public function getUser()
    {
        return $this->user;
    }

    public function getTermsAccepted()
    {
        return $this->termsAccepted;
    }

    public function setTermsAccepted($termsAccepted)
    {
        $this->termsAccepted = (Boolean) $termsAccepted;
    }
}

Next, create the form for this Registration model:

// src/Acme/AccountBundle/Form/Type/RegistrationType.php
namespace AcmeAccountBundleFormType;

use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;

class RegistrationType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('user', new UserType());
        $builder->add(
            'terms',
            'checkbox',
            array('property_path' => 'termsAccepted')
        );
        $builder->add('Register', 'submit');
    }

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

You don’t need to use a special method for embedding the UserType form. A form is a field, too - so you can add this like any other field, with the expectation that the Registration.user property will hold an instance of the User class.

Handling the Form Submission

Next, you need a controller to handle the form. Start by creating a simple controller for displaying the registration form:

// src/Acme/AccountBundle/Controller/AccountController.php
namespace AcmeAccountBundleController;

use SymfonyBundleFrameworkBundleControllerController;

use AcmeAccountBundleFormTypeRegistrationType;
use AcmeAccountBundleFormModelRegistration;

class AccountController extends Controller
{
    public function registerAction()
    {
        $registration = new Registration();
        $form = $this->createForm(new RegistrationType(), $registration, array(
            'action' => $this->generateUrl('account_create'),
        ));

        return $this->render(
            'AcmeAccountBundle:Account:register.html.twig',
            array('form' => $form->createView())
        );
    }
}

And its template:

{# src/Acme/AccountBundle/Resources/views/Account/register.html.twig #}
{{ form(form) }}

Next, create the controller which handles the form submission. This performs the validation and saves the data into the database:

use SymfonyComponentHttpFoundationRequest;
// ...

public function createAction(Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $form = $this->createForm(new RegistrationType(), new Registration());

    $form->handleRequest($request);

    if ($form->isValid()) {
        $registration = $form->getData();

        $em->persist($registration->getUser());
        $em->flush();

        return $this->redirectToRoute(...);
    }

    return $this->render(
        'AcmeAccountBundle:Account:register.html.twig',
        array('form' => $form->createView())
    );
}

Add new Routes

Next, update your routes. If you’re placing your routes inside your bundle (as shown here), don’t forget to make sure that the routing file is being imported.

  • YAML
    # src/Acme/AccountBundle/Resources/config/routing.yml
    account_register:
        path:     /register
        defaults: { _controller: AcmeAccountBundle:Account:register }
    
    account_create:
        path:     /register/create
        defaults: { _controller: AcmeAccountBundle:Account:create }
    
  • XML
    <!-- src/Acme/AccountBundle/Resources/config/routing.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <routes xmlns="http://symfony.com/schema/routing"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
    
        <route id="account_register" path="/register">
            <default key="_controller">AcmeAccountBundle:Account:register</default>
        </route>
    
        <route id="account_create" path="/register/create">
            <default key="_controller">AcmeAccountBundle:Account:create</default>
        </route>
    </routes>
    
  • PHP
    // src/Acme/AccountBundle/Resources/config/routing.php
    use SymfonyComponentRoutingRouteCollection;
    use SymfonyComponentRoutingRoute;
    
    $collection = new RouteCollection();
    $collection->add('account_register', new Route('/register', array(
        '_controller' => 'AcmeAccountBundle:Account:register',
    )));
    $collection->add('account_create', new Route('/register/create', array(
        '_controller' => 'AcmeAccountBundle:Account:create',
    )));
    
    return $collection;
    

Update your Database Schema

Of course, since you’ve added a User entity during this tutorial, make sure that your database schema has been updated properly:

$ php app/console doctrine:schema:update --force

That’s it! Your form now validates, and allows you to save the User object to the database. The extra terms checkbox on the Registration model class is used during validation, but not actually used afterwards when saving the User to the database.

最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务