cant translate name UploadButton in my Form - php

i got a problem in my form. Im translating all my fields with message.yml and it works. But i have also a upload button from VichUploaderBundle in it. I can translate the label, but when im testing it, the label is in english, but the button is in german.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', TextType::class, array('disabled' => true))
->add('title',TextType::class, array('label' => 'edit.title'))
->add('body', TextareaType::class, array('label' => 'edit.body'))
->add('date', DateType::class, array('disabled' => true, 'label' => 'edit.date'))
->add('tags', EntityType::class, array(
'class' => 'AppBundle:Tag',
'choice_label' => 'getTitle',
'multiple'=> true,
'expanded'=> true))
->add(
'technology',
EntityType::class,
array(
'class' => 'AppBundle\Entity\Technology',
'choice_label' => 'getTitle',
'group_by' => 'parent.getTitle',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('t')
->where('t.parent IS NOT NULL')
->andWhere('SIZE(t.children) <= 0');
}
))
->add('imageFile', VichImageType::class, array(
'required' => false,
'allow_delete' => true,
'download_link' => true,
'label' => 'edit.image_file',
))
;
}
I dont know how to translate the button.

You are only creating a html file input.
This input does not have some label attribute.
<input type="file" name="imageFile"/>
-> the button label is set by your browser (which seems to be in german).
You can try to change it with CSS/JS

These text is defined by the browser and not by PHP or Symfony. You will also notice that this widget will be displayed differently in different browsers and on different operating systems. There is also no possibility to change the design of the widget. Most CSS properties won't have an effect on .
The only thing that you could do is to implement a custom JavaScript uploader, but that will probably only work in modern browsers.
I have not never done this, but a quick Google search brought up http://www.queness.com/post/11434/7-javascript-ajax-file-upload-plugins that features few JavaScript plugins to do this.

Related

Add custom class to Symfony form label

I am building a Symfony 2.7 form and I would like to be able to customise the labels.
At the moment I am able to define the labels using the form builder outlined in the Symfony docs, however I can't see any way to customise the CSS of the label without having to do it on the template.
I know that I would be able to add the CSS class by individually printing the form_label inside of a with my class, but I would prefer to keep it all inside of the form builder.
I've added my current form below for reference.
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('dob_day', 'choice', array(
'required' => true,
'choices' => $dobDays,
'mapped' => false,
'attr' => array(
'label' => 'Day',
'class' => 'form-control'
)
))
->add('dob_month', 'choice', array(
'required' => true,
'choices' => $dobMonths,
'mapped' => false,
'attr' => array(
'label' => 'Month',
'class' => 'form-control'
)
))
->add('dob_year', 'choice', array(
'required' => true,
'choices' => $dobYears,
'mapped' => false,
'attr' => array(
'label' => 'Year',
'class' => 'form-control'
)
));
}
If you want to add some CSS to your HTML in the form type, use the style attribute.
->add('dob_year', 'choice', array(
'required' => true,
'choices' => $dobYears,
'mapped' => false,
'attr' => array(
'label' => 'Year',
'class' => 'form-control',
'style' => 'max-width: 4rem;'. // Concat. the styles across lines for clarity.
'border: solid 0.1rem teal;'
)
));
That said,
inside the formType is probably the last place I'd go looking to modify these elements' styles if I was the poor dev who had to maintain this form when you were gone.
While it's possible to add inline CSS here, don't put more than a few lines. If it grows to be more, add a class to some external 'main.css' then add that to your primary template (that all others inherit from) and you'll be able to reuse the styles you've made.
Last thought
This is actually pretty helpful when you're trying to make your formType modular and is why I wanted to answer :) Then I got carried away in a best practice mantra that came off as if this technique doesn't have its place, but it does!

Symfony2.7, having problems with a form collections field and Doctrine

I have a Blog entity which is supposed to have one Poll entity and each Poll entity has many Choice entities associated to it. I was trying to create an "Add/Edit Poll" form for the blog. In that form, in addition to the Poll form fields I want to have a field that allows you to add Choices (a simple text field each). I'm having trouble figuring out how I should code this so that it works well. The PollType form looks like this:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text');
$builder->add('choices', 'collection',
array(
'label_attr' => array(
'style' => 'vertical-align: top;'
),
'type' => 'text',
'required' => false,
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'attr' => array(
'style' => 'display: inline-block; margin: 0 0 20px 20px; width: 200px;'
)));
$builder->add('description', 'textarea', array('attr' => array('rows' => 6)));
$builder->add('save', 'submit', array('label' => 'Create Poll'));
$builder->getForm();
}
Originally I had 'type' => new ChoiceType() with ChoiceType being:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('title', 'text');
}
But I had to remove all this because "Variable "expanded" does not exist in bootstrap_3_layout.html.twig" error that I could not find any solution for. I changed the type to text but now I'm stumped as to how I would collect the data from each "choices" field, persist it to the Choices entity, and also associating all of them with the new Poll (which may not have been inserted yet so I don't have an ID to add to Choices->pollID). Is there a standard way for doing this kind of thing that I'm missing?
If you have Poll entity and Choice entity, so it looks like this:
$builder->add('choice', 'entity', array(
'class' => 'AcmeHelloBundle:Choice',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('c')
->orderBy('choice.name', 'ASC');
},
));
Also you must have OneToMany or ManyToMany association with your entities.
More about entity Field Type

SonataAdmin - Custom form template for each form

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().'" />'
))
)

How to add mapping to a form

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.

symfony2 form entity add values

I just need to add other defaults options to an "entity field" in Symfony2. It displays names, and I need an option "Name not in list", but I cant find a way to achieve it. Data transformers cant seem to fix my problem though.
$builder
->add('family', 'entity', array(
'class' => 'InterneFichierBundle:Family',
'property' => 'Name'
))
If the name of the family is not in the list, there should be an option "name not in list"..
Thanks a lot !
i'm pretty sure you can just specify an empty value option:
$builder
->add('family', 'entity', array(
'class' => 'InterneFichierBundle:Family',
'property' => 'Name',
'empty_value' => 'Name not in list',
))
see http://symfony.com/doc/current/reference/forms/types/entity.html#empty-value
You shouk try with :
for information you can see it here : http://symfony.com/fr/doc/current/reference/forms/types/entity.html
$builder->add('users', 'entity', array(
'class' => 'AcmeHelloBundle:User',
'query_builder' => function(EntityRepository $er) {
return $er->createQueryBuilder('u')
->orderBy('u.username', 'ASC');
},
));

Categories