HTML show "please fill out this field" message for Radio Field - html

I have a form with several inputs, some text, some radio / dropdown fields. All fields are required. For the text input I get a nice message saying "Fill out this field", but for the Radio and Dropdown fields the message doesn't show. How can I show the error message for radio and dropdown fields? Below is my code:
<form action="" method="post" class="form" role="form">
<div class="form-group required"><label class="form-control-label" for="name">Name:</label>
<input class="form-control" id="name" name="name" required="" type="text" value="">
</div>
<!-- ^ this works -->
<div class="form-group required"><label class="form-control-label" for="gender">Gender</label>
<div class="form-check">
<input class="form-check-input" id="preference-0" name="preference" type="radio" value="male">
<label class="form-check-label" for="preference-0">Male</label>
</div>
<div class="form-check">
<input class="form-check-input" id="preference-1" name="preference" type="radio" value="female">
<label class="form-check-label" for="preference-1">Female</label>
</div>
<div class="form-check">
<input class="form-check-input" id="preference-2" name="preference" type="radio" value="other">
<label class="form-check-label" for="preference-2">Other</label>
</div>
</div>
<!-- ^ this does not work -->
<div class="form-group required"><label class="form-control-label" for="choice">
Please choose "A" or "B"</label>
<select class="form-control" id="which_day" name="which_day" required="">
<option value="a">A</option>
<option value="b">B</option><
</select>
</div>
<!-- ^ neither does this... -->
<input class="btn btn-primary btn-md" id="submit" name="submit" type="submit" value="Next">
</form>

I figured it out now, at least for You need to put "required" at one of the radio / one of the dropdown fields like so:
<div class="form-check">
<input class="form-check-input" id="preference-0" name="preference" type="radio" value="male" required>
<label class="form-check-label" for="preference-0">Male</label>
</div>
And for select fields here is an answer that shows how it can be done.

Related

Angular Json Pipe TypeError: Converting circular structure to JSON

I have been trying to follow along with an angular course and came across the following error while using ngform:
core.js:6237 ERROR TypeError: Converting circular structure to JSON
--> starting at object with constructor 'TView'
| property 'blueprint' -> object with constructor 'LViewBlueprint'
--- index 1 closes the circle
Following is the template html:
<div class="container">
<h2>User Settings</h2>
<form #form="ngForm">
<div class="form-group">
<label for="name">Name</label>
<input id="name" name="name" class="form-control" placeholder="Name" />
</div>
<div class="form-check form-group">
<input class="form-check-input" type="checkbox" value="" id="emailOffers">
<label class="form-check-label" for="emailOffers">
Email Special Offers
</label>
</div>
<h5>User Interface Style</h5>
<div class="form-group">
<div class="form-check">
<input class="form-check-input" type="radio" name="InterfaceStyle" id="lightInterface" value="Light" checked>
<label class="form-check-label" for="lightInterface">
Light
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="InterfaceStyle" id="mediumInterface" value="Medium">
<label class="form-check-label" for="mediumInterface">
Medium
</label>
</div>
<div class="form-check">
<input class="form-check-input" type="radio" name="InterfaceStyle" id="darkInterface" value="Dark">
<label class="form-check-label" for="darkInterface">
Dark
</label>
</div>
</div>
<div class="form-group">
<label for="subscriptionType">Subscription Type</label>
<select class="form-control" id="subscriptionType">
<option>Monthly</option>
<option>Annual</option>
<option>Lifetime</option>
</select>
</div>
<div class="form-group">
<label for="notes">Notes</label>
<textarea class="form-control" id="notes" rows="3"></textarea>
</div>
<button class="btn btn-primary">Save</button>
</form>
</div>
{{ form | json }}
It throws you this error because it has some circular dependency on the controls so it throws you this error. You can try out the printing the value of it.
To print the entire formobject you can use
{{form.value | json}}
Stackblitz link => https://stackblitz.com/edit/angular-6-template-driven-form-validation-prcrob

If I add phone number to my HTML form, the add new hire button gets disable. Is there a limit to put html elements?

