<form id="form_Element" class="custom-form">
<div>
<div class="form-row">
<div class="form-group col-md-6">
<div class="e-float-input e-control-wrapper">
<input asp-for="OrderID" type='text' />
<label asp-for="OrderID" class="e-float-text e-label-top">Order ID</label>
</div>
</div>
<div class="form-group col-md-6">
<div class="e-float-input e-control-wrapper">
<input asp-for="CustomerID" />
<label asp-for="CustomerID" class="e-float-text e-label-top">Customer Name</label>
</div>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-12">
<ejs-textbox placeholder="Enter address" id="textbox"></ejs-textbox>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-2">
<ejs-button id="add" content="Add record" type="button"></ejs-button>
</div>
</div>
</div>
</form> |
// Grid’s created event function
function OnCreated() {
var formElement = document.getElementById("form_Element");
// Grid columns validation rules are passed to validationRules method
var rules = validationRules(this.columns);
// Form Validator control is initialized on the form with the returned rules
new ej.inputs.FormValidator(formElement, rules);
}
function validationRules(cols) {
// Columns validation rules are calculated based on the grid column valudationRules assigned
var rules = [];
var count = 0;
while (count < cols.length) {
var fieldName = cols[count].field;
var validR = cols[count].validationRules;
rules[fieldName] = (validR !== undefined) ? validR : null;
count++;
}
return rules;
} |
document.getElementById('add').addEventListener('click', function (args) {
var formObj = this.closest('.custom-form').ej2_instances[0];
// Form is validated
if (formObj.validate()) {
var formElement = document.getElementById("form_Element");
var grid = document.getElementById("Grid").ej2_instances[0];
// Values from the form's input elements are stored in a separate variable
var orderIDVal = parseInt(formElement.querySelector("#OrderID").value);
var customerIDVal = formElement.querySelector("#CustomerID").value;
var addressVal = formElement.querySelector("#textbox").value;
// A new object is formed with these new values
var newData = { "OrderID": orderIDVal, "CustomerID": customerIDVal, "ShipAddress": addressVal }
// This new object is passed to the grid’s addRecord method
grid.addRecord(newData);
}
}); |