Using Symfony 3 with FOSUserBundle, how do I make a custom field of my user registration read only)
In RegistrationFormType.php, buildForm method I tried:
->add('name', 'text', array('read_only' => true))
which gets InvalidArgumentException
->add('name', 'text', array('read_only' => true))
which gets UndefinedOptionsException
Try to do this :
->add('name', TextareaType::class, array(
'attr' => array(
'readonly' => true,
),
));
Related
There is edit form. One field should not be editable. I have tried to set options disabled=true, attr => ['readonly' => true], they make it uneditable, but when submitting form, it gets submitted, sets null to that field and then I get error when getting that field value because it cannot be null.
So I want to make that field not even exist as field but show its value in edit form. Is there a way to do it? Or also if you know how to get rid of error when submitting form with disabled field, that would work too.
public function configureFormFields(FormMapper $form)
{
if ($this->subject->getId() === null) {
$form
->add('name', 'text', ['required' => true])
->add('codeMod', 'text', ['required' => true])
->add('position', 'text', ['required' => false])
->add('projectMod', EntityType::class, ['class' => ProjectEntity::class])
->add('active', 'checkbox', ['required' => false])
->add('first', 'checkbox', ['required' => false])
->add('last', 'checkbox', ['required' => false])
->add('denialReasons', 'text', ['required' => false])
;
} else {
$form
->add('name', 'text', ['required' => true])
->add('position', 'text', ['required' => false])
// ->add('project', TextType::class, ['label' => 'form.label_project_mod', 'attr' => [/*'readonly' => true,*/ 'disabled' => true]])
->add('project', EntityType::class,
['label' => 'form.label_project_mod', 'class' => ProjectEntity::class, 'attr' => ['readonly' => true, 'disabled' => true],
// 'template' => 'ClaimClaimBundle:ClaimStatusAdmin:show_project.html.twig'
]
)
// ->add('projectMod', TextType::class, ['label' => 'form.label_project_mod', 'attr' => [/*'readonly' => true,*/ 'disabled' => true]])
->add('active', 'checkbox', ['required' => false])
->add('first', 'checkbox', ['required' => false])
->add('last', 'checkbox', ['required' => false])
->add('denialReasons', 'text', ['required' => false])
;
}
}
Currently I get error:
Type error: Return value of Qms\ClaimComponent\Status\ManagedModel\StatusManaged::getProject() must implement interface Qms\CoreComponent\Domain\Project\ManagedModel\ProjectManagedInterface, null returned
That is because field value is set to null if I have disabled field.
One way could be rewrite edit.html.twig, now sonatas default template is used. But I did not find quick way, if I override, the styling is off. For one field looks bit too much.
If you don't mind that field's value when submitting you can unmap it by setting
'mapped' => false
in its attributes.
Example:
->add('name', 'text', ['required' => true, 'mapped' => false])
I'm using symfony 2.8, I have created one registration form, I want to add bootstrap form-control class to both password and repeat password form fields.
$builder
->add('name', TextType::class,array(
'attr' => array(
'class' => 'form-control'
)
))
-> add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
'attr' => array('class' => 'form-control')
));
Incase of 'name' field its working BUT for password fields the class is not adding.
How can I add 'form-control' class for password fields.
Any help is much appreciated.
Thanks.
There are two ways of doing this. The first is to use options, which will pass the options down to each of the underlying fields:
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array('label' => 'Password'),
'second_options' => array('label' => 'Repeat Password'),
'options' => array('attr' => array('class' => 'form-control'))
));
You can also add the class in the first_options and second_options field, like so. This would be useful if you had options that were specific to each field or you wanted to override something from the main options.
->add('plainPassword', RepeatedType::class, array(
'type' => PasswordType::class,
'first_options' => array(
'label' => 'Password',
'attr' => array('class' => 'form-control')
),
'second_options' => array(
'label' => 'Password',
'attr' => array('class' => 'form-control-override')
),
'attr' => array('class' => 'form-control')
));
Also, as of Symfony 2.6 it has has built-in Bootstrap form theme support to where you shouldn't have to be adding these classes to all of your fields manually.
Some guys from the Symfony development team, suggest that you should use the boostrap's classes directly in html (here, if you want to see the suggestion). And the suggestion makes perfect sens to me, as Symfony is for backend development, and not frontend. So the ideal way of solving this is to create your two fields in the Type class, and when rendering the form, add something like:
{{ form_row(name, { attr: { 'class': 'form-control' } ) }}
{{ form_row(password, { attr: { 'class': 'form-control' } ) }}
{{ form_row(plainPassword, { attr: { 'class': 'form-control' } ) }}
I have a symfony form that looks like this and I need to pass the same options to every field.
I need to set label to true because I am using elaotranslationformbundle not globally, only for some forms.
I need to have the same class on all fields because I am using bootstrap.
I need to pass the translation_domain to each field based on an option on my form (which means I cannot do it in the setDefaultOptions method.
How can I avoid the following repetitive code ?
$defaultOptions = array(
'attr' => array('class' => 'form-control'),
'label' => true,
'translation_domain' => (
$options['professional']?
'prospect_form_librarian' :
'prospect_form_user'
)
);
$builder
->add('name', null, $defaultOptions + array('some_other_option' => 42))
->add('city', null, $defaultOptions)
->add('contactEmail', 'email', $defaultOptions)
->add('zipCode', null, $defaultOptions)
;
I would like to have the code look like this :
$defaultOptions = array(
'attr' => array('class' => 'form-control'),
'label' => true,
'translation_domain' => (
$options['professional']?
'prospect_form_librarian' :
'prospect_form_user'
)
);
$builder->someMethodCallThatWouldSolveMyProblem($defaultOptions)
$builder
->add('name', null, array('some_other_option' => 42))
->add('city')
->add('contactEmail', 'email', $defaultOptions)
->add('zipCode')
;
I'm having problems with Sonata Admin Bundle. What I would like to do is:
Add some text before some labels in my form. Like for example:
The resolution of your image must be ..x.. .
For example I have a form like this:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('locale', 'choice', array(
'choices' => array('nl' => 'NL', 'en' => 'EN'),
'required' => true,
))
->add('pageid.tag', 'text', array('label' => 'Tag'))
->add('description', 'text', array('label' => 'Beschrijving'))
->add('content', 'textarea', array('label' => 'Tekst', 'attr' => array('class' => 'ckeditor')))
->add('files', 'file', array('required' => false, 'multiple' => true))
;
}
Now I would like to add some text before my files input field.
What I've done now is:
Add this to my config.yml (overload the templates/form configuration option):
sonata_doctrine_orm_admin:
# default value is null, so doctrine uses the value defined in the configuration
entity_manager: ~
templates:
form:
- MurisBundle:PageAdmin:form_admin_fields.html.twig
But this will be used for every form, can't I set specific form templates for specific forms?
You can specify form template in your admin class overriding getFormTheme method.
Add this code to your admin class.
public function getFormTheme()
{
return array_merge(
parent::getFormTheme(),
array('MurisBundle:PageAdmin:form_admin_fields.html.twig')
);
}
getPictureUrlFull().'" alt="'.$campaign->getPicture().'" style="margin-top:10px;" />Use "help"
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('locale', 'choice', array(
'choices' => array('nl' => 'NL', 'en' => 'EN'),
'required' => true,
'help' => '<img src="'.$entity->getPictureUrlFull().'" alt="'.$entity->getPicture().'" />'
))
)
I have a form which I create using a form class. One field of this form is country_id, which gives me a country id in a text field.
However, I want create a select field instead, and not only with all ids, but rather with country names mapped by those ids.
I have a table in a database mapping country ids to names. However, the form uses only one entity, not this one.
How can I create that select using this other table here? How can I solve this?
Thanks!
EDIT:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name', 'text', ['required' => false])
->add('address', 'text', ['required' => false])
->add('zipCode', 'text', ['required' => false])
->add('city', 'text', ['required' => false]);
$builder->add('country', 'entity', [
'class' => 'AcmeBundle:Country',
'property' => 'shortName',
'required' => false,
]);
// $builder->add('country', 'text', ['required' => false]);
$builder->add('number', 'text', ['required' => false]);
}
That's the whole method inside the, let's call it, Foo entity. It generates proper HTML with ids as values, but there are still those errors I mentioned in the comment.
You need the entity Field Type. Your FormType should look like this:
$builder->add('country', 'entity', array(
'class' => 'YourBundle:Country',
'property' => 'name',
));
Checkout the docs for more info.