Following is the html code. All I wanted to know if there are limits to putting html elements for form designing.
<div class="container">
<h1>Add New Employee</h1>
<br>
<form action="process.php" method="post">
<div class="form-group">
<label for="name">Full Name:</label>
<input class="form-control" type="text" name="name">
</div>
<div class="form-group">
<label for="permanent_address">Permanent Address:</label>
<input class="form-control" type="text" name="permanent_address">
</div>
<div class="form-group">
<label for="marital_status">Marital Status:</label>
<select class="form-control" name="marital_status">
<option value="Married">Married</option>
<option value="Unmarried">Unmarried</option>
</select>
</div>
<div class="form-group">
<label for="gender">Gender: </label>
<input type="radio" value="Male" name="train">Male
<input type="radio" value="Female" name="train">Female
</div>
This is where the problem starts.
This is the reason i've disable phone number by commenting it becasue it is disabling the submit button.
<!-- <div class="form-group">
<label for="phone">Phone number: </label>
<input class="form-control" type="number" name="phone">
</div> -->
<div class="form-group">
<label for="admission_date">Admission Date: </label>
<input type="date" name="admission_date">
</div>
<div class="form-group">
<label for="Aadhar">Aadhar Number: </label>
<input class = "form-control" name="Aadhar">
</div>
<button class="btn btn-success" name="submitted">Add New Hire</button>
</form>
</div>
your code seems to run fine. There are no limits to how many elements you can have in a form - possibly limited my memory. Why do you think your form isn't working?
also for phone input try the following:
<input type="tel" id="phone" name="phone"
pattern="[0-9]{3}-[0-9]{3}-[0-9]{4}"
required>
<div class="container">
<h1>Add New Employee</h1>
<br>
<form action="https://www.google.com" method="post">
<div class="form-group">
<label for="name">Full Name:</label>
<input class="form-control" type="text" name="name">
</div>
<div class="form-group">
<label for="permanent_address">Permanent Address:</label>
<input class="form-control" type="text" name="permanent_address">
</div>
<div class="form-group">
<label for="marital_status">Marital Status:</label>
<select class="form-control" name="marital_status">
<option value="Married">Married</option>
<option value="Unmarried">Unmarried</option>
</select>
</div>
<div class="form-group">
<label for="gender">Gender: </label>
<input type="radio" value="Male" name="train">Male
<input type="radio" value="Female" name="train">Female
</div>
This is where the problem starts.
This is the reason i've disable phone number by commenting it becasue it is disabling the submit button.
<div class="form-group">
<label for="phone">Phone number: </label>
<input class="form-control" type="number" name="phone">
</div>
<div class="form-group">
<label for="admission_date">Admission Date: </label>
<input type="date" name="admission_date">
</div>
<div class="form-group">
<label for="Aadhar">Aadhar Number: </label>
<input class = "form-control" name="Aadhar">
</div>
<button class="btn btn-success" name="submitted">Add New Hire</button>
</form>
</div>

Angular2+ (click) event on radio button not working on Safari/iOS

The page is working perfectly in Chrome, IE or Firefox, but in Safari I have a weird problem. The (click) events that are inside the radio buttons are not working, the event is not fired. I have another (click) in a button later and it works fine. The problem happens on iPhone/iMac so is not device related.
The page is nothing more than a survey with some questions. Here is an example of html code:
<div class="col" *ngIf="!valueSelection">
<h4 for="">1.- Question 1</h4>
<div class="form-check">
<input class="form-check-input" type="radio" name="questionRMG1" id="questionRMG1_1" value="answerRMG1_1" [(ngModel)]="valueSelection"
(click)="onSelect(0.25)">
<label class="form-check-label" for="questionRMG1_1">Answer a.</label>
</div>
<br>
<div class="form-check">
<input class="form-check-input" type="radio" name="questionRMG1" id="questionRMG1_2" value="answerRMG1_2" [(ngModel)]="valueSelection"
(click)="onSelect(0.10)">
<label class="form-check-label" for="questionRMG1_2">Answer b./label>
</div>
<br>
<div class="form-check">
<input class="form-check-input" type="radio" name="questionRMG1" id="questionRMG1_3" value="answerRMG1_3" [(ngModel)]="valueSelection"
(click)="onSelect(0)">
<label class="form-check-label" for="questionRMG1_3">Answer c. </label>
</div>
<br>
<div class="form-check">
<input class="form-check-input" type="radio" name="questionRMG1" id="questionRMG1_4" value="answerRMG1_4" [(ngModel)]="valueSelection"
(click)="onSelect(0,50)">
<label class="form-check-label" for="questionRMG1_4">Answer d. </label>
</div>
<p>
<ngb-progressbar [value]="0" type="info">0%</ngb-progressbar>
</p>
</div>
Here is the code of the (click) that works fine:
<div class="container">
<div class="row">
<div class="col">
<h4>Text</h4>
<br>
<form #mailForm="ngForm">
<div class="form-group">
<label for="exampleInputName1">Name</label>
<input type="text" class="form-control" id="exampleInputName1" [(ngModel)]="inputName" name="inputName" placeholder="nombre empresa">
</div>
<div class="form-group">
<label for="exampleInputEmail1">Mail</label>
<input type="email" class="form-control" id="exampleInputEmail1" [(ngModel)]="inputEmail" name="inputEmail" aria-describedby="emailHelp"
placeholder="mail">
<small id="emailHelp" class="form-text text-muted">Text</small>
</div>
<div class="form-group form-check">
<input type="checkbox" class="form-check-input" id="exampleCheck1" [(ngModel)]="inputCheck" name="inputCheck">
<label class="form-check-label" for="exampleCheck1">Check me out</label>
</div>
<button type="submit" class="btn btn-primary" (click)="onSend()" ng-disabled="sending">Enviar</button>
</form>
</div>
</div>
</div>
I really have no idea of what the problem could be.
After more research, i found a similar question: Angular2 radio button not working in Production
I tried changing the environment production variable to false, and now everything works on Safari.
It´s a solution, but i don´t understand why this happens, doesn´t make sense.

