Bootstrap Form checkbox and dropdown are not properly aligned with their labels - twitter-bootstrap-3

As you can see in the picture below, the input tag aligns properly with its label. But the dropdown is not horizontally in the same line as its label; it's a bit upward vertically. Same is true for the checkbox. I would like to see them exactly in the same line as their labels. I'm using ASP.NET Core that comes with latest version of Bootstrap by default. Issue probably is not related to ASP.NET, but just in case, following is the Form in ASP.NET where tag helpers are used. NOTE: The model property Test1 in my ASP.NET model is of type bool and therefore the ASP.NET render <input asp-for="Test1" class="form-control" style="zoom:0.5;" /> as a checkbox. But that, I think, is not relevant here.
<form asp-controller="myController" asp-action="myAction" method="post" class="form-horizontal">
<div class="form-group">
<label asp-for="ProjectTitle" class="col-md-2 control-label"></label>
<div class="col-md-10">
<input asp-for="ProjectTitle" class="form-control" />
<span asp-validation-for="ProjectTitle" class="text-danger"></span>
</div>
</div>
<div class="form-group">
<label asp-for="SelectedProjStatus" class="col-md-2 control-label"></label>
<div class="col-md-10">
<select asp-for="SelectedProjStatus" asp-items="Model.lstProjStatuses"></select>
</div>
</div>
<div class="form-group">
<label asp-for="Test1" class="col-md-2 control-label"></label>
<div class="col-md-1">
<input asp-for="Test1" class="form-control" style="zoom:0.5;" />
<span asp-validation-for="Test1" class="text-danger"></span>
</div>
</div>
</form>

Related

How do I bind a Razor partial to a sub-property of its parent's model?

I have a form in a Razor page containing loads of fields, all with identical markup.
#page "{id:int}"
#model RazorPagesMovie.Pages.Movies.EditModel
<form method="post">
<div class="form-group">
<label asp-for="Movie.Title" class="control-label"></label>
<input asp-for="Movie.Title" class="form-control" />
<span asp-validation-for="Movie.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.ReleaseDate" class="control-label"></label>
<input asp-for="Movie.ReleaseDate" class="form-control" />
<span asp-validation-for="Movie.ReleaseDate" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.Genre" class="control-label"></label>
<input asp-for="Movie.Genre" class="form-control" />
<span asp-validation-for="Movie.Genre" class="text-danger"></span>
</div>
</form>
Rather than my page file being stuffed with all this copy/paste I decide to eliminate the code duplication using the feature the ASP documentation says I should be using for this, namely a partial.
So I create a partial
#model object
<div class="form-group">
<label asp-for="#Model" class="control-label"></label>
<input asp-for="#Model" class="form-control" />
<span asp-validation-for="#Model" class="text-danger"></span>
</div>
...and reference that from my page file using
<partial name="_MovieField" for="Movie.Title" />
Only problem, nothing works. The asp-for="#Model" in the label doesn't resolve the display name for the property so the label is blank, and none of the validation works either.
It seems that if this can be make to work then some kind of specific technique is required to pass a sub-property of a model to a partial in such a way that any tag helpers will still work. Unfortunately the ASP Core documentation doesn't provide any clue as to go about this.
Does anyone know how to get this to work?

How can I re-use code that uses asp-for in an ASP.NET Core MVC app?

