OK, this is starting to push me against the wall, and I can’t do it, so she hopes someone can help!
I use Symfony 2.3 and I created my custom form in the "UserType" class. I have this in two places, one for when the application adds a new user, and I want the field to remain as it is. However, I am creating a new “UserType” to view all users, so the application is a management area.
So, I want to remove the “required” from my password entry in my user form when the form is simply edited, for example. Email is being updated.
From what I read, this is not feasible in twig (which I don’t understand why!), But I have to do it in the builder?
1st, is there a way to do this in a branch, the best option! 2nd, how can I do this with only one of my "UserTypes" and not with the other?
So my code is as follows:
UserType.php
public function buildForm(FormBuilderInterface $builder, array $options) {
$RoleOpts = ['ROLE_USER' => 'User','ROLE_ADMIN' => 'Admin'];
$builder->setMethod('POST');
$builder->add('username', 'text', ['label' => 'Username', 'attr' => ['autocomplete' => 'off','class' => 'Admin_Username']]);
$builder->add('password', 'password',['label' => 'Password', 'attr' => ['autocomplete' => 'off','class' => 'Admin_Pswd']]);
$builder->add('firstname', 'text', ['label' => 'First Name','attr' => ['autocomplete' => 'off','class' => 'Admin_Name']]);
$builder->add('surname', 'text', ['label' => 'Surname', 'attr' => ['autocomplete' => 'off','class' => 'Admin_Surname']]);
$builder->add('email', 'email', ['label' => 'Email', 'attr' => ['autocomplete' => 'off','class' => 'Admin_Email']]);
$builder->add('userroles', 'choice', ['label' => 'Role','choices' => $RoleOpts, 'attr' => ['class' => 'Admin_Role']]);
$builder->add('save', 'submit', ['label' => 'Save User', 'attr' => ['class' => 'Admin_SaveUser']]);
}
ViewUsers Action
public function ViewUsersAction() {
$GetAllUsers = $this->getDoctrine()
->getRepository('Bundle:Users')
->findAll();
foreach($GetAllUsers as $UserKey => $UserValue) {
$UserID = $UserValue->getId();
$ViewAllUsers[$UserKey] = $this->createForm(new UserType(),
$GetAllUsers[$UserKey],
['action' => $this->generateUrl('Admin_EditUser', ['id' => $UserID])]
)->createView();
}
return $this->render('Bundle:Admin:Users/view.html.twig', ['ViewUsers' => $ViewAllUsers ]);
}
Twig file
{% for View in ViewUsers %}
{{ form_start(View, {'attr': {'class': 'EditUser'}}) }}
{{ form_errors(View) }}
{{ form_row(View.username, {'attr': {'disabled': 'disabled'}}) }}
{{ form_row(View.password, {'attr': {'required': false}}) }}
{{ form_row(View.firstname) }}
{{ form_row(View.surname) }}
{{ form_row(View.email) }}
{{ form_row(View.userroles) }}
{{ form_row(View.save, { 'label': 'Update User' }) }}
{{ form_end(View) }}
{% endfor %}
I read, A forced field is not required , but adds it to the constructor, which will change and add a new user, and edit all the users that I do not want!
Any help please?
* If I forgot any code or other sections that you need to see, please let me know!