I have to design a classic, form based operational web app.
Each form contains some controls, mostly input controls. Many of those controls have validation or behavioral rules, some rules are valid only for a single control (jndependent checks) and some depend on the values of other controls (dependent checks).
Moreover, some controls have the same semantic meaning across different forms.
For example, the "customer name" input field should always have a maximum length of 50 chars and it is shared between many different forms. Another example is that the "documents" combobox is filtered based on the age field (ie: a customer with less than 18 years have different documents). If there are zero documents in that combo, then it should completely disappear.
The rules should be centralized and reused. Even though I can define the rules in functions contained in controllers, I don't want to have the programmer to remember to add a validation rule for a certain field, because I'm sure he won't.
What I am after is an AngularJS-based smart way to define reusable rules for the fields for all the forms, and a way to enforce the programmers to automatically use those rules in the fields. Ideally the rules should be defined on the server and downloaded when needed, because I'll have to redo the check at the server side, for obvious security reasons.
I've looked at angular custom directives, but I'm not sure this is the right way to implement such a thing. It surely works but I'd like to know how to define cross-field rules and enforce their use.
For example, using a custom directive myCustomerName for the customer field name:
app.js var myApp = angular.module("MyApp", []);
myApp.directive("myCustomerName",
function()
{
return
{
restrict: 'E',
templateUrl: 'customer_name.html'
};
}
);
customer_name.html
<div class="form-group">
<label for="customerName">Customer Name</label>
<input type="text" class="form-control" id="customerName" ng-model="customerName">
</div>
etc. but then I don't have cross-check rules!
I could instead define a more general custom directive:
myApp.directive("ufeCheck",
function()
{
return {
restrict: 'E',
templateUrl: function(e, attr) {
return attr.type + '.html';
}
};
}
);
And then use it in the html like:
<ufe_check type="customer_name"></ufe_check>
<ufe_check type="customer_age"></ufe_check>
But, Where would I put the cross field checking? How should I download the server-defined rules?
I think I need a rules engine executor, client side and AngularJS forms-validation compliant.
I've looked at Valdr: https://github.com/netceteragroup/valdr And it's good but the rules are defined only in the client and are not cross field.