Page 1 of 1

Form Onsubmit validation

Posted: 14 Apr 2020
by vicks1985
I am trying to check if the value of the field already exists in SharePoint list. If it exists than i need to display validation message "Value already exists" or if it does not than i need to submit the form successfully. I tried to use callback function to get value from ajax success in form submit function as below. I am now getting correct message when value exists, but when it does not exist the form is not getting submitted. Please help.

if (fd.field('PONumber').value()) {
checkPoExist(fd.field('PONumber').value(), function (d) {
if (d == 0) {
fd.field('FileLeafRef').readonly(false);
fd.field('OrderAmount').readonly(false);
fd.sourceFormParam('/sites/QS/Pages/SODashboard.aspx');
return true;
}
else {
$('.poMsg').html("PO already exists");
return false;
}
});
}


function checkPoExist(poVal, callback) {
var items;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('SalesOrderPacket')/items?$select=ID,PONumber,ContentType/Name&$expand=ContentType&$filter=ContentType eq 'SalesOrderSet' and PONumber eq '" + poVal + "'",
type: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function (data) {
items = data.d.results.length;
callback(items);
},
error: function (xhr, status, error) {
//debugger;
//logerror(data.responseText, 'eventhome js', 'Popupdialog');
}
});
}

Re: Form Onsubmit validation

Posted: 14 Apr 2020
by mnikitina
Hello vicks1985,

This part of the code looks fine.

Are you getting any errors in the browser console(F12)? Could you please share the complete code to troubleshoot the issue.

Thank you!

Re: Form Onsubmit validation

Posted: 14 Apr 2020
by vicks1985
I am not seeing any error on F12 console. Below is my entire function for onsubmit, where i am checking for other required fields. I hope it helps

fd.onsubmit(function () {
if (!fd.field('FileLeafRef').value() || !fd.field('CustomerName') || fd.field('OMRep').value().length == 0
|| !fd.field('OrderValue') || !fd.field('OrderType')) {
if (!fd.field('FileLeafRef').value()) {
$('.nameCusMsg').html("You must specify a value for this required field");
}
if (!fd.field('CustomerName').value()) {
$('.custNameMsg').html("You must specify a value for this required field");
}
if (fd.field('OMRep').value().length == 0) {
$('.custOmrepMsg').html("You must specify a value for this required field");
}
if (!fd.field('OrderValue').value()) {
$('.custOrdValMsg').html("You must specify a value for this required field");
}
if (!fd.field('OrderType').value()) {
$('.custOrdTypeMsg').html("You must specify a value for this required field");
}
return false;
}
//below if code to check PCode exists
if (fd.field('PONumber').value()) {
checkPoExist(fd.field('PONumber').value(), function (d) {
if (d > 0) {
$('.poMsg').html("PO already exists");
return false;
}
});
}
//
fd.field('FileLeafRef').readonly(false);
fd.field('OrderAmount').readonly(false);
fd.sourceFormParam('/sites/QS/Pages/SODashboard.aspx');
return true;

});

Re: Form Onsubmit validation

Posted: 14 Apr 2020
by vicks1985
No error on F12 console. Below is my function for onsubmit, where I am checking for other required fields.

fd.onsubmit(function () {
if (!fd.field('FileLeafRef').value() || !fd.field('CustomerName') || fd.field('OMRep').value().length == 0
|| !fd.field('OrderValue') || !fd.field('OrderType')) {
if (!fd.field('FileLeafRef').value()) {
$('.nameCusMsg').html("You must specify a value for this required field");
}
if (!fd.field('CustomerName').value()) {
$('.custNameMsg').html("You must specify a value for this required field");
}
if (fd.field('OMRep').value().length == 0) {
$('.custOmrepMsg').html("You must specify a value for this required field");
}
if (!fd.field('OrderValue').value()) {
$('.custOrdValMsg').html("You must specify a value for this required field");
}
if (!fd.field('OrderType').value()) {
$('.custOrdTypeMsg').html("You must specify a value for this required field");
}
return false;
}
//below if code to check PCode exists
if (fd.field('PONumber').value()) {
checkPoExist(fd.field('PONumber').value(), function (d) {
if (d == 0) {
fd.field('FileLeafRef').readonly(false);
fd.field('OrderAmount').readonly(false);
fd.sourceFormParam('/sites/QS/Pages/SODashboard.aspx');
return true;
}
else {
$('.poMsg').html("PO already exists");
return false;
}
});
}

});

Re: Form Onsubmit validation

Posted: 15 Apr 2020
by mnikitina
Hello vicks1985,

I think I've found the problem. You need to check if the record exists on field change, no when the form is submitted. You can validate the results the same way, please see the update part of the code below.

Code: Select all

function checkPoExist(poVal, callback) {
var items;
$.ajax({
url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getbytitle('SalesOrderPacket')/items?$select=ID,PONumber,ContentType/Name&$expand=ContentType&$filter=ContentType eq 'SalesOrderSet' and PONumber eq '" + poVal + "'",
type: "GET",
contentType: "application/json;odata=verbose",
headers: {
"Accept": "application/json; odata=verbose",
"X-RequestDigest": $("#__REQUESTDIGEST").val(),
},
success: function (data) {
items = data.d.results.length;
callback(items);
},
error: function (xhr, status, error) {
//debugger;
//logerror(data.responseText, 'eventhome js', 'Popupdialog');
}
});
}

fd.field('PONumber').change(function() {
if (fd.field('PONumber').value()) {         
checkPoExist(fd.field('Title').value(), function (d) {
if (d == 0) {
fd.field('FileLeafRef').readonly(false);
fd.field('OrderAmount').readonly(false);
fd.sourceFormParam('/sites/QS/Pages/SODashboard.aspx');
record = 'new';
}
else {
$('.poMsg').html("PO already exists");
record = 'exist';
}
});
}
});

fd.onsubmit(function () {
            if (record == 'new') {
            //save if PCode doesn't exists
            return true;
            }
            return false;
});


Re: Form Onsubmit validation

Posted: 17 Apr 2020
by vicks1985
Thank you mnikitina. It worked