Korzystam z FOS User Bundle dostepnym w Symfony 2 i chcialbym, aby podczas rejestracji uzytkownik mogl wybrac rodzaj formularza z okreslonymi polami do wypelnienia.
Bylyby to 2 formularze: dla osoby prywatnej i firmy (wybor odbywalby sie poprzez klikniecie na stosowny link: Osoba prywatna | Firma)
Na chwile obecna zrobilem formularz dla osoby prywatnej. Wszystko dziala jak nalezy, ale nie mam pomyslu, jak zrobic form dla firmy i podpiac go pod odpowiedni link o etykiecie Firma.
Ponizej fragmenty kodu.
Ml\UserBundle\Form\RegistrationType.php
namespace Ml\UserBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Collection; class RegistrationType extends AbstractType { { // custom field $builder } public function getParent() { return 'fos_user_registration'; } public function getName() { return 'ml_user_registration'; } }
User.php (Entity)
namespace Ml\UserBundle\Entity; use Gedmo\Mapping\Annotation as Gedmo; use FOS\UserBundle\Model\User as BaseUser; use Doctrine\ORM\Mapping as ORM; /** * @ORM\Entity * @ORM\Table(name="fos_user") * @ORM\Entity(repositoryClass="Ml\UserBundle\Entity\UserRepository") */ class User extends BaseUser { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @var string * * @ORM\Column(name="name", type="string", length=50, nullable=true) */ private $name; /** * @var string * * @ORM\Column(name="surname", type="string", length=50, nullable=true) */ private $surname; /** * @var string * * @ORM\Column(name="address", type="string", length=100, nullable=true) */ private $address; /** * @var string * * @ORM\Column(name="post_code", type="string", length=20, nullable=true) */ private $postCode; /** * @var string * * @ORM\Column(name="city", type="string", length=50, nullable=true) */ private $city; /** * @var string * * @ORM\Column(name="phone", type="string", length=20, nullable=true) */ private $phone; /** * @var integer * * @ORM\Column(name="mobile", type="integer", length=20, nullable=true) */ private $mobile; /** * @var string * * @ORM\Column(name="company_name", type="string", length=255, nullable=true) */ private $companyName; /** * @var string * * @ORM\Column(name="nip", type="string", length=13, nullable=true) */ private $nip; /** * @var integer * * @ORM\Column(name="regon", type="integer", length=14, nullable=true) */ private $regon; /** * @var boolean * * @ORM\Column(name="type_account", type="boolean", options={"default":0}, nullable=true) */ private $typeAccount; /** * @var \DateTime $created * * @ORM\Column(name="created", type="datetime") * @Gedmo\Timestampable(on="create") */ protected $created;
app\Resources\FOSUserBundle\views\Registration\register.html.twig
{% extends "::layout.html.twig" %} {% block title %}Rejestracja konta | {{ parent() }}{% endblock %} {% block contentpage %} <h1 class="section-title">Rejestracja konta</h1> <p>Typ konta: Osoba prywatna | Firma</p> {% block fos_user_content %} {% include "FOSUserBundle:Registration:register_content.html.twig" %} {% endblock fos_user_content %} {% endblock contentpage %}
config.yml
fos_user: db_driver: orm firewall_name: main user_class: Ml\UserBundle\Entity\User registration: form: type: ml_user_registration from_email: address: noreply@project.com sender_name: Project service: mailer: fos_user.mailer.twig_swift
services.yml
services: ml.form.registration: class: Ml\UserBundle\Form\RegistrationType tags: - { name: form.type, alias: ml_user_registration }
Czy ktos pomoze? Bede wdzieczny za wszelkie wskazowki.
Dokonalem kilku poprawek.
services.xml
<?xml version="1.0" ?> <container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/ser...pan>> <services> <service id="ml.form.registration" class="Ml\UserBundle\Form\RegistrationType"> <tag name="form.type" alias="ml_user_registration" /> <argument type="service" id="service_container" /> </service> </services> </container>
services.yml
services: ml.form.registration: class: Ml\UserBundle\Form\RegistrationType tags: - { name: form.type, alias: ml_user_registration } arguments: ["@service_container"]
RegistrationType.php
namespace Ml\UserBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilderInterface; use Symfony\Component\OptionsResolver\OptionsResolverInterface; use Symfony\Component\Validator\Constraints\Email; use Symfony\Component\Validator\Constraints\Length; use Symfony\Component\Validator\Constraints\NotBlank; use Symfony\Component\Validator\Constraints\Collection; use Symfony\Component\Validator\Constraints\IsTrue; use Symfony\Component\DependencyInjection\ContainerInterface as Container; class RegistrationType extends AbstractType { private $container; private $type; public function __construct(Container $container) { $this->container = $container; $this->type = $this->container->get('request')->get('type'); } { if ($this->type == 'company') { $builder 'label' => 'Akceptuję regulamin', 'required' => true, 'mapped' => false, 'constraints' => new IsTrue(), )) } else { $builder 'pattern' => '.{3,}' // minlength ) )) 'pattern' => '.{3,}' // minlength ) )) 'pattern' => '.{3,}' // minlength ) )) 'pattern' => '.{5,}' // minlength ) )) 'pattern' => '.{3,}' // minlength ) )) 'pattern' => '.{3,}' // minlength ) )) 'pattern' => '.{3,}' // minlength ), 'required' => false )) 'label' => 'Akceptuję regulamin', 'required' => true, 'mapped' => false, 'constraints' => new IsTrue(), )) ; } } ... }
Niestety, formularz nie dziala tak, jak nalezy. Z rejestracja osoby nie ma problemu, ale jest z rejestracja firmy. Dostaje zwrotke o nastepujacej tresci: " Ten formularz nie powinien zawierać dodatkowych pól."
Jak to naprawic? Prosze o pomoc.