I have an account (like company account) entity with a user association for the accountOwner.
Both account owner and users with ROLE_ADMIN can edit the account, but only users with ROLE_ADMIN can set the account owner.
Do I need 2 form types? or can I conditionally present the accountOwner field on the same form based on user role?
You can present accountOwner association conditionally.
When you want to modify form dynamically, you'll usually want to use form events.
However, since your form's fields do not depend on actual data bound to the form, but on security context, you can just inject authorization checker into your form type and check whether you want to add needed field:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('always_present_field');
$builder->add('another_always_present_field');
if ($this->authorizationChecker->isGranted('ROLE_ADMIN')) {
$builder->add('conditional_field_if_current_user_is_admin');
}
}
Related
I have a user class that has a onetomany self-referencing relationship (ancestor and decendents). And I have an invoice class, which references two users based on the ancestor-decendents relationship. Meaning that, a user creates an invoice, so the form's "from" property will have the logged in user, while the form's "to" property should be a selection from the logged in user's decendets.
This is the invoice's buildForm method
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('createdDate')
->add('from')
->add('to', 'entity', array('class' => 'Disty\SystemBundle\Entity\User'))
;
}
To keep it short, I want to somehow only show options of decendent users. Right now it shows all users registered.
You should take a look at the query_builder parameter that should do the trick.
If you look at the documentation you can find query_builder option of the entity form type. So you just need to modify query builder to retrieve only users which belong to the current logged in user.
I have two entities :
User that contains login data (with FOSUser),
and other informations about them (name, first name, date of birth, etc) in another entity called UserInfo.
In SonataAdmin, I want to manage my Users (done) but I need to add fields in the table that are in UserInfo (name, first name...).
Any idea ?
Thanks !
Depending on the relationship type, you should be able to just reference userinfo.firstName, eg:
public function configureShowFields(ShowMapper $show)
{
$show->add('userinfo.firstName')
->add('userinfo.dob');
}
Of course, if you have many userinfo's attached to the entity I don't think this will work.
There must be existing a relation in between user and userInfo. say OneToOne relation. Then from userInfo entity u will get the user data and will show on the Admin side.
i.e.
$subject = $this->getSubject();
$user = $this->subject->getUser();
Will give you user, if you want to further perform actions on that user.
Imagine three entities - Account, Address, Purchase. Account has a OneToMany relationship with Address. A Purchase is related to an Account, but not with an Address - it does however have a text field for the address (this is because addresses can change, I don't want the address directly related). On the users' account page they can add addresses. Whilst logged into the site, a Purchase id is stored in the session and used to retrieve the Purchase details when required.
What I want to do on the checkout page is display a list of all the addresses a user currently has in a <select>, allow them to pick one, and update the address in the current Purchase. $account->getAddresses() exists and will show the addresses relevant to the user.
I have read http://symfony.com/doc/current/reference/forms/types/collection.html and http://symfony.com/doc/current/cookbook/form/form_collections.html and can't see how to apply it to this situation, although an embedded form isn't really necessary - I don't want to change other details of the Purchase at that stage.
So my question is (at least one of): how do I pass the results of $account->getAddresses() to a form type? Or in the form type, should I use an entity field type, and if so, how do I get the custom query_builder to contain the current user in a form type? Or how else should I do this?
You need to pass the entity in to the Type's constructor and then use it to get the parameter.
Class YourType extends AbstractType
{
private $account;
public function __construct($account)
{
$this->account = $account;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$accountId = $account->getAccountId();
$builder->add('addressId',
'entity',
array('class' => 'YourBundle:Address',
'query_builder' => function(EntityRepository $er) use ($accountId) {
return $er->createQueryBuilder('a')
->where('a.accountId = ?1')
->setParameter(1, $accountId)));
}
}
I am creating an application where visitors can upload some stuff (it will be for invited people only). At the end, if they are not logged in, they are asked to log in or create a user. If they create a new user, I only want to ask them to fill in their name and email.
a password will be generated, and a mail will be sent to the user with the links to change their password if they want to (only to make the procedure as low level as possible).
I can't seem to remove the password fields from the registration form. Can someone help me out. I create a custom form type, service and registered it. I also put custom templates in the app/Resource folder etc. Although my custom Form type AND the templates are being used, the password still appears ...
class RegistrationFormType extends BaseType {
public function buildForm(FormBuilderInterface $builder, array $options) {
parent::buildForm($builder, $options);
// add your custom field
$builder->add('username');
$builder->add('email');
}
public function getName() {
return 'val_user_registration';
}
}
You don't need to extend your parent form ... and you're better off not doing it in this case.
just create a Username/Email form and create the new User entity yourself in a custom registration method then persisting it into database ( don't forget to set the usernameCanonical property on your newly created User ).
FOSUserBundle calls several password-related things during the registration process which you don't need and can't easily circumvent in this case.
You can pass the entity ( with newly created password ) to the update method of FOSUserBundle's UserManager service after you have completed the password/email step then.
I'm trying to build a form gathering information about new player in a game.
To start a game one need to provide nickname, email and a code.
Codes are stored in another table connected with player table with one-to-one relation
What I need to do during validation is to check if provided token exists and if so store Player id in Code record.
To do that I'm trying to build a form:
class PlayerType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('code','text')
->add('email', 'email')
->add('nick', 'text')
;
}
...
}
but in that way during validation (or probably during creating instance of Code)
Argument 1 passed to Player::setCode() must be an instance of Code, string given
which is obvious since string has been provided.
What to do to perform a lookup during form validation and pass not token string but token instance?
Look into data transformers. This will let you to create a form with a scalar type field that gets transformed into an entity when populating the model on a form submit.
Regarding the validation, you'll need to create a custom validation constraint that checks if the given code exists in the database.