Angular JS Magazine

luni, 1 septembrie 2014

Angular and Form Elements - part 6

Sometimes you may find annoying the validation implementation from Angular and Form Elements - part 5 and you want a validation process that takes action after the submit button is pressed. You can modify the example from Angular and Form Elements - part 5 like this:

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <title></title>
    <script src="../angular/angular.min.js"></script>
    <link href="../bootstrap/css/bootstrap.css" rel="stylesheet"/>
    <link href="../bootstrap/css/bootstrap-theme.css" rel="stylesheet"/>
    <script>
        angular.module("atpModule", [])
                .controller("atpCtrl", function ($scope) {
                    $scope.addNewPlayer = function (newPlayer) {
                        if (atpForm.$valid) {
                            if (angular.isDefined(newPlayer)) {
                                $scope.atp.push({name: newPlayer.name, 
                                   email: newPlayer.email, rank: $scope.atp.length + 1});
                            }
                        } else {
                            $scope.validationStatus = true;
                        }
                    };
                    $scope.getErrorMessage = function (error) {
                        if (angular.isDefined(error)) {
                            if (error.required) {
                                return "Please provide a value";
                            } else if (error.email) {
                                return "Please provide a valid e-mail address";
                            }
                        }
                    };
                });
    </script>
    <style>
        span.error {
            color: red;
            size:10px;
        }
    </style>
</head>
<body>
<div ng-app="atpModule" ng-controller="atpCtrl" ng-init="atp=[
     { name: 'Nadal, Rafael (ESP)', email: 'nadalrafael@gmail.com', rank: 1 },
     { name: 'Djokovic, Novak (SRB)', email: 'djokovicnovak@gmail.com', rank: 2 },
     { name: 'Federer, Roger (SUI)', email: 'federerroger@gmail.com', rank: 3 },
     { name: 'Wawrinka, Stan (SUI)', email: 'wawrinkastan@gmail.com', rank: 4 },
     { name: 'Ferrer, David (ESP)', email: 'ferrerdavid@gmail.com', rank: 5 }]">

    <div id="atpPanel" class="panel">
        <h3 class="panel-header">ATP SINGLES RANKINGS</h3>
        <table class="table">
            <thead>
            <tr>
                <th>Name</th>
                <th>E-mail</th>
                <th>Rank</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="item in atp">
                <td>{{item.name}}</td>
                <td>{{item.email}}</td>
                <td>{{item.rank}}</td>
            </tr>
            </tbody>
        </table>
    </div>
    <div class="col-xs-6">
        <form name="atpForm" novalidate ng-submit="addNewPlayer(player)">
            <div class="well">
                <div class="form-group row">
                    <label for="playerNameId">Player Name:</label>
                    <input id="playerNameId" name="playerName" 
                               type="text" class="form-control" ng-model="player.name"
                                required>

                    <div ng-show="validationStatus">
                        <span class="error">{{getErrorMessage(atpForm.playerName.$error)}}</span>
                    </div>
                </div>
                <div class="form-group row">
                    <label for="playerEmailId">Player E-mail:</label>
                    <input id="playerEmailId" name="playerEmail" type="email" class="form-control"
                           ng-model="player.email" required>

                    <div ng-show="validationStatus">
                        <span class="error">{{getErrorMessage(atpForm.playerEmail.$error)}}</span>
                    </div>
                </div>
                <div class="form-group row">
                    <label for="playerRankId">Player Rank:</label>
                    <span id="playerRankId">{{atp.length + 1}}</span>
                </div>
                <button class="btn btn-primary btn-block">
                    Add Player
                </button>
            </div>
        </form>
    </div>
</div>
</body>
</html>

Until the Add Player button is pressed:

After the Add Player button was pressed: