1

i'm a newbie in angular JS. and i was trying the input type="text", i'm retrieving my name from my controller and i was using $dirty and $invalid to validate my data but it isn't showing when i make the input field empty.

JS Fiddle : https://jsfiddle.net/U3pVM/24718/

var FormValidations = angular.module('FormValidations',[]);

FormValidations.controller('FormsValidations', function( $scope ){

    $scope.formsToBeValidated = {
        firstName : 'Daniel'
    };


});
1
  • why don't you use ng-messages Commented May 11, 2016 at 7:16

4 Answers 4

3

Unless you add name attribute to form level field, that will not get included in form object when you specified in the name attribute on form, currentlyh it is name="studentForm". Do add name="firstName" to your input field

<input type="text" class="form-control" 
  name="firstName" ng-model="formsToBeValidated.firstName" required/>

Forked Fiddle

For more information you could refer this answer, which has more detailed


More convenient way to solve this issue would be using ng-messages instead of using ng-show/ng-hide, for that you have to include ng-messages module with its ng-messages dependency.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the help ... name attribute was missing.
@Daniel do go through the link which I've added in answer. Thanks :)
0

you should try this:

      <input name="firstName" type="text" class="form-control" 
ng-model="formsToBeValidated.firstName" required>

fiddle

Comments

0

as other says , add "name" attribute to your input.

I made a custom directive to display error on input using ng-message ( so you have to include it )

directives.directive('inputerrormsg', function () {
    return {
        replace: true,
        restrict: 'E',
        plain: true,
        scope: {
            inputdata: "=",
            dpattern: '@'
        },
        template: '<div class="help-block has-error text-center" ng-messages="inputdata[\'$error\']" >' +
        '<p ng-message="required">{{\'The field is required\'| translate }}</p>' +
        '<p ng-message="minlength">{{\'Input too short\'| translate }}</p>' +
        '<p ng-message="maxlength">{{\'Input too long\'| translate }}</p>' +
        '<p ng-message="email">{{\'Email invalid\'| translate }}</p>' +
        '<p ng-message="date">{{\'Date invalid\'| translate }}</p>' +
        '<p ng-message="number">{{\'Write number only\'| translate }}</p>' +
        '<p ng-message="pattern">' +
        '<span ng-switch="dpattern">' +
           '<span ng-switch-when="date">{{\'Date incorrecte: YYYY, YYYY/MM, YYYY/MM/DD\' |translate}}</span>' +
        '<span ng-switch-default>{{\'Saisir uniquement des lettres\'| translate }}</span>' +
        '</span>' +
        '</p>' +
        '</div>'
    }
});

USAGE :

required field

<div style="padding-left: 0px;" ng-class="{ 'has-error' : !formDeter.scientificname.$valid">
<div class="input-group no-padding">
    <input type="text"
           class="form-control input-md" name="scientificname"
           required />
    <span class="input-group-addon">
          <span class="glyphicon glyphicon-question-sign"></span>
    </span>
</div>
<dir.inputerrormsg inputdata="formDeter.scientificname" ></dir.inputerrormsg>

EMail :

<div class="form-group row" ng-class="{ 'has-error' : !formData.email.$valid }">
    <label class="col-xs-4 control-label">{{"Email" | translate }}</label>
    <div class="col-xs-8 input-group">
        <input type="email" class="form-control input-md" ng-model="email" name="email" id="email" required
               placeholder="{{'Email' | translate }}"/>
    </div>
    <dir.inputerrormsg inputdata="formData.email" dpattern="email" ></dir.inputerrormsg>
</div>

Number only :

 <div class="form-group row" ng-class="{ 'has-error' : !formLocalisation.decimallongitude.$valid }">
    <label class="col-xs-4 control-label">{{"Longitude" | translate }}</label>
    <div class="col-xs-8 input-group">
        <input type="number" popover-trigger="focus" placeholder="{{'Longitude' | translate }}"
               class="form-control input-md" 
               ng-model="specimen.decimallongitude" name="decimallongitude"
                />
        <span class="input-group-addon" >
            <span class="glyphicon glyphicon-question-sign"></span>
        </span>
    </div>
    <dir.inputerrormsg inputdata="formLocalisation.decimallongitude" ></dir.inputerrormsg>
</div>

...

Comments

0

You didn't specified name to your input. Try this:

<form name="studentForm" novalidate>
    <input type="text" class="form-control" name ="input" ng-model="formsToBeValidated.firstName" required>
    <span studentForm.input.$error.required && studentForm.input.$dirty>First Name is Required</span>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.