Git Product home page Git Product logo

ui-validate's Introduction

ui-validate Build Status npm version Bower version Join the chat at https://gitter.im/angular-ui/ui-validate

General-purpose validator for ngModel.

Angular.js comes with several built-in validation mechanism for input fields (ngRequired, ngPattern etc.) but using an arbitrary validation function requires creation of a custom formatters and / or parsers. The ui-validate directive makes it easy to use any function(s) defined in scope as a validator function(s). A validator function will trigger validation on both model and input changes.

Requirements

  • AngularJS

Usage

You can get it from Bower

bower install angular-ui-validate

Load the script files in your application:

<script type="text/javascript" src="bower_components/angular/angular.js"></script>
<script type="text/javascript" src="bower_components/angular-ui-validate/dist/validate.js"></script>

Add the specific module to your dependencies:

angular.module('myApp', ['ui.validate', ...])

Or using npm, webpack and es6 import

npm install --save angular-ui-validate

Don't add script tags in your html page. instead

import uiValidate from 'angular-ui-validate'

angular.module('myApp', [uiValidate, ...])

Development

We use Karma and jshint to ensure the quality of the code. The easiest way to run these checks is to use grunt:

npm install -g gulp-cli
npm install && bower install
gulp

The karma task will try to open Firefox and Chrome as browser in which to run the tests. Make sure this is available or change the configuration in karma.conf.js

Gulp watch

gulp watch will automatically test your code and build a release whenever source files change.

How to release

Use gulp to bump version, build and create a tag. Then push to GitHub:

gulp release [--patch|--minor|--major]
git push --tags origin master # push everything to GitHub

Travis will take care of testing and publishing to npm's registry (bower will pick up the change automatically). Finally create a release on GitHub from the tag created by Travis.

ui-validate's People

Contributors

0x-r4bbit avatar aidanhs avatar ajoslin avatar asadighi avatar caiotoon avatar dandoyon avatar dhilt avatar doogd avatar douglasduteil avatar edwardhotchkiss avatar ejmarino avatar jimallanson avatar joshkurz avatar just-boris avatar mfeingold avatar nateredding avatar petebacondarwin avatar peterdavehello avatar pkozlowski-opensource avatar pocesar avatar powerkiki avatar proloser avatar r0b- avatar realtica avatar shaungrady avatar solidspark avatar teamon avatar thetrevdev avatar vinniefranco avatar ziv avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

ui-validate's Issues

Trigger validation through events such as clicks?

The docs state: A validator function will trigger validation on both model and input changes.

Is there a way to trigger the validation through another type of event, such as a click on a button?

Value Undefined

I've written string validation functions which for some reason generate errors, saying that there is no property length of the argument value because it is undefined. After setting a condition breakpoint, it appears that the validators are calledbefore the form inputs are loaded in the DOM at which point they are undefined. Does anyone have any ideas why this is or how to correct it?

image

Support deep watching

$watch has a 3rd parameter, and when set to true it watches the entire object. It would be useful to be able to do i.e. ui-validate-watch-deep="..."

invalid value is bound to model

When I my validation expression returns false, the invalid value is bound to the model, anyway. This isn't how any of the pre-canned or custom validators work. Why does it behave this way?

Validator function setting multiple types of errors

I need to have one function that validates a field that can have multiple error types. Splitting this validator to multiple separate functions is not practical due to the amount of code required to parse this input. I have created a my own variation of the uiValidate directive with instead of:

        ctrl.$validators[key] = validateFn;

having this:

        ctrl.$validators[key] = function(value) {
          if(ctrl._validatedKey) {
            ctrl.$setValidity(ctrl._validatedKey, true);
            delete ctrl._validatedKey;
          }

          var retVal = validateFn(value);
          if(angular.isString(retVal)) {
            ctrl.$setValidity(retVal, false);
            ctrl._validatedKey = retVal;
            return false;
          }
          return retVal;
        };

So basically in a validator function instead of returning return false; I can do return "errorFoo" or return "errorBar" depending on the error.

Is such a feature welcome in this library, should I bother with a pull request or keep my changes local?

ui-validate makes model undefined

ui-validate makes the model undefined after change for some reason. My college has the same issue in a completely different situation. any idea why this might happen?

Feature Request: Using $watchCollection from ui-validate-watch-collection

I used the ui-validate-watch directive on my element hoping that the source code would be smart enough to handle arrays. Sadly it didn't, not as I expected at least.

I dag into the source code here https://github.com/angular-ui/ui-validate/blob/master/src/validate.js#L139 and found that only each item in the array is being watched, and not the the array itself.

I can only assume that the use case for this implementation was something along the lines of ui-validate-watch="['someValueModel', 'someOtherValueModel']"

My use case is actually watching an entire array, perhaps something along the lines of ui-validate-watch-collection="'myArray'" .

If this is something that could be added to ui-validate, I could try and create a PR.
If I am misusing the module, could you please explain how to achieve my goal?

CDN

Is ui-validate available on any cdn (except rawgit)?

demo in html preview

Forgive me if this isn't the best place to post this.

The demo at the url linked from this page https://angular-ui.github.io/ (it links to an htmlpreview page [http://htmlpreview.github.io/?https://github.com/angular-ui/ui-validate/master/demo/index.html]) does not work. I'm not sure why. I cloned it to try to fix the issue, but the demo DOES work. Just not in that htmlpreview.github link.

Having the demo seem broken will discourage people from using the module. Which I think is a disservice with how great the code is. Thanks!

After second trigger of the validation the model binding value is undefined

I'm using a directive for an input text control where i have the following two methods inside controller function.
The first time i paste some text in the text box both methods are triggered. When i type in or delete a character from the text box first is called the validation method and then the ng-change associated method (tiggeredOnTextInputChange) but the value passed (val) is undefined (ng-model="theBindingModelValue"). How can i deal with this?

        $scope.tiggeredOnTextInputChange = function (val) {
            var processedVal = val.length - 1; //"val" will be undefined the second time ngChange is triggered so this line will throw error
            $scope.theBindingModelValue = processedVal;
        };
        $scope.validateUserInput = function(value){
            var isValid = someFactoryWithoutAsyncCalls.validate(value);
            return isValid;
        }

Html of the directive (testInput.html)

<div class="form-group" data-ng-form name="myForm">
  <input type="text" name="{{inputName}}" placeholder={{inputPlaceholder}} class="form-control" data-ng-model="theBindingModelValue" data-ng-change="tiggeredOnTextInputChange(theBindingModelValue)" ui-validate="{isInputValid:'validateUserInput($value)'}"/>
  <span data-ng-show="myForm['\{\{inputName\}\}'].$error.isInputValid">Invalid input</span>
</div>

The complete directive:

function testInput() {

    function testInputCtrl($scope, someFactoryWithoutAsyncCalls) {
        $scope.tiggeredOnTextInputChange = function (val) {
            var processedVal = val.length - 1; //"val" will be undefined the second time ngChange is triggered so this line will throw error
            $scope.theBindingModelValue = processedVal;
        };
        $scope.validateUserInput = function(value){
            var isValid = someFactoryWithoutAsyncCalls.validate(value);
            return isValid;
        }
    }

    testInputCtrl.$inject = ["$scope", "someFactoryWithoutAsyncCalls"];

    return {
        scope: {
            inputName: "@",
            inputPlaceholder: "@"
        },
        restrict: "EA",
        require: "ngModel",
        replace: true,
        templateUrl: "testInput.html",
        controller: testInputCtrl
    };
}

I call it this way:

<test-input input-name="testinput" input-placeholder="TEST"></test-input>

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.