I am currently working on a basic CRUD web application that uses the Serenity template (Serene) in Visual Studio. This template provides access to multiple plugins. The Sergen plugin generates c# and html files that create a CRUD table for a specified database. The jQuery validate plugin is used to modify clientside field validation. I am trying to add custom field validation to the frequency input field by adding the following combination of jQuery and regex (the commented section) to the TableIndex.cshtml file (generated by Sergen):
{
ViewData["Title"] = Serenity.LocalText.Get("Db.Default.Table.EntityPlural");
}
<div id="GridDiv"></div>
<script type="text/javascript">
jQuery(function () {
new SerenityNowSolution.Default.TableGrid($('#GridDiv'), {}).init();
Q.initFullHeightGridPage($('#GridDiv'));
});
//The following is code that I added:
$(document).ready(function () {
$.validator.addMethod("TableDialog1_Frequency", function (value, element) {
return this.optional(element) || /(\d)+(d|h|m|s)/.test(value);
}, "Frequency is invalid: Please enter a valid frequency.");
$("#TableDialog1_Form").validate({
rules: {
TableDialog1_Frequency: "required TableDialog1_Frequency",
},
});
});
</script>
As far as I can tell, this is the only cshtml file generated by Sergen. My attempts at adding custom field validation are running into a few issues:
I am unsure about the placement of my added code (if this is the correct file to edit).
My added code currently tries to add validation to the frequency input field by adding a regex rule (method) to the validator function in the validate plugin. Unfortunately, the Sergen generated code "auto-creates" (for lack of a better description) the add-row modal (id = TableDialog#_Form) that contains the frequency input field (id = TableDialog#_Frequency). In my above code, I attempt to add field validation to a specific modal's input field (form/frequency #1). However, every time that the add-row button is clicked on the web page, the appearing modal has a unique identifying number in its ids. It would appear that I need to either edit the modal generation code to remove these numbers, or modify my validate code to account for this. I am unsure of how to do either.
The following code is used to generate the add-row modals (TableDialog#_Form):
namespace SerenityNowSolution.Default {
@Serenity.Decorators.registerClass()
export class TableDialog extends Serenity.EntityDialog<TableRow, any> {
protected getFormKey() { return TableForm.formKey; }
protected getIdProperty() { return TableRow.idProperty; }
protected getLocalTextPrefix() { return TableRow.localTextPrefix; }
protected getNameProperty() { return TableRow.nameProperty; }
protected getService() { return TableService.baseUrl; }
protected form = new TableForm(this.idPrefix);
}
}
Thank you for taking the time to read this. Please let me know if I should show any additional code. Any help would be greatly appreciated.
UPDATE:
This is currently my added code (after making suggested changes):
$(document).ready(function () {
$.validator.addMethod(
"regex",
function (value, element, regexp) {
var re = new RegExp(regexp);
return this.optional(element) || re.test(value);
},
"Please check your input."
);
});
$(document).on('click', '.tool-button.save-and-close-button', function () {
var item = $("div.field.Frequency input").attr("id");
var abc = $("div.s-Form form").attr("id");
$(abc).validate();
$(item).rules("add", {
regex: "^(\d)+(d|h|m|s)$"
});
$(abc).validate();
});
$(document).on('click', '.tool-button.apply-changes-button.no-text', function () {
var item = $("div.field.Frequency input").attr("id");
var abc = $("div.s-Form form").attr("id");
$(abc).validate();
$(item).rules("add", {
regex: "^(\d)+(d|h|m|s)$"
});
$(abc).validate();
});
Unfortunately, field validation is still not occurring.