I'm developing an app using ASP.NET Core MVC. In the app, there are many forms. The input sections in the forms consist of some HTML that is repeated a lot. For example, for a checkbox, we're using 13 lines of HTML and in a form, we might have tens of checkboxes. Each checkbox consists of labels and input tags and in those tags, we're using the asp-for tag helper.
I was thinking that maybe we could use a partial view in order to only use one line per checkbox and limit duplication.
I tried something like this
Partial view:
#model object
<div class="form-group form-md-checkboxes">
<label asp-for="#Model" class="control-label col-md-3"></label>
<div class="col-md-9 control-label">
<div class="md-checkbox">
<input asp-for="#Model" class="md-check" />
<label asp-for="#Model">
<span></span>
<span class="check"></span>
<span class="box"></span>
</label>
</div>
<span asp-validation-for="#Model" class="text-danger"></span>
</div>
</div>
In the form in the main razor page where the model contains multiple bool properties.
<form>
...
<partial name="_checkbox" for="BoolPropertyA" />
<partial name="_checkbox" for="BoolPropertyB" />
<partial name="_checkbox" for="BoolPropertyC" />
...
</form>
This doesn't seem to work properly, however. The Label doesn't get any text. Is what I'm trying to do at all possible? If so where am I going wrong?
Here is a working demo:
Partial view(I use string as a model and repalce tag helpers with html code):
#model string
<div class="form-group form-md-checkboxes">
<label for="#Model" class="control-label col-md-3" >#Model</label>
<div class="col-md-9 control-label">
<div class="md-checkbox">
<input id="#Model" name="#Model" class="md-check" />
<label for="#Model">
<span></span>
<span class="check"></span>
<span class="box"></span>
</label>
</div>
<span class="text-danger field-validation-valid" data-valmsg-for="#Model" data-valmsg-replace="true"></span>
</div>
</div>
form:
<form id="form1" method="post" asp-action="Form2">
#await Html.PartialAsync("_checkbox", "BoolPropertyA")
#await Html.PartialAsync("_checkbox", "BoolPropertyA")
#await Html.PartialAsync("_checkbox", "BoolPropertyA")
</form>
result:

Bootstrap - span element is not aligned to label

