1

I have single page NodeJS + AngularJS app. There are some fields to fill with text. In controller.js I wanna to check if one or more of fields are empty to send alert with error in browser.

My controller.js file:

var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {
    console.log("Hello World from controller");

    //$http.get('/builder').success(function(response){
    //    $scope.PEM = response;
    //});

    $scope.sendParts = function(){
        var bool = true;
        for(var prop in $scope.pem){
            if(prop == 'undefined'){
                bool = false;
                break;
            }

            if(bool){
                $http.post('/builder', $scope.pem).success(function(response){
                $scope.PEM = response;
            });
            }
            else{
                // why it is not working?
                alert("ERROR");
                //OR
                $scope.PEM = "ERROR";
            }
        }
    }
}]);

index.html file:

<!DOCTYPE>
<html ng-app="myApp">
<head>

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"
          integrity="sha384-1q8mTJOASx8j1Au+a5WDVnPi2lkFfwwEAa8hDDdjZlpLegxhjVME1fgjWPGmkzs7" crossorigin="anonymous">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap-theme.min.css"
          integrity="sha384-fLW2N01lMqjakBkx3l/M9EahuwpSfeNvV63J5ezn3uZzapT0u7EYsXMjQV+0En5r" crossorigin="anonymous">

    <title>PEMbuilder</title>
    <style>
        .container {
            margin-left: 30%;
            margin-top: 5%;
            padding: 10px;
            background: #ffbf80;
            width: 390px;
            border: 2px solid #ffbf80;
            border-radius: 5px;
        }
        textarea {
            resize: vertical;
        }

    </style>
</head>
<body>


  <div class="container" ng-controller="AppCtrl">
  <h1>PEMbuilder</h1>
          <h4><b>Name of project:</b></h4>
          <input class="data" ng-model="pem.name" type="text">
          <h4><b>Certificate:</b></h4>
          <textarea rows="4" cols="50" class="data" ng-model="pem.crt" placeholder="Enter your certificate"></textarea>
          <h4><b>Intermediate:</b></h4>
          <textarea rows="4" cols="50" class="data" ng-model="pem.int" placeholder="Enter your intermediate"></textarea>
          <h4><b>Root:</b></h4>
          <textarea rows="4" cols="50" class="data" ng-model="pem.root" placeholder="Enter your root"></textarea>
          <h4><b>Private key:</b></h4>
          <textarea rows="4" cols="50" class="data" ng-model="pem.pk" placeholder="Enter your private key"></textarea>
          <h4><b>Password:</b></h4>
          <input class="data" ng-model="pem.pass" type="password">

          <h4><button class="btn btn-primary" ng-click="sendParts()">Create full certificate</button></h4>
          <h4><button class="btn btn-primary" onclick="window.location.href = '/download';">Download full certificate</button></h4>

      <h3 style="color: red">{{err}}</h3>
      <a onclick="$('#text').slideToggle('slow');" href="javascript://">Show/Hide full pem file</a>
      <div id="text" style="display:none; white-space: pre-wrap; word-wrap: break-word; -moz-control-character-visibility: visible;">{{PEM}}</div>

  </div>

  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
  <script src='https://ajax.googleapis.com/ajax/libs/angularjs/1.5.0-rc.0/angular.min.js'></script>
  <script src='controllers/controller.js'></script>
</body>
</html>

So I want to handle somehow if any field is empty. screenshot of app

3
  • 1
    Check out ngForm and custom validators Commented Feb 24, 2016 at 21:13
  • Have you stepped through your code to make sure the 'bool' is actually false? Also, I don't see where $scope.pem is being initiated. Finally, you're referring to $scope.pem and $scope.PEM. Commented Feb 24, 2016 at 21:15
  • $scope.pem is not defined until your have at least one field with input. One easy work aroundwould be to initially set var bool = $scope.pem. However I suggest you look into ngForm as well Commented Feb 24, 2016 at 21:17

2 Answers 2

2

Notice that when no fields have text entered that $scope.pem is undefined. Once at least one field has text input, the model is defined. A quick work around would be to check if $scope.pem === undefined and if that condition is true, raise an alert.

var myApp = angular.module('myApp', []);
myApp.controller('AppCtrl', ['$scope', '$http', function($scope, $http) {

    $scope.sendParts = function(){
        if($scope.pem === undefined) alert("I have no fields filled out")
        var bool = true;
        for(var prop in $scope.pem){
            if(prop == 'undefined'){
                bool = false;
                break;
            }

            if(bool){

            }
            else{
                // why it is not working?
                alert("ERROR");
                //OR
                $scope.PEM = "ERROR";
            }
        }
    }
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

Please do check out ngForm
0

You need to inject $window to your controller, and use it like this:

$window.alert("ERROR");

$window (angularjs doc)

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.