I am using the FOSUserBundle, how do i add a checkbox that has to be selected for the user to create an account.
Does FOS already cater for this, and perhaps i just have to add a line in the config file, or maybe i have to change the controller some how.
I have added a link to a picture to better explain what i need to do
This is really important, as users must accept the terms and conditions before registration.
You can add simply à field with the option « mapped » at false for doesn't add it in your entity.
$builder
->add('cgu', 'checkbox', array(
'label' => 'Je reconnais…',
'required' => true,
'mapped' => false
))
;
In your form you will have to use class:
Symfony\Component\Validator\Constraints\True;
like this :
<?php
namespace Project\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Validator\Constraints\True;
use FOS\UserBundle\Form\Type\RegistrationFormType;
class MyUserRegistrationType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user', new RegistrationFormType())
->add('cgu', 'checkbox', array(
'label' => 'Your label',
'required' => true
))
->add('t_and_c', 'checkbox', array(
'property_path' => false,
'constraints' => new True(array('message' => 'Your Confirmation Message','groups' => 'Registration')),
))
;
}
/**
* #return string
*/
public function getName()
{
return 'project_userbundle_user';
}
}
I see two solutions, the first is the legacy create a legacy from that of FOSUserBundle form. But you can have issues at validating your User entity.
I suggest you create a form MyUserRegistrationType.php for example and add FOSUserBundle form and checkbox like this :
<?php
namespace Project\UserBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use FOS\UserBundle\Form\Type\RegistrationFormType;
class MyUserRegistrationType extends AbstractType
{
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user', new RegistrationFormType())
->add('cgu', 'checkbox', array(
'label' => 'Your label',
'required' => true
))
;
}
/**
* #return string
*/
public function getName()
{
return 'project_userbundle_user';
}
}
Ajeet's answer is correct for validating the form, but the user can submit the form and then an error will show. There is another option if you want to prevent the user from being able to submit the form at all and you need to use javascript or jQuery. I use this and I actually hide the submit button until the user has filled out all items needed. Below is a simple script using jQuery:
<script>
$('#chkSelect').click(function(){ validatechecked(); });
validatechecked(){
var isChecked = $('#chkSelect').prop('checked');
if(var){
#submit.show();
}
else{
alert("Please agree to the terms to proceed.")
#submit.hide();
}
};
</script>
You will need to adjust the code with the id of your submit button and checkbox. You can add other options to it for more validation.
I use this along with annotations in my entity to give the user quicker feedback and to prevent clearly wrong/incomplete submissions. It is something extra you need to add to your twig file and you probably should put the jQuery code in a separate js file.
Related
I have a problem with the dependency injection, and saving it to the database, I have a following formType inside another form which i use in two different controller routes which handle the same entity: Creating a new entity and editing it. When i'm trying to create a new entity and set $user field for it, it will save user field as empty string.
class SomeClass extends AbstractType
{
private $tokenStorage;
private $user;
public function __construct(TokenStorageInterface $tokenStorage)
{
$this->tokenStorage = $tokenStorage;
}
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$user = $this->tokenStorage->getToken()->getUser();
$builder
->add('price', null, array(
'label' => 'price',
'required' => true,
'translation_domain' => 'some_translation',
'attr' => array(
'placeholder' => 'price'
)
))
->add('user', HiddenType::class, array(
'data' => $user
));
// dump($user);
// exit(0);
}
I have tried it out and dumped it and debugged and what i've found out is that when i dump $user i get the proper data i need firstName, lastName etc. , but when i submit the data it does not save it to the database instead i get empty string. Then i dumped out the whole form after submit in the controller and found out that the user field is null.
However it does work when i go to edit route and edit the existing entity, but if i create new entity on the edit form it does work on the one that exists, but not the newly made entity.
I have also checked the controller and the Entity class itself and those are not the problem.
I have a form responsible of creating and updating users. A user can (or not) have an address (OneToOne unidirectional relation from user).
When I create a user, no problem.
When I update a user, usually no problem.
Problems come up when i update a user which already has an address and try to unset all the address fields. There is then a validation error.
The wanted behavior would be to have the user->address relation set to null (and delete the previously set address on the DB).
There is a cascade_validation, the addess field in form (nested form) is set to not be required and the user entity allow the address to be null.
UPDATE
Relevant entities and forms :
User entity (Getters & Setters are classical, Symfony generated):
class User
{
[...]
/**
* #var \Address
*
* #ORM\OneToOne(targetEntity="Address", cascade="persist")
* #ORM\JoinColumn(
* name="address_id", referencedColumnName="id"
* )
*/
private $address;
[...]
}
The address entity is classical, no bidirectionnal relation to user.
User form
class UserType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
[...]
->add('address', new AddressType(), array('required' => false))
[...]
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Xentia\FuneoBundle\Entity\User',
'cascade_validation' => true
));
}
public function getName()
{
return 'user';
}
}
The address nested form is classical
As you can see, the is a quite classical and straightforward code. The only particular case is that address is optional. Leading to an validation error only in the case that the address was previously set (and, thus, exist in the DB and as a not null relation with the user) and the user want to unset it (all address fields are left empty).
It seems that if the related address has not an actual instance it can still be optional. But, if an instance of the address exist and is linked with the user, it can not be optional anymore.
UPDATE 2
namespace Xentia\FuneoBundle\Form\Type;
use Doctrine\Common\Util\Debug;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class AddressType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add("suggestLocality", null, array(
'mapped' => false,
'label' => 'Locality'
))
->add("suggestStreet", null, array(
'mapped' => false,
'label' => 'Street'
))
->add('street')
->add('locality')
->add('postalCode')
->add('country', null, array(
'label' => false,
))
->add('latitude', 'hidden')
->add('longitude', 'hidden');
$builder->addEventListener(FormEvents::PRE_SUBMIT,
function(FormEvent $event) {
$address = $event->getData();
if (!empty($address)) {
$addressLocality = $address['locality'];
if (empty($addressLocality)) {
$event->setData(null);
}
}
}
);
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Xentia\FuneoBundle\Entity\Address',
'validation_groups' => array('Default'),
));
}
public function getName()
{
return 'address';
}
}
Try setting orphanRemoval on your relation
/** #OneToOne(targetEntity="...", orphanRemoval=true) */
$address
EDIT
I see now, you have placed the wrong listener. First of all it should be POST_SUBMIT, PRE_SUBMIT is to process request data and modify form. On POST SUBMIT you can modify the object.
I have a form with a collection field type.
I'd like to filter it as we can do for entity field types but I'm not finding the solution.
i found other similar questions but no satisfiable answer so far. Can we do something like :
$builder
->add('userIngredients', 'collection', array(
'type' => new UserImportedIngredientType($this->userIngredients),
'query_builder'=>$this->queryBuilder,
))
;
If not, can we use form listener event to exclude some elements based on the object property ? How ?
This collection represents userIngredients that I want the user to be able to change if they have their property isImported set to true, hence the search for the query_builder solution.
Well, I figured I could do something as simple as build a regular form not attached to a parent entity.
In case this might help someone :
class UserImportedIngredientType extends AbstractType
{
protected $userIngredients;
protected $userImportedIngredients;
/**
* #param FormBuilderInterface $builder
* #param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
foreach ($this->userImportedIngredients as $userImportedIngredient)
{
/**
* #var $userImportedIngredient UserIngredient
*/
$builder
->add($userImportedIngredient->getId(), 'genemu_jqueryselect2_entity', array(
'query_builder'=>$this->userIngredients,
'class' => 'AppBundle:FoodAnalytics\UserIngredient',
'multiple' => false,
'label' => $userImportedIngredient->getName(),
'required'=>false,
'mapped' => false,
'data' => $userImportedIngredient
))
;
}
}
/**
* #return string
*/
public function getName()
{
return 'appbundle_foodanalytics_user_imported_ingredient';
}
public function __construct($userIngredients, $userImportedIngredients)
{
$this->userIngredients=$userIngredients;
$this->userImportedIngredients=$userImportedIngredients;
}
}
I solved it like this in my case with additionally Sonata on top of Symfony. What I did was feeding the 'data' parameter the specifically doctrine queried array of entities result. (In Repository: return $queryBuilder->getQuery()->getResult();)
/** #var MyEntityRepository $myEntityRepository */
$myEntityRepository = $this->getEntityManager()->getRepository(MyEntity::class);
/** #var MyEntity[] $myEntities */
$myEntities = $myEntityRepository->findBySomeCriteriaFilter(
$parameter, Constants::specificConstant
);
$formMapper->add(
// html name=
'myEntityProperty',
\Symfony\Component\Form\Extension\Core\Type\CollectionType::class,
[
// specific form for MyEntity
'entry_type' => new Form\MyEntity\MyEntityType(),
'allow_add' => true,
'label' => false,
'entry_options' => [
'label' => false
],
// the filtered array of entities from doctrine repository query above
'data' => $myEntities,
]
);
Use this instead of sonata_type_model_list, I guess.
Also if you want to filter sonata_type_model, use EntityType instead and use the 'query_builder' option with a Closure and returning the queryBuilder instead of the array of entities. This is all very inconsistent, best don't use symfony and sonata at all.
As far as I know Collection does not have the query_builder option
So you cannot go this way.
Is hard to decipher what you are trying to do with 4 lines of formType.
Your code looks alright, except the unsuppported query_builder, and you are already passing the userIngredients to the constructor.
My though is that if you need to filter this, then you shouldn't do it in the collection Type, but in other place.
EDITED:
On a second though, you are trying to filter the collection in the wrong place. Is not on the base collection , but in :
class UserImportedIngredientType extends AbstractType {
function __construct( $userIngredients ) {
// Do your stuff
}
function buildForm( FormBuilderInterface $builder, array $options) {
// Add here your entity with the query_filter option :)
}
}
Check the other entries in SO over Collections, there are many of them
I have entity Message with ManyToOne relation with entity User:
class Message
{
...
/**
* #var User $sender
*
* #ORM\ManyToOne(targetEntity="Acme\UserBundle\Entity\User")
* #ORM\JoinColumn(name="sender_id", referencedColumnName="id")
*
**/
private $sender;
...
}
If $sender doesn't have email value i need to create new field for my form, so i create form for Message entity in Contoller:
$user = $this->getUser();
$message = new Message();
$message->setSender($user);
$formBuilder = $this->createFormBuilder($message, array(
'cascade_validation' => true
));
$formBuilder->add('body', 'textarea');
if (!$user->getEmail()) {
$formBuilder->add('email', 'email', array(
'property_path' => 'sender.email'
));
}
And i have some validation rules in validation.yml for entity User. Can i automatically validate this field by my validation rules for User entity in another entity's form? I don't know how to do it.
I found workaround right now: create new MissingEmailType:
class MissingEmailType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('email')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\UserBundle\Entity\User',
'validation_groups' => array(
'MissingEmail'
),
));
}
public function getName()
{
return 'missing_email';
}
}
But it looks complicated. Is there any better solutions?
You could redirect the page to the user profile page instead of loading the message form and state that the user needs to add an email prior to adding the message. If you redirect quickly or create a popup, the user might not be turned off as long as they can return to the original page after adding their email. Then validtion is simple since you only need to validate the user entity.
I am having two entity files one as user.php and another as usertype.php. now i want to display a login form with 3 fields viz username, password and usertype. the usertype will be a selection that will fetch data from usertype table. here is the code that i wrote inside user.php to create a manytoone field for usertype_id
/**
* #ORM\ManyToOne(targetEntity="Usertype")
*/
protected $usertype;
Below is my Form generation Code
class LoginForm extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('login', 'text', array('label' => 'Username',));
$builder->add('password');
}
}
Now I need to add one more field to my form builder that will be a selection of usertype table.
...
use Acme\YourBundle\Entity\Usertype;
class LoginForm extends AbstractType {
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add('usertype', 'entity',
array(
'class' => 'AcmeYourBundle:Usertype'
'label' => 'User Type',
)
);
}
}
You can read more informations about the entity field type wich will give you the options available for this type of field.
Don't forget to add a __toString() method to your model to tell the form builder what to display.
namespace Acme\YourBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
class Usertype
{
public function __toString()
{
return $this->getName();
}
}
There are other ways to do it, but you can try this:
$builder->add('usertype', 'entity',
array(
'class' => 'YourBundle:UserType
'required' => true, // Choose if it's required or not
'empty_value' => 'User type', // Remove this line if you don't want empty values
'label' => 'Type', // You can put a label here or remove this line
)
);
I hope it helped!
http://symfony.com/doc/2.0/reference/forms/types/entity.html
property¶
type: string
This is the property that should be used for displaying the entities as text in the HTML element. If left blank, the entity object will be cast into a string and so must have a __toString() method.