Angular get ng-model properties eg. minlength

I'm trying to make form validation in angular ( my 1st time ), and I got stucked in following moment:

I've made template with validation messages: form_messages.html

e.g. snippet from index.html

<input type="password" name="password" ng-model="password" ng-minlength="6" required> <div class="form-error" role="alert" ng-messages="form.password.$error" ng-show="form.password.$invalid && form.$submitted || form.password.$dirty"> <ng-messages ng-messages-include="/path/to/form_messages.html"></ng-messages> </div>

e.g. messages in template form_messages.html

<p class="form-error__message" ng-message when="required">Password is required.</p> <p class="form-error__message" ng-message when="minlength">Password needs to be at least 6 characters.</p>

Everything works pretty good, but I want it to be flexible for other inputs too.

What is the best way to accomplish this?

Something as following abstract snippet in form_messages.html would be wonderfull:

<p class="form-error__message" ng-message when="minlength"> {{ }} needs to be at least {{ ng-model.minlength }} characters. </p>

-------------Problems Reply------------

Actually there is a feature request for this

For getting validators you can use $error object, for example for minlength it will be something like form.$error.minlength[0].$name. But, there is no way to get exact value of this validator yet. See this issue:

You need to implement ngModel directive (code borrowed from the issue):

app.directive('ngModel', function attributeNgModelDirective() {
return {
require: 'ngModel',
link: function(scope, el, attrs, ctrl) {
ctrl.$attributes = attrs;

Then you can use it as form.$error.minlength[0].$attributes.minlength

<p class="form-error__message" ng-message when="minlength">
{{ form.$error.minlength[0].$name }} needs to be at least {{ form.$error.minlength[0].$attributes.minlength }} characters.

See the complete example:

Category:javascript Views:4 Time:2018-12-30

Related post

  • get expando model properties in python for google-app-engine 2010-05-21

    How do I get all the properties from an expando model? (not just I want to do something like this: ... recs = query.fetch( 100 ) for rec in recs: for name, value in rec.iteritem(): # figure out what, if any, expando properties are

  • How Can I get the model properties using T4 template 2011-05-03

    I am using T4Scaffolding, and tried to create a custom Scaffold template. It's not project, not MVC. My user class : public class User { public int Id {get;set;} public string Name {get; set;} } in the .cs.t4 file , I need to get the user pro

  • Get DataMapper Model Properties 2011-01-12

    Is there a way to get the properties of a model with DataMapper? For example: require 'rubygems' require 'datamapper' class User include DataMapper::Resource property :id, Serial property :name, String end Could I get the properties of User in an arr

  • Get notified when properties change in the Model 2012-03-29

    There seems to be conflicting thoughts on whether INotifyPropertyChanged should be implemented in the Model or not. I think that it should be implemented in the ViewModel, but I can't figure out how it would be accomplished. There are plenty of menti

  • UI elements from several model properties in ASP.NET MVC 2012-10-18

    I've been doing some searches, found this ASP.NET MVC 3 - Partial vs Display Template vs Editor Template but I am still not clear as what and how should I use for "custom UI controls" for several model properties. I have two examples in mind: FistNam

  • Capturing view model properties after form submit 2010-09-07

    I have a strongly typed view that is using a viewmodel I created. I have a date field in my viewmodel that is a formatted display of a property of my main model. In the POST Edit action (I followed the nerd dinner tutorial) parameters are int id, For

  • Is there a way to override Model properties without defining them all again with Kohana? 2010-10-04

    I have the following, for example: class Model_User extends ORM { protected $_rules = array( 'username' => array( 'not_empty' => NULL, 'min_length' => array(6), 'max_length' => array(250), 'regex' => array('/^[-\pL\[email protected]]++$/uD'), ), '

  • MVVM - Bind directly to Model object exposed from VM or implement a separate property in VM to access the Model properties 2011-04-27

    Sorry if the title is confusing but I don't really know how to shorten my question. Anyway, here it goes. I'm using WPF, Entity Framework and MVVM. Currently, in my ViewModel, I have a property public Model.Document Document {get;set;} //Model.Docume

  • Binding Model properties directly in View 2011-04-27

    I've found this text in Prism documentation. I'm starting on MVVM and I'm at lost. Can (should) I bind model properties in the view or I must create a viewmodel with a proxy property for every property in the model? The model classes typically provid

  • Loop through T model properties. Building a search 2011-04-28

    I'm about to build a search page on a website and the search is going to be extensive with a couple of models and with each model having a lot of properties. Is there a way to do this in a generic way or use reflector as I have seen in some posts? I

  • MVC Razor How to get the model in the Controller on HttpPost when the model is dynamic 2011-08-12

    I'm working a feature in the application where model will be dynamic in the sense that any settings data could be displayed and the view will get the model based on what tab they clicked on. I use Hidden field to store what the settings name was beca

  • How to bind URL parameters to model properties with different names 2011-12-14

    Okay, lets say I have a URL like so, which is mapped via HTTP verb GET to the controller action I have below: GET /foo/bar?sort=asc&r=true How can I bind this to my model Bar on my controller action I have below: class Bar { string SortOrder { ge

  • ASP MVC 3, how to set model properties in controller? 2011-12-16

    I've got a view with a submit formular (Html.BeginForm()), and want to set some model properties in the controller, returning the view, hoping to see this properties in the view. Here is my pattern on the standard MVC 3 logon formular. Precision: I r

  • Intellisense for model properties not working in View for HtmlHelper 2018-03-25

    My question is simple and might not be important to other coders, but it would really help a lot if some one can explain to me why I can't get the Intellisense working When using DisplayNameFor. It is working on DisplayFor or other models, but not th

  • Magic __get getter for static properties in PHP 2009-08-14

    public static function __get($value) does not work, and even if it did, it so happens that I already need the magic __get getter for instance properties in the same class. This probably is a yes or no question, so, it is possible? --------------Solut

  • How to get Hibernate configuration properties? 2009-11-17

    I`m using hibernate with jpa, and it is configured with persistence.xml Is it possible to get hibernate connection properties from web application? Thanks. --------------Solutions------------- Probably not without using reflection and relying on the

  • How to get the Model from an ActionResult? 2010-01-15

    I'm writing a unit test and I call an action method like this var result = controller.Action(123); result is ActionResult and I need to get the model somehow, anybody knows how to do this? --------------Solutions------------- In my version of ASP.NET

  • Get all models having implemented a certain method 2010-02-06

    I have many models created with the standard rails model generator. Some of the models have then got a method called foo(). Is there a simple way to figure out all the class names of the generated models having the foo() method implemented ? I mean p

  • Getting EL variable properties in eclipse 2010-03-15

    I'm using Eclipse/Spring source to edit JSP tags and EL. If I want to check what all the properties of a variable are is there a way to do this? Or even find out which file is generating the variable that's passed in. I'm thinking something along the

  • How to get the model of iPhone from code. (e.g. MC143C) 2010-06-05

    I need to get the iPhone model number from code. I can see the model number from my iPhone which is "MC143C", but when I am retrieving this by using code it is returning "iPhone". This is the code I am using to get the model number. NSLog(@"model: %@

  • C#, get all collection properties from an object 2010-06-11

    I have a class with 3 List collections like the following. I am trying to have a logic which will iterate through the object's "collection" properties and do some operation using the data stored in those collections. I am just wondering if there is a

  • What's the Rails Way to get the Model Class from the Controller? 2010-10-25

    I can do this in the controller to get the model class:"Controller", "").singularize.constantize But is there a built in method for this in the controller? Something like self.model_class? Thanks! --------------Solutions--------

  • How can I get some model's objects and its associated model's objects conditionally through an intermediary model object, using ActiveRecord 2011-01-19

    How can I get some model's objects and its associated model's objects conditionally through an intermediary model object? This will then be used to generate JSON (via to_json). Here is the setup made more generic: class JobSet < ActiveRecord::Base

  • When to use get and set properties in C# 2011-03-16

    When should we use get and set properties in C#? --------------Solutions------------- According to the Property Usage Guidelines on MSDN: Use a property when the member is a logical data member. In the following member declarations, Name is a propert

  • LINQ: How to get parent types' properties while getting their children collections 2011-04-10

    I have a collection of parent types that contains a list of children types: public class ParentType { public int ID; public string Name; public List<ChildType> ChildTypes { get; set; } } public class ChildType { public int ID; public string Nam

Copyright (C), All Rights Reserved.

processed in 0.137 (s). 11 q(s)