display an image inside a text field in laravel 5.3 - laravel-5.3

Is it possible to place an image inside Form::text() in Laravel? I have tried this:
{{ Form::text('Photo', asset('images/img.jpg'), array('class' => 'form-control', 'required' => '')) }}
but I get this on my view:
http://localhost/HBS/public/images/img.jpg

Related

How to get a custom value for a field type in sonata?

With Sonata, when I create a contract with a choiceType, the user can choose contract1 or contract2 and in my database I would get "451" for contract1 and "678" for contract2.
In my Field List all my data are displayed but for my contract I've got either "451" or "678" and I would like instead of those numbers, contract1 or contract2.
This is my field for creating the contract :
$mapper
->add('contract', ChoiceType::class, [
'choices' => [
'contract1' => '451',
'contract2' => '678',
],
])
And in my code for the field, I don't know how to tell it if 451 then 'contract1'. I started like that :
->add('contract', null, [
'label' => 'Contract',
])
Any idea ?
You can use the form entity type which would solve your problem:
$builder->add('contract', EntityType::class, [
// looks for choices from this entity
'class' => Contract::class,
// uses the Contrzct.name property as the visible option string
'choice_label' => 'name',
// Query builder to select your to specific contract
'query_builder' => function (ContractRepositoty $contractRepository) {
return $contractRepository->createQueryBuilder('support_time_slot')
->where('contract.id in :ids')
->setParameter('ids', [461,678])
->orderBy('contract.name');
},
// used to render a select box, check boxes or radios
'multiple' => true,
'expanded' => true,
]);
I found a solution. I created a specific template and in it I translated the value I wanted:
->add('contract', null, [
'label' => 'Contract',
'template' => 'AdminBundle:ContractAdmin:list__operation.html.twig'
])
And my Twig :
{% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %}
{% block field %}
{% if value %}
{{ ('contract.operation.'~value~'.value')|trans }}
{% endif %}
{% endblock %}

Symfony Form builder: how to organise/group fields into HTML sections from the controller

I would like in the form builder controller to say this field belongs to this group then in the view/theme organize all of group1 into div1, group2 into div2, etc..
I tried something like this (creating sub forms) but that is not working; I am not able to display the group
$builder->add(
$builder->create('group1', FormType::class, array('inherit_data' => true))
->add('brand', TextType::class, array(
'label' => 'brand',
'invalid_message' => 'Enter a brand',
))
);
twig
{{ form_row(form.group1) }}
In case someone else is looking; my error was that I forgot to loop through
{% for row in form.group1 %}
{{ form_row(row) }}
{% endfor %}

Custom Symfony ChoiceList way of rendering choices (grouped in divs)

I have a choice form field that has a lot of options that I need to group somehow so that they will be rendered divided in groups, maybe placed inside different divs.
I'm currently trying to implement the ChoiceListInterface to achieve that, but I don't know how to implement the methods so I can render the choices divided by groups, but the docs does not clarify how to do that..
The choices always get rendered together.
You have this array
$grouped_choices = array(
'Swedish Cars' => array(
'volvo' => 'Volvo',
'saab' => 'Saab',
),
'German Cars' => array(
'mercedes' => 'Mercedes',
'audi' => 'Audi'
)
);
First way: quick and simple
$builder->add($name, 'choice', array(
'choices' => $grouped_choices),
)
But I don't thinks it works with 'expanded' => true
So there is another way, more customizable (maybe more dirty)
In your FormType
foreach($grouped_choices as $name => $choices) {
$builder->add($name, 'choice', array(
'choices' => $choices),
'expanded' => true, //custom the widgets like you wants
);
}
Let the controller send the array to the view, then in the view
{% for name, choices in grouped_choices %}
<div class="whatever">
{{ form_row(name) }}
</div>
{% endfor %}

Where can I change the form text box size?

I make form in Controller like this below.
$form = $this->createFormBuilder($row)
->add('comment',null,array('label' => 'input comment'))
then in the twig file...
{{form_widget(form.commentToMutor)}}
It shows text input box,but it is too small.
How to change size of TextBox
Extending #manseuk 's answer (I don't have enough reputation to post a comment), you can also specify the html style attribute inside the form builder, if you preffer:
$form = $this->createFormBuilder($row)
->add('comment', null, array(
'label' => 'input comment',
'attr' => array('style' => 'width: 200px')
)
);
Edited from html attribute for width to html style attribute.
You could add a class to the form field :
$form = $this->createFormBuilder($row)
->add('comment',null,array(
'label' => 'input comment',
'attr' => array('class' => 'myclass')
)
);
and then create the CSS relevant to that class :
.myclass {
width: 200px;
}
Docs for the attr attribute here
Or in the twig file:
{# Define CSS class and call #}
{{ form_widget(form.commentToMutor, { 'attr': {'class': 'myclass'} }) }}
{# ... or enter width directly #}
{{ form_widget(form.commentToMutor, { 'attr': {'style': 'width: 200px'} }) }}
More here
Setting the width directly did not work for me. I had to set it through the style. Of course this is an easy fix, the correct way is to use a css class like others have suggested.
$form = $this->createFormBuilder($row)
->add('comment', null, array(
'label' => 'input comment',
'attr' => array('style' => 'width:200px')
)
);

Symfony2 - custom css for select in forms

i have a little problem with the rendering of my forms.
I would like my different select box have different size.
<div >
{{ form_label(form.date_of_birth, 'date de naissance') }}
{{ form_widget(form.date_of_birth )}}
</div>
<div >
{{ form_label(form.Alimentation, 'Alimentation') }}
{{ form_widget(form.Alimentation)}}
</div>
This two widgets renders select box. I want the two to have a different size, one large, one thin.. But, i can't directly set the style of the , and set the width of the div does nothing.
I also tried this:
->add('date_of_birth', 'date', array('required' => false, 'attr' => array('style' => 'width:330px' ),
'widget' => 'choice',
'format' => 'dd-MM-yyyy',
'years' => range(1970,2012)))
This does nothing.
Explained in Twig Template Form Function Reference http://symfony.com/doc/current/reference/forms/twig_reference.html
{{ form_widget(form.name, { 'attr' : { 'class' : 'yourClass' } }) }}
Now you have added a class attribute to your form field with yourClass as css class. So you can style your field using css.

Resources