I have a translations catalog on app/resources/translations/
menus.en.xliff, messages.en.xliff, user.en.xliff, ...
My form structure:
public function buildform(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array('label' => 'user.register', 'translation_domain' => 'user' ))
}
public function configureOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User'
));
}
This method is valid but not the best.
I would like don't repeat 'translation_domain' => 'forms' on all the form fields.
I'm triying to pass the translation domain on configureOptions method but fail.
public function buildform(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array('label' => 'user.register'))
public function configureOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\User',
'translation_domain' => 'user'
));
}
But, when i write all the translations on messages.en.xliff all works.
How can i fix it?
It should be in setDefaultOptions not configureOptions:
http://api.symfony.com/2.6/Symfony/Component/Form/AbstractType.html
Related
I`m use Symfony 2.3 and propel.
I have type form ClientType, and the type is declared in it сlient_profile
class ClientType extends BaseClientType
{
protected $options = array(
'label' => false,
'data_class' => 'Artsofte\ClientsBundle\Model\Client',
'name' => 'client_form'
);
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('сlient_profile', 'client_profile', array(
'label' => FALSE
))
->add(...)
;
}
}
Type form сlient_profile create on class СlientProfileType
class СlientProfileType extends BaseСlientProfileType{
protected $options = array(
'data_class' => 'Artsofte\DeveloperClientsBundle\Model\СlientProfile',
'name' => 'Сlient_profile'
);
public function buildForm(FormBuilderInterface $builder, array $options){
var_dump($builder->getData());
$builder
->add('name', 'text', array(
'label' => 'Name client',
'attr' => array(
'placeholder' => 'Name client'. $builder->getData()->getName()
))
->add(..............);
}
var_dump($builder->getData()); - return is null.
How do I fill in $builder->getData() in the form type client_profile, or how else can I transfer data from the client_profile model to the form type client_profile?
in class ClientType add option clientProfile. This option add - data model getData()
class ClientType extends BaseClientType
{
protected $options = array(
'label' => false,
'data_class' => 'Artsofte\ClientsBundle\Model\Client',
'name' => 'client_form'
);
public function buildForm(FormBuilderInterface $builder, array $options){
$builder
->add('сlient_profile', 'client_profile', array(
'label' => FALSE,
'contractorProfile' => $builder->getData(),
))
->add(...)
;
}
}
but not decided why in class СlientProfileType $builder->getData() is null.
class FormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c')
->add('d');
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' =>'entity'
));
}
}
html form field:
<form>
<input name="a" value="a">
<input name="b" value="b">
</form
When I submit the form, the c,d is set to null.
When the form does not have this field, I do not want to update c and d :)
insted of this
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c')
->add('d');
}
use this
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c', TextType::class , array('mapped' => false))
->add('d', TextType::class , array('mapped' => false));
}
Update your buildForm function like this. In The Symfony FormType, by default all fields are required.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('a')
->add('b')
->add('c',TextType::class, ['required' => false,'empty_data' => null])
->add('d',TextType::class, ['required' => false,'empty_data' => null] );
}
If you want to persist the entity in database c and d must be nullable as well
I have a problem with a simple form. I have a class called JurisdictionUser() and my class form is JurisdictionUserNewType().
If I put the next code, its work fine:
$jurisdiction_user = new JurisdictionUser();
$form = $this->createFormBuilder($jurisdiction_user)
->add('name', 'text')
->add('email', 'text', array('required' => false))
->add('save', 'submit')
->getForm();
but, if I put the next code:
$jurisdiction_user = new JurisdictionUser();
$form = $this->createFormBuilder(new JurisdictionUserNewType(), $jurisdiction_user);
It give me the next error:
ContextErrorException: Catchable Fatal Error: Argument 2 passed to Symfony\Bundle\FrameworkBundle\Controller\Controller::createFormBuilder() must be of the type array, object given, called in /home/sierra/dev_env/iyc-open010/src/Radmas/BackofficeAdminBundle/Controller/UserAdminController.php on line 119 and defined in /home/sierra/dev_env/iyc-open010/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php line 175
I need help xD. My class JurisdictionUserNewType().
<?php
/**
* Created by PhpStorm.
* User: diego
* Date: 15/01/14
* Time: 16:21
*/
namespace Radmas\BackofficeAdminBundle\Form\Type;
use Radmas\BackofficeAdminBundle\Form\Transformer\CapitalToSmallLetterTransformer;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class JurisdictionUserNewType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$transformer = new CapitalToSmallLetterTransformer();
$builder
->add('name', 'text');
$builder->add(
$builder->create('email', 'text', array('required' => false))
->addModelTransformer($transformer)
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Radmas\Open010Bundle\Document\JurisdictionUser'
));
}
public function getName()
{
return 'jurisdictionUserNew';
}
}
You should use
$this->createForm(new JurisdictionUserNewType(), $jurisdiction_user)
in controller, instead of createFormBuilder.
I cannot get $this->generateUrl() work, but it's work from my controller, or should I define 'setAction' using another way ?
class UserLoginType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('POST')
->setAction($this->generateUrl('fos_user_security_check'))
->add('username', 'text')
->add('password', 'password')
->add('save', 'submit');
}
public function getName()
{
return 'user_login';
}
}
If you are building your form in a separate class and not in your controller, you should pass the action to the form type like this:
// In your controller
$form = $this->createForm(new FormType(), $object, array(
'action' => $this->generateUrl('target_route'),
'method' => 'POST',
));
The AbstractType does not contain any method generateUrl(), that's why you can't set the action in the type directly. You can find details here: http://symfony.com/doc/current/book/forms.html#changing-the-action-and-method-of-a-form
Yes, in the Form class, function buildForm , put this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->setMethod('POST')
->setAction( $options["data"]["action"])
->add('name', TextType::class, array('label' => 'Nombre'))
;
}
I am doing some forms under Symfony 2.1.9
For useful reasons, I have done a FUser class, who extends from my user entity.
I have done an UserType class, for being able to reuse my form.
My UserType class looks like this :
<?php
namespace CD\BoutiqueBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nom', 'text')
->add('prenom', 'text')
->add('mail', 'text')
->add('fb_id','hidden',array('data'=>'null'))
->add('pass', 'repeated',
array('type' => 'password', 'invalid_message' => 'The password fields must match.',
'first_options' => array('label' => 'Mot de passe'),
'second_options' => array('label' => 'Confirmer le mot de passe')))
->add('tel', 'text', array('required' => false))
->add('adresse', 'text')
->add('code_postal', 'text')
->add('ville', 'text')
->add('tel', 'text')
->add('date_naissance', 'birthday', array('widget' => 'choice', 'years' => range(1933,2013,1), 'months' => range(1,12,1), 'days' => range(1,31,1)));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CD\ConfigBundle\Entity\User'
));
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'CD\ConfigBundle\Entity\User',
);
}
public function getName()
{
return 'usertype';
}
}
What I want to do is quite simple, but I didn't find my answer neither on google, neither on symfony doc, neither on this website.
I want to send options from my controller to this UserType.
For instance, in some cases, I don't want to add "tel" and "adresse" inputs.
Or sometimes, I want this input to be required; in others, I want the same input not required.
How can I do this?
If I understand correctly you need to modify your form dynamically.
Take a look here: How to Dynamically Modify Forms Using Form Events
UPDATE
To pass options to your form you can modify the form this way:
<?php
namespace CD\BoutiqueBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$myCustomVar = $options['foo'];
$builder
->add('nom', 'text')
->add('prenom', 'text')
->add('mail', 'text')
->add('fb_id','hidden',array('data'=>'null'))
->add('pass', 'repeated',
array('type' => 'password', 'invalid_message' => 'The password fields must match.',
'first_options' => array('label' => 'Mot de passe'),
'second_options' => array('label' => 'Confirmer le mot de passe')))
->add('tel', 'text', array('required' => false))
->add('adresse', 'text')
->add('code_postal', 'text')
->add('ville', 'text')
->add('tel', 'text')
->add('date_naissance', 'birthday', array('widget' => 'choice', 'years' => range(1933,2013,1), 'months' => range(1,12,1), 'days' => range(1,31,1)));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'CD\ConfigBundle\Entity\User',
'foo' => 'bar'
));
}
public function getName()
{
return 'usertype';
}
}
As you can see in setDefaultOptions method you must declare the custom option.
Then in your controller you can instantiate your form this way:
$myForm = $this->createForm(new UserType(), $entity, array('foo' => 'baz'));
Hope this helps