0

I have a form which seems to work well on the most part. However, my selects are playing up a bit, and I cant seem to submit the form. My form looks like

<form ng-submit="submit(emailform)" name="emailform" method="post" action="" class="form-horizontal emailType" role="form">
    <div class="form-group" ng-class="{ 'has-error': emailform.inputTitle.$invalid && submitted }">
        <label for="inputTitle" class="col-lg-4 control-label">Title</label>
        <div class="col-lg-8">
            <select ng-model="formData.inputTitle"  data-ng-options="title for title in titles" id="inputTitle" required>
                <option value="">Please select</option>
            </select>
        </div>
    </div>
    <div class="form-group" ng-class="{ 'has-error': emailform.inputName.$invalid && submitted }">
        <label for="inputName" class="col-lg-4 control-label">First Name(s)</label>
        <div class="col-lg-8">
            <input ng-model="formData.inputName" type="text" class="form-control" id="inputName" name="inputName" placeholder="First Name(s)" required>
        </div>
    </div>
    <div class="form-group" ng-class="{ 'has-error': emailform.inputLinks.$invalid && submitted }">
        <label for="inputLinks" class="col-lg-4 control-label">Link to be sent</label>
        <div class="col-lg-8">
            <select ng-model="formData.inputLinks"  data-ng-options="link for link in links" id="inputLinks" required>
                <option value="">Please select</option>
            </select>
        </div>
    </div>
    <div class="form-group">
        <div class="col-lg-offset-2 col-lg-10">
            <button type="submit" class="btn btn-default" ng-disabled="submitButtonDisabled">
                Send Message
            </button>
        </div>
    </div>
</form>
<p ng-class="result" style="padding: 15px; margin: 0;">{{ resultMessage }}</p>

So a simple form with two selects and one input. My Controller looks like the following

'use strict';

/* Controllers */

function EmailViewCtrl($scope, $http) {

    $scope.titles =
    [
        "Mr",
        "Mrs",
        "Miss",
        "Ms",
        "Dr"
    ];

    $scope.links =
    [
        "email1",
        "email2",
        "email3",
        "email4",
        "email5"
    ];

    $scope.result = 'hidden'
    $scope.resultMessage;
    $scope.formData; //formData is an object holding the name, email, subject, and message
    $scope.submitButtonDisabled = false;
    $scope.submitted = false; //used so that form errors are shown only after the form has been submitted
    $scope.submit = function(emailform) {
        $scope.submitted = true;
        $scope.submitButtonDisabled = true;
        if (emailform.$valid) {
            $http({
                method  : 'POST',
                url     : 'backend/email.php',
                data    : $.param($scope.formData),  //param method from jQuery
                headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  //set the headers so angular passing info as form data (not request payload)
            }).success(function(data){
                console.log(data);
                if (data.success) { //success comes from the return json object
                    $scope.submitButtonDisabled = true;
                    $scope.resultMessage = data.message;
                    $scope.result='bg-success';
                } else {
                    $scope.submitButtonDisabled = false;
                    $scope.resultMessage = data.message;
                    $scope.result='bg-danger';
                }
            });
        } else {
            $scope.submitButtonDisabled = false;
            $scope.resultMessage = 'Failed <img src="http://www.chaosm.net/blog/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley">  Please fill out all the fields.';
            $scope.result='bg-danger';
        }
    }
}

EmailViewCtrl.$inject = ['$scope', '$http'];

Now the problem is, my selects on their default option (please select) have a red border around them on page load. Obviously this should not appear until they submit the form without an option selected.

Secondly, if I provide the form with valid data, the submit button does not seem to become active. How can I make this active?

Lastly, at the moment, everything is in one controller. Should I move things like the selects values into their own controller and what would be the best way to achieve this?

Thanks

2
  • I don't get the red borders: plnkr.co/edit/aaA69cMcGlC1GuTrX8sw?p=preview Commented Jul 22, 2015 at 9:10
  • Strange - I just double checked and it seems that I only get the red border in firefox. Commented Jul 22, 2015 at 9:20

1 Answer 1

1

You can use form.input.$dirty to check if an input has been touched and only in that case show a validation error.

ng-class="{ 'has-error': emailform.inputName.$invalid && emailform.inputName.$dirty }"

See the example below for a working copy of your code:

var app = angular.module("app", []);

app.controller("EmailViewCtrl", function EmailViewCtrl($scope, $http) {

  $scope.titles = [
    "Mr",
    "Mrs",
    "Miss",
    "Ms",
    "Dr"
  ];

  $scope.links = [
    "email1",
    "email2",
    "email3",
    "email4",
    "email5"
  ];

  $scope.result = 'hidden'
  $scope.resultMessage;
  $scope.formData; //formData is an object holding the name, email, subject, and message
  $scope.submitButtonDisabled = false;
  $scope.submitted = false; //used so that form errors are shown only after the form has been submitted
  $scope.submit = function(emailform) {
    $scope.submitted = true;
    $scope.submitButtonDisabled = true;
    if (emailform.$valid) {
      alert("POST!");
    } else {
      $scope.submitButtonDisabled = false;
      $scope.resultMessage = 'Failed <img src="http://www.chaosm.net/blog/wp-includes/images/smilies/icon_sad.gif" alt=":(" class="wp-smiley">  Please fill out all the fields.';
      $scope.result = 'bg-danger';
    }
  }
});
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>


<div ng-app="app" ng-controller="EmailViewCtrl">
  <form ng-submit="submit(emailform)" name="emailform" method="post" action="" class="form-horizontal emailType" role="form">
    <div class="form-group" ng-class="{ 'has-error': emailform.inputTitle.$invalid && emailform.inputTitle.$dirty }">
      <label for="inputTitle" class="col-lg-4 control-label">Title</label>
      <div class="col-lg-8">
        <select class="form-control" ng-model="formData.inputTitle" data-ng-options="title for title in titles" id="inputTitle" required>
          <option value="">Please select</option>
        </select>
      </div>
    </div>
    <div class="form-group" ng-class="{ 'has-error': emailform.inputName.$invalid && emailform.inputName.$dirty }">
      <label for="inputName" class="col-lg-4 control-label">First Name(s)</label>
      <div class="col-lg-8">
        <input ng-model="formData.inputName" type="text" class="form-control" id="inputName" name="inputName" placeholder="First Name(s)" required>
      </div>
    </div>
    <div class="form-group" ng-class="{ 'has-error': emailform.inputLinks.$invalid && emailform.inputLinks.$dirty }">
      <label for="inputLinks" class="col-lg-4 control-label">Link to be sent</label>
      <div class="col-lg-8">
        <select class="form-control" ng-model="formData.inputLinks" data-ng-options="link for link in links" id="inputLinks" required>
          <option value="">Please select</option>
        </select>
      </div>
    </div>
    <div class="form-group">
      <div class="col-lg-offset-2 col-lg-10">
        <button type="submit" class="btn btn-default" ng-disabled="submitButtonDisabled">
          Send Message
        </button>
      </div>
    </div>
  </form>
  <p ng-class="result" style="padding: 15px; margin: 0;">{{ resultMessage }}</p>
</div>

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

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.