form.valid on Angular gets false after the 1st submit - html

I am working on Angular 7 and having the following situation:
.html code (updated):
<form #form="ngForm" (ngSubmit)="addRecord(form)">
<div class="modal-header">
<h5 class="modal-title" id="modalRecordLabel">Add Record</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close" (click)="resetForm(form)">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<small style="margin-top: 10px;">All fields with asterisk (*) are required.</small>
<div class="container-fluid">
<div class="row">
<div class="col-md-10">
<!-- Name -->
<div class="form-group">
<label for="input1">Name *</label>
<input
type="text"
class="form-control"
id="input1"
name="input1"
[(ngModel)]="myObject.description"
required
#input1=ngModel
[class.field-error]="form.submitted && input1.invalid"
tabindex="11">
<div [hidden]="!form.submitted || input1.valid " class="alert alert-danger">
Name is required.
</div>
</div>
<!-- Date 1-->
<div class="form-group">
<label for="date1">Date 1 *</label>
<div class="input-group">
<input
class="form-control"
placeholder="yyyy-mm-dd"
id="date1"
name="date1"
[ngModel]="date1"
required
#date1=ngModel
[class.field-error]="form.submitted && date1.invalid"
ngbDatepicker #da="ngbDatepicker"
tabindex="12">
<div class="input-group-append">
<button class="btn btn-outline-secondary calendar" (click)="da.toggle()" type="button"></button>
</div>
</div>
<div [hidden]="!form.submitted || date1.valid " class="alert alert-danger">
Date 1 is required.
</div>
</div>
<!-- Date 2 -->
<div class="form-group">
<label for="date2">Date 2 *</label>
<div class="input-group">
<input
class="form-control"
placeholder="yyyy-mm-dd"
id="date2"
name="date2"
[ngModel]="date2"
required
#date2=ngModel
[class.field-error]="form.submitted && date2.invalid"
ngbDatepicker #da2="ngbDatepicker"
tabindex="13">
<div class="input-group-append">
<button class="btn btn-outline-secondary calendar" (click)="da2.toggle()" type="button"></button>
</div>
</div>
<div [hidden]="!form.submitted || date2.valid " class="alert alert-danger">
Date 2 is required.
</div>
</div>
<!--Date 3-->
<div class="form-group">
<label for="date3">Date 3</label>
<div class="input-group">
<input
class="form-control"
placeholder="yyyy-mm-dd"
id="date3"
name="date3"
[ngModel]="date3"
ngbDatepicker #da3="ngbDatepicker"
tabindex="14">
<div class="input-group-append">
<button class="btn btn-outline-secondary calendar" (click)="da3.toggle()" type="button"></button>
</div>
</div>
</div>
<!--Date 4-->
<div class="form-group">
<label for="date4">Date 4</label>
<div class="input-group">
<input
class="form-control"
placeholder="yyyy-mm-dd"
id="date4"
name="date4"
[ngModel]="date4"
ngbDatepicker #da4="ngbDatepicker"
tabindex="15">
<div class="input-group-append">
<button class="btn btn-outline-secondary calendar" (click)="da4.toggle()" type="button"></button>
</div>
</div>
</div>
<!-- Type -->
<div class="form-group">
<label for="input2">Type</label>
<div class="input-group">
<select
class="form-control"
id="input2"
name="input2"
[(ngModel)]="myObject.type.id"
(change)="selectOption($event.target.value)"
#input2="ngModel"
tabindex="16">
<option *ngFor='let input2 of input2List' [value]="input2.id">{{input2.description}}</option>
</select>
</div>
</div>
</div>
</div>
</div> <!-- closing div for class="container-fluid"-->
</div> <!-- closing div for class="modal-body"-->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal" (click)="resetForm(form)" tabindex="101">Close</button>
<button type="submit" class="btn btn-primary" tabindex="102">Add</button>
</div>
</form>
.ts code:
addRecord(form: NgForm): void {
if(form.valid) {
//do something
}
}
The first time the form is submitted, all the data is saved successfully. However, if I tried to submit a second record using the same form, now form.valid is always false.
So my question is, why is form.valid always false after the first submit?

Related

Having problems with Vue and images on my modal

