angularjs select drop down validation

Anna Smother

Angular v1.57:

Got a question, when I fill my select drop down I want to validate it. It should be required AND an item should be selected. When I get some data in my model and it's not good, the drop-down should not have selected anything. This works, out of the box, but it should make my select drop-down field $invalid in order to draw a simple red border around it (with css). All my input fields has the same construction.

As you can see, I have tried it with the plnkr, below, but no luck. The select drop-down field stays valid, even when nothing is selected, but my model ($scope.data.selector) has a "falsy" value.

$scope.data = {
    selector: 3
}

When I change the model to a valid value, e.g:

$scope.data = {
    selector: 2
}

I can see the value that is selected in the drop-down. But when there is a "falsy" value, the select drop-down should be $invalid. How can I achieve this (it should act like the input field when there is no value).

http://plnkr.co/edit/unmF87anBrm4q8ZguPMp?p=preview

<body ng-app="myApp" ng-controller="MyController">
  <form name="testForm" novalidate="">
    <label>Select a number</label>
    <div ng-class="{'has-error': testForm.testList.$invalid}">
      <select class="form-control" name="testList" ng-model="data.selector" ng-options="item.value as item.label for item in list" required></select>
    </div>

    <label>Input something</label>
    <div ng-class="{'has-error': testForm.testInput.$invalid}">
      <input class="form-control" name="testInput" type="text" ng-model="data.inputor" required />
    </div>
  </form>
</body>
var myApp = angular.module("myApp", []);

myApp.controller("MyController", function($scope) {
  $scope.data = {
    selector: 3,
    inputor: ""
  }
  $scope.list = [{
    value: 1,
    label: "One"
  }, {
    value: 2,
    label: "Two"
  }];
});
Anna Smother

Well the thing that gave me a desired result, was to look (in the controller) if the value is in the option list, if it is nothing happens to the model, if not make that part of the model undefined.

Not perfect, but it works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

angularjs select drop down validation

From Dev

Angularjs drop down validation not working

From Dev

Select Drop down Form Validation (Display text)

From Dev

AngularJS drop down - auto select not working

From Dev

multiple drop down list validation when select item using jquery

From Dev

AngularJS Bind data to select drop down when the value of another select drop down is changed

From Dev

AngularJS case-insensitive binding to a static select drop-down

From Dev

How do I position the drop down of AngularJS Material Select?

From Dev

Drop Down menu Validation with Alert

From Dev

AngularJs Search with drop down filter

From Dev

drop down filter not working in angularjs

From Dev

Select particular item in SELECT drop-down using angularjs when initialized from object array

From Dev

AngularJS: Set default select option when using directive to apply select drop down to page

From Dev

Angular Drop Down, Select by Index

From Dev

Drop down select form in Laravel

From Dev

php multi select drop down

From Dev

Remote modal on drop down select

From Dev

Splinter: Select From Drop Down

From Dev

Select tag with no drop down options

From Dev

Adding a Down Arrow to Select Drop Down

From Dev

Drop Down value mapping into anthore Drop Down in angularjs

From Dev

Data validation - Drop down list with no duplicates in excel

From Dev

Excel Validation Drop Down list using VBA

From Dev

Data Validation Drop Down Box Arrow Disappearing

From Dev

Using Data Validation with drop-down list

From Dev

validation only if a drop down value is not nil

From Dev

Excel Data Validation Drop Down Box

From Dev

Data Validation Drop Down Box Arrow Disappearing

From Dev

jQuery clone validation not working for HTML drop down

Related Related

  1. 1

    angularjs select drop down validation

  2. 2

    Angularjs drop down validation not working

  3. 3

    Select Drop down Form Validation (Display text)

  4. 4

    AngularJS drop down - auto select not working

  5. 5

    multiple drop down list validation when select item using jquery

  6. 6

    AngularJS Bind data to select drop down when the value of another select drop down is changed

  7. 7

    AngularJS case-insensitive binding to a static select drop-down

  8. 8

    How do I position the drop down of AngularJS Material Select?

  9. 9

    Drop Down menu Validation with Alert

  10. 10

    AngularJs Search with drop down filter

  11. 11

    drop down filter not working in angularjs

  12. 12

    Select particular item in SELECT drop-down using angularjs when initialized from object array

  13. 13

    AngularJS: Set default select option when using directive to apply select drop down to page

  14. 14

    Angular Drop Down, Select by Index

  15. 15

    Drop down select form in Laravel

  16. 16

    php multi select drop down

  17. 17

    Remote modal on drop down select

  18. 18

    Splinter: Select From Drop Down

  19. 19

    Select tag with no drop down options

  20. 20

    Adding a Down Arrow to Select Drop Down

  21. 21

    Drop Down value mapping into anthore Drop Down in angularjs

  22. 22

    Data validation - Drop down list with no duplicates in excel

  23. 23

    Excel Validation Drop Down list using VBA

  24. 24

    Data Validation Drop Down Box Arrow Disappearing

  25. 25

    Using Data Validation with drop-down list

  26. 26

    validation only if a drop down value is not nil

  27. 27

    Excel Data Validation Drop Down Box

  28. 28

    Data Validation Drop Down Box Arrow Disappearing

  29. 29

    jQuery clone validation not working for HTML drop down

HotTag

Archive