Form not sending out select and checkbox inputs in Angular 2 - forms

I'm working on a small Angular 2 To-do app. I didn't want to have any problems with browser compatibility with inputs types like date, datetime-local, etc., so I did make <select> inputs for user to type in the date and time. Everything works fine, inputs are dynamic, so the user cannot choose the day that does not exist (like e.g. 02/29/2017), etc.
The problem is, I want to send form's data to the service and then to the Back-End of my app, but when I submit the form, values from <select> inputs are not included in the sent object, as well as my checkbox input. I'm not often using these types of inputs, so I'm sorry if this is a noobish question, but I cannot figure out what I'm doing wrong.
Here is the code:
<form #f="ngForm" (ngSubmit)="add(f.value)">
<div *ngIf="error">
<p>{{ error }}</p>
</div>
<div class="login-input-container">
<input type="text" placeholder="title" name="title" ngModel autocomplete="off" required minlength="1" maxlength="100">
</div>
<div class="login-input-container">
<div class="datetime-container">
<div>
<select #year name="year" (change)="showMonths(); selectedYear = year.value; yearDays(year.value);" required>
<option class="invisible" value="" disabled selected>year</option>
<option *ngFor="let year of years" [value]="year" placeholder="year">{{ year }}</option>
</select>
<select #month *ngIf="showedMonths" name="month" (change)="showDays(month.value, year.value); selectedMonth = month.value;" required>
<option class="invisible" value="" disabled selected>M</option>
<option *ngFor="let month of months" [value]="month">{{ month }}</option>
</select>
<select *ngIf="showedDays" name="day" (change)="showTime()" required>
<option class="invisible" value="" disabled selected>d</option>
<option *ngFor="let day of days" [value]="day">{{ day }}</option>
</select>
</div>
<div *ngIf="showedTime">
<select name="hours" required>
<option class="invisible" value="" disabled selected>hh</option>
<option *ngFor="let hour of hours" [value]="hour">{{ hour }}</option>
</select>
:
<select name="minutes" required>
<option class="invisible" value="" disabled selected>mm</option>
<option *ngFor="let minute of minutes" [value]="minute">{{ minute }}</option>
</select>
</div>
</div>
</div>
<div class="login-input-container">
<textarea id="todo-description" type="text" placeholder="description (optional)" name="description" ngModel autocomplete="off" minlength="1" maxlength="500"></textarea>
</div>
<div class="login-input-container">
<span><p>should we notify you?</p><label for="notify-1"><input id="notify-1" type="checkbox" [checked]="todo.notify" value="1"><span></span></label></span>
</div>
<div class="login-input-container">
<input type="submit" value="Submit" (click)="error = ''">
</div>
</form>

