
Security News
Browserslist-rs Gets Major Refactor, Cutting Binary Size by Over 1MB
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
angular-conditional-validation
Advanced tools
AngularJS module that enables conditional validation
The conditional validation module provides an enableValidation
attribute directive that can be used to conditionally enable/disable validation for elements with an ngModel
directive.
Most (if not all) of the standard Angular validators can be disabled individually using falsy values.
For example when the expression for a ngMin
attribute evaluates to undefined
, then validation will be disabled for the min
validator.
However, this requires that the logic that determines when validation should be enabled is combined with the logic that defines the values for parameterized validators.
At best you can write something like this:
<input
type="date"
ng-model="myCtrl.startDate"
ng-min="myCtrl.enableStartDateValidation && myCtrl.minStartDate">
The enableValidation
directive provides a cleaner method to separate the logic.
When applied to the example above, the result looks like this:
<input type="date"
ng-model="myCtrl.startDate"
ng-min="myCtrl.minStartDate"
enable-validation="myCtrl.enableStartDateValidation">
Using the enableValidation
directive it also easier to disable multiple validators at once.
If the example above is extended with a max
validator, you can simply add the ng-max
attribute without having to repeat myCtrl.enableStartDateValidation &&
in the validator expression.
The result:
<input type="date"
ng-model="myCtrl.startDate"
ng-min="myCtrl.minStartDate"
ng-max="myCtrl.maxStartDate"
enable-validation="myCtrl.enableStartDateValidation">
Another reason for using the enableValidation
directive is when you use (custom) validators that do not provide a method to (conditionally) disable them.
NPM
npm install angular-conditional-validation
Bower
bower install angular-conditional-validation
To use the Angular conditional validation module in your application either include the script (angular-conditional-validation(.min).js
) using a <script>
tag or require/load it via a script loader.
Furthermore add angularConditionalValidation
to the Angular dependencies of your application.
Now you should be able to use the enableValidation
directive in your templates.
The directive supports three types of values:
Both synchronous and asynchronous validators are supported by the enableValidation
directive.
A set of demos can be found at the following JS Bin: jsbin.com/necile
var ctrl = this;
ctrl.someValue = 'hello';
ctrl.anotherValue = true;
<input type="text" ng-model="ctrl.someValue" ng-minlength="5" enable-validation="ctrl.anotherValue">
var ctrl = this;
ctrl.someValue = 'hello';
ctrl.shouldValidate = function() {
return new Date().getDay() == 1; // Only validate on mondays :)
};
<input type="text" ng-model="ctrl.someValue" ng-minlength="5" enable-validation="ctrl.shouldValidate">
var ctrl = this;
ctrl.someValue = 'hello';
ctrl.enabledValidators = {
required: false,
minlength: true,
'*': false // Disable other validators.
};
<input
type="text"
ng-model="ctrl.someValue"
required
ng-minlength="4"
ng-maxlength="6"
pattern="abcde"
enable-validation="ctrl.enabledValidators">
var ctrl = this;
ctrl.someValue = 'hello';
ctrl.enabledValidators = function() {
return {
required: false,
minlength: true,
'*': function() {
return new Date().getDay() == 2; // Other validators are only enabled on tuesday.
}
};
};
<input
type="text"
ng-model="ctrl.someValue"
required
ng-minlength="4"
ng-maxlength="6"
pattern="abcde"
enable-validation="ctrl.enabledValidators">
FAQs
AngularJS module that enables conditional validation
The npm package angular-conditional-validation receives a total of 9 weekly downloads. As such, angular-conditional-validation popularity was classified as not popular.
We found that angular-conditional-validation demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Browserslist-rs now uses static data to reduce binary size by over 1MB, improving memory use and performance for Rust-based frontend tools.
Research
Security News
Eight new malicious Firefox extensions impersonate games, steal OAuth tokens, hijack sessions, and exploit browser permissions to spy on users.
Security News
The official Go SDK for the Model Context Protocol is in development, with a stable, production-ready release expected by August 2025.