How to save data from HTML form on mongodb using radio buttons and checkboxes

I have an HTML form and I need to save the inserted data on a mongo database. What I have difficulties is to save data inserted by radio button and ckeckboxes.
Here is my HTML code:
<div class="form-group">
<label>Foto:</label>
<input class="form-control" type="text" name="animal[foto]" placeholder="image url">
</div>
<div class="form-group">
<label>Animal:</label>
<div class="form-check">
<label class="form-check-label"><input class="form-check-input" name="animal[tipo]" type="radio" value="dog" > Dog</label>
</div>
<div class="form-check">
<label class="form-check-label"><input class="form-check-input" name="animal[tipo]" type="radio" value="cat"> Cat</label>
</div>
<div class="form-check">
<label class="form-check-label"><input class="form-check-input" name="animal[tipo]" type="radio" value="other"> Other:</label>
<input type="text" class="form-control" name="animal[tipo]" placeholder="Which?">
</div>
</div>
<div class="form-group">
<label>Gender:</label>
<div class="form-check">
<label class="form-check-label"><input class="form-check-input" name="animal[sexo]" type="radio" value="Male"> Male</label>
</div>
<div class="form-check">
<label class="form-check-label"><input class="form-check-input" name="animal[sexo]" type="radio" value="Female"> Female</label>
</div>
How can I save what is select in radio btn?
First you have to get the value from the radio buttons.
If you are using a javascript library like jQuery, it's very easy:
alert($('input[name=animal[tipo]]:checked').val());
This code will select the checked input with animal[tipo] name, and gets it's value. Simple isn't it?
Then you can save it as you normally do

HTML form validation - don't validate hidden text inputs

I have a form where the user has to choose between two modes, with two individual sets of inputs. Some inputs are required.
<form>
<div class="form-group">
<label>This or that?</label>
<div class="controls">
<label class="radio-inline">
<input type="radio" name="thisthat" value="this">
This
</label>
<label class="radio-inline">
<input type="radio" name="thisthat" value="that" checked>
That
</label>
</div>
</div>
<div class="this"> <!-- hidden as the page loads -->
<div class="form-group">
<input type="text" class="form-control" required="" placeholder="Some of this" />
</div>
<div class="form-group">
<input type="text" class="form-control" required="" placeholder="Some more of this" />
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="This again" />
</div>
</div>
<div class="that">
<div class="form-group">
<input type="text" class="form-control" required="" placeholder="Some of that" />
</div>
<div class="form-group">
<input type="text" class="form-control" required="" placeholder="Some more of that" />
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="That again" />
</div>
</div>
<button class="btn btn-info" type="submit">Submit</button>
</form>
Example on CodePen
When the user fills out one part of the form the submit is prevented because of the empty fields in the other part.
How can I disable the validation for the fields in the hidden part? Preferably without removing the required properties of the hidden inputs.
Is there a default way to do this?