My form is to be capable of switching between view and edit modes. By default the form should display in view mode and when I click edit button the fields should be available for change. The form is working good between view and edit modes as expected, but in view mode the span element (for display value) is misaligned with the label, I mean the span element's value is displaying a bit higher to the horizontal line of the label.
Below is a piece of code from my form:
div class="container">
<h1>Deal Form</h1>
<form class="form-horizontal" (ngSubmit)="onSubmit()" #dealForm="ngForm">
<div class="form-group">
<label class="control-label col-sm-2">First Name</label>
<span class="control-label" *ngIf="!editMode">{{model.name}</span>
<div class="col-sm-6" *ngIf="editMode">
<input class="form-control" type="text" name="name" [(ngModel)]="model.name" />
</div>
</div>
</form>
</div>
Use a form static control instead of a <span> element:
div class="container">
<h1>Deal Form</h1>
<form class="form-horizontal" (ngSubmit)="onSubmit()" #dealForm="ngForm">
<div class="form-group">
<label class="control-label col-sm-2">First Name</label>
<div class="col-sm-6" *ngIf="editMode">
<p class="form-control-static" *ngIf="!editMode">{{model.name}}</p>
<input class="form-control" type="text" name="name" [(ngModel)]="model.name" />
</div>
</div>
</form>
</div>
Here is a "static" Bootstrap snippet (view in full page):
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="container">
<h1>Deal Form</h1>
<form class="form-horizontal" (ngSubmit)="onSubmit()" #dealForm="ngForm">
<div class="form-group">
<label class="control-label col-sm-2">First Name</label>
<div class="col-sm-6">
<input class="form-control" type="text" name="name" value="model name" />
</div>
</div>
</form>
</div>
<div class="container">
<h1>Deal Form</h1>
<form class="form-horizontal" (ngSubmit)="onSubmit()" #dealForm="ngForm">
<div class="form-group">
<label class="control-label col-sm-2">First Name</label>
<div class="col-sm-6">
<p class="form-control-static">model name</p>
</div>
</div>
</form>
</div>
It works now. Thanks for the help. However I have modified the code to meet my requirement and it's as below.
<div class="form-group">
<label class="control-label col-sm-2">First Name</label>
<div class="col-sm-6">
<p class="form-control-static" *ngIf="!editMode">{{var1}}</p>
<input class="form-control" *ngIf="editMode" type="text" name="var1" [(ngModel)]="var1" />
</div>
</div>

bootstrap 4 validation icons repeat all through the input width with prestshop 1.7

I'm creating a prestashop 1.7.2.1 module. Prestashop comes with bootstrap 4 support.
I'm trying to use the from validation example from https://v4-alpha.getbootstrap.com/components/forms/#validation with the following code:
<div class="container">
<form>
<div class="form-group row has-success">
<label for="inputHorizontalSuccess" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control form-control-success" id="inputHorizontalSuccess" placeholder="name#example.com">
<div class="form-control-feedback">Success! You've done it.</div>
<small class="form-text text-muted">Example help text that remains unchanged.</small>
</div>
</div>
<div class="form-group row has-warning">
<label for="inputHorizontalWarning" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control form-control-warning" id="inputHorizontalWarning" placeholder="name#example.com">
<div class="form-control-feedback">Shucks, check the formatting of that and try again.</div>
<small class="form-text text-muted">Example help text that remains unchanged.</small>
</div>
</div>
<div class="form-group row has-danger">
<label for="inputHorizontalDnger" class="col-sm-2 col-form-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control form-control-danger" id="inputHorizontalDnger" placeholder="name#example.com">
<div class="form-control-feedback">Sorry, that username's taken. Try another?</div>
<small class="form-text text-muted">Example help text that remains unchanged.</small>
</div>
</div>
</form>
</div>
using Pingendo 4 (bootstrap 4 desktop app editor) it works properly.
but when I add this code to my prestashop module all the inputs contains the validation icons repeated all through the input's width.
any ideas ?
PrestaShop's bootstrap CSS for these icons is being overriden by PrestaShop's own CSS.
When disabling the background property, it works fine.
You can modify the CSS and change background property to background-color.

How to create an array of sub-forms with CForm?

I have an array of models, all of them being instances of one class. I need to create a form which contains subforms corresponding to the models from the array.
Have to use CForm, because i've made a nice small extension of it, which can render subforms in CJuiTabs. In the code used at the moment POST-parameter names are wrong, because they get prefixed by the name of the class of a model from the array.
Below you can see a snippet of HTML-code generated. See those YPageInfo[...] named inputs? That's the problem - only one set of inputs with this prefix gets received by the server.
<div style="visibility:hidden"><input type="hidden" value="1" name="yform_20bd4d2c" id="yform_20bd4d2c" /></div>
<div class="row field_name">
<label for="YPageRevision_name">Name</label>
<input name="YPageRevision[name]" id="YPageRevision_name" type="text" maxlength="45" />
</div>
<div class="row field_parent_id">
<label for="YPageRevision_parent_id">Parent Id</label>
<input id="YPageRevision_parent_id_hidden" name="YPageRevision[parent_id]" type="hidden" /><input id="YPageRevision_parent_id" type="text" value="" name="autocomplete-field" />
</div>
<div id="yw1">
<ul>
<li>en</li>
<li>ru</li>
</ul>
<div id="enSubFormTab"><div class="row field_title">
<label for="YPageInfo_title" class="required">Title <span class="required">*</span></label>
<input name="YPageInfo[title]" id="YPageInfo_title" type="text" maxlength="256" />
</div>
<div class="row field_content">
<label for="YPageInfo_content" class="required">Content <span class="required">*</span></label>
<textarea name="YPageInfo[content]" id="YPageInfo_content"></textarea>
</div>
</div>
<div id="ruSubFormTab"><div class="row field_title">
<label for="YPageInfo_title" class="required">Title <span class="required">*</span></label>
<input name="YPageInfo[title]" id="YPageInfo_title" type="text" maxlength="256" />
</div>
<div class="row field_content">
<label for="YPageInfo_content" class="required">Content <span class="required">*</span></label>
<textarea name="YPageInfo[content]" id="YPageInfo_content"></textarea>
</div>
</div>
</div>
<div class="row buttons"><input name="submit" type="submit" value="Create" />
</div>
I found one way to do what I needed.
In the elements property of the sub-form there are some inputs declared. Each standard input element declared there is an instance of CFormInputElement, so it has the attributes property. Here you can set any HTML attribute, which will be rendered in the resulting input tag. I put my custom name attribute here, and change it in a loop, where i create sub-forms configuration items for the main CForm.
Maybe there is a better way?