One of the key features of form validation in Angular is an error displaying. When we create a form in view template, Angular creates a controller for the form. This controller contains controls and nested forms with controls statuses (valid/invalid), errors etc.
The default behavior of controller is to set errors for controls with invalid values immediately when the form is displayed. F.e. a form with some mandatory fields was loaded on a web page and these fields are empty by default, the form is waiting for user data. The controller sets errors for these mandatory fields because no value is also an invalid value. But it’s better to show these errors to user only on their actions like field editing or trying to send/save data, not at once an empty form was shown.
To display errors we use a combination of control states: $dirty && $invalid, where $dirty means control data was changed by user, $invalid indicates that the model has invalid values.
In this example, we use only one directive “required” to check if the field is filled, and
ngMessages module to display errors. However, the form displays an error only if field1 was edited by user:
ng-show="testForm.field1.$dirty". This means if a user clicks Send button right away with no data inputted to the form, there won’t be any error shown. It’s necessary to set property $dirty for the control, we use
$setDirty() method for this.
Here’s a small service to set this property:
This is how the service can be used:
formValidation.localValidateForm(scope.testForm). For convenience of use I created a directive which sets property $dirty on Submit action to all form fields and calls event handler if the form is valid:
On Send button click the testForm will show errors in case of invalid data, otherwise, action submitForm() will be called.
There are some issues with form validation in Angular 1. The entire validation system is based on rendered input fields and this leads to next disadvantages:
Below I describe validation solution with use of Angular 1. In the example I use a small library for data validation - validatejs. This library provides a declarative way to validate js objects.
The validation function returns a list of errors for invalid data and undefined for valid data.
The library has several stock validators like
URL etc, and you can add your custom one. After checking the form with validatejs we display it and call
formValidation.localValidateForm(testForm) to show errors.
Example with the same form:
The form is not displayed in this example because
ng-if condition is not satisfied. On
Check button click I call function
ctrl.checkData in the controller to validate our values, and if they are invalid the function returns a list of errors.
Programmatic generation of PDF files is a frequent task when developing applications that can export reports, bills, or questionnaires. In this article, we will consider three common tools for creating PDFs, including their installation and converting principles.
For any project there may be a need to use a database full-text search. We expect high speed and relevant results from this search. When we face such problem, we usually think about Solr, ElasticSearch, Sphinx, AWS CloudSearch, etc. But in this article we will talk about PostgreSQL. Starting from version 8.3, a full-text search support in PostgreSQL is available. Let's look at how it is implemented in the DBMS itself.
Welcome all who are reading this article. I was given a task of creating a parser (spider) with the Scrapy library and parsing FTP server with data. The parser had to find lists of files on the server and handle each file separately depending on the requirement to the parser.
When dealing with one of our projects (LookSMI media monitoring platform) we have to handle the huge volume of data – and its quantity is constantly growing. At the same time, we must run quick searches with smart rules. In this article I'll explain how we have achieved required performance.
In VIM there is a command for char search: f. After first use it can be repeated with ;. I like to navigate in line with it. You see that you need to go to bracket in a middle of a line - you press f( and one-two ; and you are there. There's no such command in Emacs, so I had to write my own. I've managed even to implement repetition with ;.