how to render symfony4 forms with same classes as symfony3? - css

In symfony 3 the forms were rendered with the classes "control-label" for the label element and "form-control" for the input element, and both wrapped in a div with the class "form-group" like this:
<div class="form-group">
<label class="control-label" for="hazardlogbundle_acrgroup_serial">Serial</label>
<input type="number" id="hazardlogbundle_acrgroup_serial" name="hazardlogbundle_acrgroup[serial]" class="form-control">
</div>
In symfony4 however, all three of these classes have been omitted, the same form now looks like this:
<div>
<label for="hazardlogbundle_acrgroup_name" class="required control-label">Grupp (ATS) namn</label>
<input type="text" id="hazardlogbundle_acrgroup_name" name="hazardlogbundle_acrgroup[name]" required="required" class="form-control">
</div>
I happened to use these three classes (or actually, bootstrap did) for my css styling ๐Ÿ˜‡ Is there an easy way of getting these back?
To get the form-control on the actual input I can use:
$builder->add('name', TextType::class, array('label' => 'Grupp (ATS) namn', 'attr' => array('class' => 'form-control')));
But that doesn't solve the problem for the wrapping div or the label itself... I understand I can go and create custom twig templates for each and every form, but no thank you ๐Ÿ˜Ž

You can assign "themes" to the Symfony Forms. There are already themes from Symfony for Bootstrap 3,4 and 5. For that you need to specify the theme in the config file of twig. (config/packages/twig.yaml)
For bootstrap 3:
twig:
form_themes: ['bootstrap_3_layout.html.twig']
For bootstrap 4:
twig:
form_themes: ['bootstrap_4_layout.html.twig']
In Symfony 4 there seems to be no predefined theme for bootstrap 5 yet.
You can read more here: https://symfony.com/doc/4.3/form/form_themes.html

Related

AngularJS ng-style with ng-model scope variable

I'm building a website to let users design their own mobile app. One side of the website has a form that the user fills out, and the other side has a live preview of the app based off of the form data. This is how it is set up:
Controller:
$scope.primaryColor = "#325490";
Form Input:
<!-- Primary Color -->
<div class="form-group">
<label for="primaryColor" class="col-sm-2 control-label">Primary:</label>
<div class="col-sm-10">
<input type="text" class="form-control" ng-model="$parent.primaryColor">
</div>
</div>
Live Preview:
<div class="mockView" ng-style="{'background-image':'url({{$parent.backgroundFile}})', 'background-color':'{{$parent.primaryColor}}'}">
I have to use $parent because I am using ng-include on my index page to include the form.htm and preview.htm files. I have tested the form and I know it is changing all of the scope variables that I have, but the previewer is not changing. Any help is appreciated!
Remove {{}} and '' and it should work, like this:
<div class="mockView" ng-style="{'background-image':'url({{$parent.backgroundFile}})', 'background-color': $parent.primaryColor }">

What does Bootstrap's control-label class do?