If you use [value] or [ngValue] you need to use [(ngModel)] or ngModel (ngModelChange)="showMonths()..." instead of `(change)="..."

Related

Making a search form display inline with Bootstrap 5

I am trying to create a search form within a page using Bootstrap 5 and the following code:
<div class="row mb-5">
<div class="col-lg-10 offset-lg-1">
<form class="form-inline" role="form">
<label for="field">Find all where...</label>
<select id="field" class="form-select">
<option value="company_name" selected>Company Name</option>
<option value="federal_ein" selected>EIN</option>
<option value="state">State</option>
<option value="city">City</option>
</select>
<label for="term">includes...</label>
<input type="text" class="form-control" id="term" placeholder="Enter full/partial term" name="term">
<button type="submit" class="btn btn-primary btn-lg">Search</button>
</form>
</div>
</div>
I want the form to be inline, with the text box stretched to fill unused space in the row. I can't figure out how to achieve this. Any help on it? Thanks!
This code seems to work well. Thanks!
<form id="frmSearch" name="frmSearch" role="form" class="was-validated">
<div class="row mb-4">
<div class="col-lg-2 offset-lg-1 text-end">Find companies where...</div>
<div class="col-lg-2 text-start">
<select ID="ddlType" Class="form-control rounded" required>
<option value="" selected>Search by...</option>
<option value="company_name">Company Name</option>
<option value="city">City</option>
<option value="federal_ein">EIN</option>
<option value="state">State</option>
</select>
</div>
<div class="col-lg-1 text-center">contains...</div>
<div class="col-lg-4 d-flex">
<input type="text" ID="tbTerm" class="form-control rounded text-black" required />
</div>
<div class="col-lg-1 mx-auto">
<button type="submit" ID="btnSearch" class="btn-success btn text-white">Search</button>
</div>
</div>
</form>

Load dropdown item but disable for selection

So I want to be able to load the 'Choose' dropdown option, but disable it as soon as a change is made on the drop-down.
I've attempted adding a disabled attribute tag on the selection, but it loads on 'Alabama' and I don't want that.
How would I be able to tackle this?
<div class="row mt-3">
<div class="col-md">
<label for="resident">I am a resident of:</label>
</div>
</div>
Adding hidden and selected to the first option should do the trick.
<div class="row mt-3">
<div class="col-md">
<label for="resident">I am a resident of:</label>
<select class="custom-select" id="resident" required>
<option hidden selected value="">Choose...</option>
<option>Alabama</option>
<option>Alaska</option>
<option>Colorado</option>
<option>New York</option>
<option>South Dakota</option>
</select>
<div class="invalid-feedback">
Select state of residence.
</div>
</div>
</div>
If you want to still see the "Choose..." after the user has made a selection, replace hidden with disabled.
Why don't you disable the first option? (no javascript is needed and the result is like what you want to achieve I think):
<div class="row mt-3">
<div class="col-md">
<label for="resident">I am a resident of:</label>
<select class="custom-select" id="resident" required>
<option value="" disabled>Choose...</option>
<?php foreach (Vars::states() as $abbr => $state): ?>
<option value="<?= $abbr; ?>"><?= $state; ?></option>
<?php endforeach; ?>
</select>
<div class="invalid-feedback">
Select state of residence.
</div>
</div>
</div>
And a demo code snippet of what it looks like:
<select required>
<option value="" disabled selected>Select your option</option>
<option value="1">Alabama</option>
<option value="2">Alaska</option>
<option value="3">...</option>
</select>

How to put glyphicon in bootstrap form?

I have a bootstrap form and want to add a glyphicon before the SELECT fields.
However, it makes a breakline somewhy.
<form role="form" id="registration-form" onsubmit="return onSubmitContinueButton()">
<div class="row">
<div class="form-group col-md-6">
<label class="sr-only" for="xxx"></label>
<span class="glyphicon glyphicon-name"></span> <!-- <-- there is a breakline after this.... why?-->
<select class="form-control" name="aaa">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
<div class="form-group col-md-6">
<label class="sr-only" for="xxx"></label>
<span class="glyphicon glyphicon-name"></span> <!-- <-- there is a breakline after this.... why?-->
<select class="form-control" name="aaa">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
</form>
I want the glyphicon to be just near the select field and not above it.
How can I fix it?
Thanks
If you want to add icons in front of the select field make it an input group like this
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><span class="glyphicon glyphicon-plus"></span></span>
<select class="form-control" name="aaa">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
For more information look right here: Bootstrap input groups

URL direct depending on form selection

I have a simple form which I would like to redirect the user to a specific URL within my site, depending on the value they have selected. So far I have this but instead of directing to one of the 4 urls, I instead get "http://mydomain.com/?ApplianceType=americano.html&sub=Submit" in my address bar:
<form name="form1" method="get">
<p>
<label>
<select name="ApplianceType" id="ApplianceType">
<option value="americano.html">American Fridge Freezer</option>
<option value="freezer.html">Freezer only</option>
<option value="fridge.html">Fridge only</option>
<option value="fridgefreezer.html">Fridge/Freezer</option>
</select>
</label>
<label>
<input type="submit" name="sub" id="sub" value="Submit">
</label>
</p>
</form>
Can anyone please advise?
You can change your HTML:
<form name="form1" id="yourForm" action="americano.html" method="get"> <!-- Added an ID to the form, and set the default value of `action` to 'americano.html'. -->
<p>
<label>
<select name="ApplianceType" id="ApplianceType" onchange="setURL(this)"> <!-- Assigned the `select` to run the function on value change. -->
<option value="americano.html">American Fridge Freezer</option>
<option value="freezer.html">Freezer only</option>
<option value="fridge.html">Fridge only</option>
<option value="fridgefreezer.html">Fridge/Freezer</option>
</select>
</label>
<label>
<input type="submit" name="sub" id="sub" value="Submit">
</label>
</p>
</form>
Then use the following Javascript function:
<script>
function setURL(selectedValue)
{
var element = document.getElementById('yourForm'); // Change 'yourForm' to the actual ID you assign on your form.
element.action = selectedValue.value;
}
</script>
Please take note of the comments in the code.

Some fields are binding on postback some don't

I have a view containing a number of fields
<div class="editor-label">
<label for="Supplier">Supplier</label>
</div>
<div class="editor-field">
<input id="Supplier" name="Supplier" type="text" value="Swifts Antiference Division Ltd" />
<input id="SupplierId" name="SupplierId" type="hidden" value="1" />
</div>
<div class="editor-label">
<label for="Scheme_Group">Group</label>
</div>
<div class="editor-field">
<select id="Scheme_Group_Id" name="Scheme.Group.Id"><option value="">Select group</option>
<option value="3">Gels</option>
<option value="2">Gloves</option>
<option selected="selected" value="1">Needles</option>
<option value="4">Soap</option>
</select>
The problem I am having is when the form these fields are contained within is submitted to teh controler action only some of the values are received by the action. The Supplier and SupplierId fields work as expected but the "Scheme_Group_Id" is never populated.
You should also post how your action is declared and what type are you passing to the view.
Please check the binding in the action parameters, something like this :
public void Edit([Bind(Prefix = "ClassName")]string fieldName){ ...action code...}
More information in here MSDN