I have cards that shows the name, description,price and an image, this works but when I press more info I basiclly want to show the same stuff but with more info but the modal doesn't want to show the image, pretty new to vue and such so I have no clue what's wrong:
<div class="container" id="app">
<div class="row justify-content-center mt-5">
<div class="card mr-5 mb-5" v-for="product in products" v-bind:key="product.id">
<div class="card-body">
<img v-bind:src="product.image" style="height:15rem">
<h5 class="card-title">{{ product.name }}</h5>
<p class="card-text">{{ product.description }}</p>
<p class="card-text"><b>Price: {{ product.price }}</b></p>
<button type="button" class="btn btn-primary" data-toggle="modal"
v-on:click="showProductInUpdateForm(product.id)">More Info</button>
</div>
</div>
</div>
<div class="modal fade" id="updateModal">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">More Info</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form id="formUpdate">
<img v-bind:src="product.image">
<div class="form-group">
<label for="Id">ID</label>
<input type="text" class="form-control" name="updateId" id="updateId" readonly>
</div>
<div class="form-group">
<label for="Name">Name</label>
<input type="text" class="form-control" name="updateName" id="updateName" readonly>
</div>
<div class="form-group">
<label for="Description">Description</label>
<input type="text" class="form-control" name="updateDescription" id="updateDescription" readonly>
</div>
<div class="form-group">
<label for="Price">Price</label>
<input type="text" class="form-control" name="updatePrice" id="updatePrice" readonly>
</div>
<div class="form-group">
<label for="Image">Image</label>
<input type="text" class="form-control" name="updateImage" id="updateImage" readonly>
</div>
<div class="form-group">
<button type="submit" class="btn btn-danger float-right" data-dismiss="modal">Close</button>
<button type="button" class="add-to-cart btn btn-primary float-right" data-dismiss="modal">Add to cart</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
You are declaring variable product in a v-for loop. So product can be only accessed inside that loop block.
<div v-for="product in products" v-bind:key="product.id">
{{product}}
</div>
...
<!-- product is 'undefined' here since it's outside of the v-for loop block -->
<img v-bind:src="product.image">
You can store selected item in some global property that can be accessed from everywhere:
<button v-on:click="showProductInUpdateForm(product)">More Info</button>
...
<img v-bind:src="selectedItem.image">
data {
return {
selectedItem: {}
}
},
methods: {
showProductInUpdateForm (product) {
this.selectedItem = product
...
}
}

Laravel: How To Print Ticket Using Laravel After Adding Data?

Good day pips. I am struggling on printing after I click create.
the ui for creating and printing
When I click the create button, it will add to the database and print a number or ticket. All I know is dompdf but that one is downloading pdf
<div class="modal fade" id="callModal" tabindex="-1" role="dialog" aria-labelledby="callModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="callModalLabel">New Call</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<!--start of the form-->
<form id="form-data" class="form-horizontal" method="POST" action="{{ route('call.store') }}">
{{ csrf_field() }}
<div class="row form-group">
<div class="col-md-12">
<input type="hidden" class="form-control" id="trans-id" name="trans_id" readonly>
</div>
<div class="col-md-12">
<label>Transaction Type:</label>
<input type="text" class="form-control" id="trans-name" readonly>
</div>
<div class="col-md-12">
<input type="hidden" id="called" name="called" value="NO">
</div>
<div class="col-md-12">
<label>Department:</label>
<select class="form-control" id="dept_id" name="dept_id" required>
<option value="" data-hidden="true"
selected="selected">-- Select Department --
</option>
#foreach($departments as $department)
<option value= "{{ $department->id }}">
{{ $department->dept_name }}
</option>
#endforeach
</select>
</div>
<div class="col-md-12">
<label>RFID</label>
<input type="text" id="rfid" class="form-control" name="rfid" required>
</div>
<div class="col-md-12">
<label>Student First Name</label>
<input type="text" id="firstname" class="form-control" name="firstname" required="">
</div>
<div class="col-md-12">
<label>Student Last Name</label>
<input type="text" id="lastname" class="form-control" name="lastname" required>
</div>
<div class="col-md-12">
<label>Amount</label>
<input type="number" id="amount" class="form-control" name="amount" step=".01" required>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-success btn-fill pull-right" id="form-button-add" name="form-button-add" onclick="window.print()">
CREATE <!--CREATE, SAVE AND PRINT THE TICKET-->
</button>
<button data-dismiss="modal" aria-hidden="true" class="btn btn-basic pull-right" style="margin-right: 2%">
CANCEL
</button>
<div class="clearfix"></div>
</form>
<!-- end of the form -->
</div>
</div>
</div>
</div>
I already code the javascript onclick in my create button. My problem is it prints the current page of the ui. I just want to print the details such as C-1, counter 1. Another problem is that is show the preview but it doesn't add on the database.

