ok, this is starting to drive me up the wall that I can not do this, so heres hoping someone can help!
I am using Symfony 2.3 and I have built up my user form in an 'UserType' class. I have this in two places, one is for when the app adds a new user, which I want the field to stay as it is. However I build a new 'UserType' to view all users, so the app as a management area.
So I want to be able to remove 'required' from my password input on my users form, when a form is just edited, e.g. the email is updated.
From what I have read, this is not doable 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 twig, best option! 2nd, how do I do this to just one of my 'UserTypes' and not the other?
So my code 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 have read, Force a field to not be required, but that adds it to the builder, which would change both add new user, and edit all users, which i don't want!
Any help, please?
*If I have forgotten any code or other sections you need to see, please let me know!