What CSS formatting is applied to the <label> HTML element by the .control-label Bootstrap 3 class. (I also face difficulties locating that class using Chrome's devtools)
Moreover, in the official Bootstrap 3 documentation, .control-label seems to be used only in case of .form-horizontal. Is that true and why?
The control-label class is useful for validation states, that's why we need it in all labels even the fields bootstrap's documentation doesn't mention.
We can see it in the bootstrap source code when it is defining the has-success, has-warning, etc classes: https://github.com/twbs/bootstrap/blob/bfb99413eefbbe2e8fbb1e477cbfa63ea7d36140/dist/css/bootstrap-rtl.css#L3242
As you can see, it uses the control-label class not the label element. If we remove the control-label we'll have an undesired effect of not coloring the label green.
Vertical form without control-label class and has-success on form-group:
<div class="form-group has-success">
<label for="exampleInputEmail1">Email address</label>
<input type="email" class="form-control" id="exampleInputEmail1" placeholder="Enter email">
</div>
Using control-label class:
<label for="exampleInputEmail1" class='control-label'>Email address</label>
That's why I think it is better to keep it! Unless no color is the desired effect.

Styling Checkbox in Laravel Form

I am using Laravel Forms package to generate the form elements. I am using Bootstrap Material design library to get the elements shiny.
Library: https://fezvrasta.github.io/bootstrap-material-design/#checkbox
For Checkboxes, I am not able to get the style as required. It displays a normal checkbox without any material design style.
Below is my code in Laravel view.
{!! Form::checkbox('remember', 1, null, ['id'=>'remember', 'class' => 'checkbox']) !!}
{!! Form::label('remember', 'Remember me') !!}
Instead of the above code, if I use the direct HTML code, it works. Below is the code that works as expected.
<div class="checkbox">
<label>
<input name="remember" type="checkbox"> Remember me
</label>
</div>
How can I generate the above code using Laravel Forms?
The styling of the checkbox is on input[type="checkbox"] and not in the checkbox class.
create a css as
.checkbox {}
to style the checkbox.

How to access each items of form_widget(form.**)

How to access the each form item
$builder->add('icon', 'entity', array(
'class' => 'UserBundle:IconPics',
'property' => โ€˜label', 'expanded' => true, 'multiple' => false,
));
in twig
{{ form_label(form.icon) }}
{{ form_widget(form.icon) }}
it show this codes automatically and works well as radio button selector.
<div id="fos_user_registration_form_icon">
<input type="radio" id="fos_user_registration_form_icon_1" name="fos_user_registration_form[icon]" required="required" value="1" />
<label for="fos_user_registration_form_icon_1" class="required">pictureA.jpg</label>
<input type="radio" id="fos_user_registration_form_icon_2" name="fos_user_registration_form[icon]" required="required" value="2" />
<label for="fos_user_registration_form_icon_2" class="required">pictureB.jpg</label></div>
But I want to access each items in this code manually.
Since,I want to change code like this.
pictureB.jpg --> <img src="pictureB.jpg">
pictureA.jpg --> <img src="pictureA.jpg">
Is it possible??
If you want to customise the way any form widget is rendered, you've then to override it. Take a deeper look at How to customize Form Rendering section of the documentation.
You can find here the default behaviour of all the form fields widget blocks. As described in the documentation, you've to,
First, know which block to override.
Then, Override the block using form theming (the documentation
is full of examples whether you need to override your widget only
when a given template is concerned or globally on your application)

Default CSS3 Checkbox Template in MVC

I've been trying to get the default checkbox template written for my Boolean editors and I've run into an issue due to how MVC Razor renders multiple input elements for a single boolean model property.
I have this template defined:
#model Boolean?
<div class="check-box">
#Html.CheckBox("", Model.HasValue && Model.Value)
#Html.LabelForWithoutText(m => m, new object())
</div>
If I manually write out the HTML like:
<div class="check-box">
<input type="checkbox" title="Create?" value="true" name="check" id="chkCreate">
<label title="Create?" for="chkCreate"></label>
</div>
Everything works fine.
But when Razor renders my template on a Boolean property of a model the html is rather different. Due to how MVC renders other hidden inputs for posting booleans back to action methods.
The Razor version looks like this:
<div class="check-box">
<input type="checkbox" value="true" name="GloballyShare" id="GloballyShare" data-val-required="The GloballyShare field is required." data-val="true">
<input type="hidden" value="false" name="GloballyShare">
<label title="GloballyShare" for="GloballyShare"></label>
</div>
The problem is the extra hidden input. I don't want to change this behaviour as that will globally effect how MVC form work by default and I can't think of a way to deal with this in CSS.
So I'm wondering how this could be achieved. You can see a working example of the problem here:
Default CSS3 Checkbox Template in MVC
If you try it then remove the hidden input element and try it again the top most checkbox starts working the same as the bottom checkbox
I've just managed to fix the jsFiddle.
I changed the label selector from a + to a ~ and both checkboxes are now working:
.check-box input[type=checkbox]:checked + label {
Changed to:
.check-box input[type=checkbox]:checked ~ label {
Fixed jsFiddle

Resources