Witam.
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
{
public function buildForm
(FormBuilderInterface
$builder, array $options) {
// custom field
$builder
->add('name', 'text', array('label' => 'Imię')) ->add('surname', 'text', array('label' => 'Nazwisko')) ->add('address', 'text', array('label' => 'Adres')) ->add('city', 'text', array('label' => 'Miejscowość')) ->add('post_code', 'text', array('label' => 'Kod pocztowy')) ->add('phone', 'text', array('label' => 'Telefon stacjonarny')) ->add('mobile', 'text', array('label' => 'Telefon komórkowy')); }
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"
<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');
}
public function buildForm
(FormBuilderInterface
$builder, array $options) {
if ($this->type == 'company') {
$builder
->add('company_name', 'text', array('label' => 'Nazwa firmy')) ->add('address', 'text', array('label' => 'Adres')) ->add('post_code', 'text', array('label' => 'Kod pocztowy')) ->add('city', 'text', array('label' => 'Miasto')) ->add('nip', 'text', array('label' => 'NIP')) ->add('regon', 'text', array('label' => 'Regon', 'required' => false)) ->add('phone', 'text', array('label' => 'Telefon kontaktowy')) ->add('mobile', 'text', array('label' => 'Telefon komórkowy', 'required' => false)) ->add('typeAccount', 'hidden', array('data' => '1'))
->add('termsAccepted', 'checkbox', array( 'label' => 'Akceptuję regulamin',
'required' => true,
'mapped' => false,
'constraints' => new IsTrue(),
))
} else {
$builder
->add('name', 'text', array('label' => 'Imię', 'pattern' => '.{3,}' // minlength
)
))
->add('surname', 'text', array('label' => 'Nazwisko', 'pattern' => '.{3,}' // minlength
)
))
->add('address', 'text', array('label' => 'Adres', 'pattern' => '.{3,}' // minlength
)
))
->add('post_code', 'text', array('label' => 'Kod pocztowy', 'pattern' => '.{5,}' // minlength
)
))
->add('city', 'text', array('label' => 'Miejscowość', 'pattern' => '.{3,}' // minlength
)
))
->add('phone', 'text', array('label' => 'Telefon kontaktowy', 'pattern' => '.{3,}' // minlength
)
))
->add('mobile', 'text', array('label' => 'Telefon komórkowy', 'pattern' => '.{3,}' // minlength
), 'required' => false
))
->add('typeAccount', 'hidden', array('data' => '0'))
->add('termsAccepted', 'checkbox', array( '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.
Ten post edytował swiezak 20.01.2016, 22:41:00