How could I prevent inputs from shuffling to a wrong location when showing info boxes using Bootstrap?

I am trying to create a form which should show an alert:
When hitting the button control next to the input control, it should show an alert. The alert appears correctly, however, the input controls following after shuffle to a wrong location:
I would like to see the following happen:
How could I prevent the shuffling of input controls on the left to the right side? Keep in mind that in the solution I would like the order of input controls to be the same when resizing to smaller screen size.
This is what I tried so far in code:
function toggleInfo(ele) {
if (ele.style.display === "none") {
ele.style.display = "block";
} else {
ele.style.display = "none";
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="col-md-6">
<div class="form-group">
<label>One</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default" onclick="toggleInfo(foo_id)">
-
</button>
</div>
</div>
</div>
<div class="alert alert-info" style="display: none;" id="foo_id">
<strong>Info: </strong> Message.
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Two</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Three</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Four</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Five</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
I tried wrapping up every two col-md-6 elements in a <div> element to see if these would keep sticking together. This was not the case:
function toggleInfo(ele) {
if (ele.style.display === "none") {
ele.style.display = "block";
} else {
ele.style.display = "none";
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div><!-- Not working -->
<div class="col-md-6">
<div class="form-group">
<label>One</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default" onclick="toggleInfo(foo_id)">
-
</button>
</div>
</div>
</div>
<div class="alert alert-info" style="display: none;" id="foo_id">
<strong>Info: </strong> Message.
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Two</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
</div><!-- End of change I tried -->
<div class="col-md-6">
<div class="form-group">
<label>Three</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Four</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Five</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
What could I do to fix this problem?
You are missing row class and also you required to update your DOM check snippet.
function toggleInfo(ele) {
if (ele.style.display === "none") {
ele.style.display = "block";
} else {
ele.style.display = "none";
}
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
<div class="row">
<!-- Not working -->
<div class="col-md-6">
<div class="form-group">
<label>One</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default" onclick="toggleInfo(foo_id)">
-
</button>
</div>
</div>
</div>
<div class="alert alert-info" style="display: none;" id="foo_id">
<strong>Info: </strong> Message.
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Two</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
</div>
<!-- End of change I tried -->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Three</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Four</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Five</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
</div>
u need to wrap 2 col-md-6 divs in a row like this:
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>One</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default" onclick="toggleInfo(foo_id)">
-
</button>
</div>
</div>
</div>
<div class="alert alert-info" style="display: none;" id="foo_id">
<strong>Info: </strong> Message.
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Two</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
You should place <div class="container-fluid"><div class="row" /></div></div> as parent to your child <div class="col-md-6" />
The important part is <div class="row"> /*child col */</div>
See sample snippets below:
<div class="container-fluid">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>One</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default" onclick="toggleInfo(foo_id)">
-
</button>
</div>
</div>
</div>
<div class="alert alert-info" style="display: none;" id="foo_id">
<strong>Info: </strong> Message.
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<label>Two</label>
<div class="input-group">
<input type="text" class="form-control" />
<div class="input-group-btn">
<button type="button" class="btn btn-default">
-
</button>
</div>
</div>
</div>
</div>
</div>
Tweak here: https://codepen.io/kenneth-bolico/pen/oNvmdRo

Element with name not being sent with form data via POST

I am trying to submit my modal title as post data, however it isn't being sent.
I have given the element a name attribute. Elements in the modal body are submitting as I would expect. Trying to submit the element whose name is "editModalTitle" below
HTML:
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<form action="/editItem" method="post" id ="editModalForm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" name="eModalTitle"></h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Quantity:</label>
<div class="row">
<div class="col">
<select class="form-control" id="qtSelect">
<option>Set to</option>
<option>Add</option>
<option>Subtract</option>
</select>
</div>
<div class="col" id= "qtD">
<input type="text" class="form-control" id="qt" name="quantityTB" placeholder="Quantity">
</div>
</div>
</div>
<div class="form-group" id="prD">
<label>Price:</label>
<input type="text" class="form-control" id="pr" name="priceTB" placeholder="Enter new price here...">
</div>
<div class="form-group">
<label>Notes:</label>
<div class="row">
<div class="col">
<select class="form-control" id="ntSelect">
<option>Set notes to:</option>
<option>Add to notes:</option>
</select>
</div>
<div class="col">
<input type="text" class="form-control" id="ntInput" name="notesTB"placeholder="Enter notes here...">
</div>
</div>
</div>
<div class="form-group">
<label>Location:</label>
<input type="text" class="form-control" id="loc" name="locationTB" placeholder="Enter new location here...">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" id="eSvBtn" class="btn btn-primary">Save changes</button>
</div>
</form>
</div>
</div>
</div>
</div>
Form data here
Edit 1: Added full html for modal.
<div class="modal fade" id="editModal" tabindex="-1" role="dialog" aria-labelledby="editModal" aria-hidden="true">
<div class="modal-dialog" role="document">
<form action="/editItem" method="post" id ="editModalForm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" name="eModalTitle"></h5>
<!-- hidden value for modal title to post -->
<input type="hidden" id="Modal_title_to_post" name="Modal_title_to_post" value="eModalTitle">
<!-- hidden value for modal title to post -->
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group">
<label>Quantity:</label>
<div class="row">
<div class="col">
<select class="form-control" id="qtSelect">
<option>Set to</option>
<option>Add</option>
<option>Subtract</option>
</select>
</div>
<div class="col" id= "qtD">
<input type="text" class="form-control" id="qt" name="quantityTB" placeholder="Quantity">
</div>
</div>
</div>
<div class="form-group" id="prD">
<label>Price:</label>
<input type="text" class="form-control" id="pr" name="priceTB" placeholder="Enter new price here...">
</div>
<div class="form-group">
<label>Notes:</label>
<div class="row">
<div class="col">
<select class="form-control" id="ntSelect">
<option>Set notes to:</option>
<option>Add to notes:</option>
</select>
</div>
<div class="col">
<input type="text" class="form-control" id="ntInput" name="notesTB"placeholder="Enter notes here...">
</div>
</div>
</div>
<div class="form-group">
<label>Location:</label>
<input type="text" class="form-control" id="loc" name="locationTB" placeholder="Enter new location here...">
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="submit" id="eSvBtn" class="btn btn-primary">Save changes</button>
</div>
</div>
</form>
</div>
</div>
Updated code with hidden value for the modal title :)

Post sent with no parameters

I have got the following HTML code. I have no idea why the POST is sent with no parameters (I checked the parameters are not being sent with the firefox debugger):
<div class="modal fade"id="myModal"tabindex="-1"role="dialog"aria-labelledby="myModalLabel"aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button"class="close"data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title"id="myModalLabel">Add opinion</h4>
</div>
<form role="form" action="api/sendEntry" method="post">
<div class="modal-body">
<div class="form-group">
<label for="user_message">The following: </label>
<input type="text" class="form-control" id="user_message" placeholder="In next...">
<label for="user_date">Until: </label>
<input type="date" class="form-control" id="user_date">
</div>
</div>
<div class="modal-footer">
<button type="submit"class="btn btn-default">Submit</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close </button>
</div>
</form>
</div>
</div>
</div>
I suspect the nested input fields may have something to do with it, but I need to have them nested to have proper formatting.
You need to assign a name to your input fields. For example:
<input type="text" class="form-control"
id="user_message" name="user_message" placeholder="In next..." />
Form data is posted as name/value pairs. If you don't provide name attributes, nothing will be posted.
As a side note, I would also recommend you to correctly close your input tags.
<div class="modal fade"id="myModal"tabindex="-1"role="dialog"aria-labelledby="myModalLabel"aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button"class="close"data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title"id="myModalLabel">Add opinion</h4>
</div>
<form role="form" action="api/sendEntry" method="post">
<div class="modal-body">
<div class="form-group">
<label for="user_message">The following: </label>
<input type="text" class="form-control" id="user_message" placeholder="In next..." name="user_message">
<label for="user_date">Until: </label>
<input type="date" class="form-control" id="user_date" name="user_date">
</div>
</div>
<div class="modal-footer">
<button type="submit"class="btn btn-default">Submit</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Close </button>
</div>
</form>
</div>
</div>
Receive your data with $_POST['user_date'] and $_POST['user_message']