2

I am receiving a JSON data from backend like these

{
  "data":{
    "xyz":[
      {
        "number":"1",
        "short_text":"Vertrag unterzeichnen",
        "long_text":"Nach Vertrabsunterzeichnung Namen eintragen",
        "is_photo":false
      },
      {
        "number":"2",
        "short_text":"HR unterrichten",
        "long_text":"HR hat eigene Workflows",
        "is_photo":true
      }
    ]
  }
}

And in the html i am populating a form by ng-repeat

<tr data-ng-repeat="choice in choices track  by $index">
    <td>{{choice.number}}</td>
    <td><p>{{choice.short_text}}</p></td>
    <td><input type="textbox" size="50" class="des-textinput" ng-model="choice.desc" required></td>
    <td><input type="checkbox"  ng-model="choice.include"></td>
    <td><input type="file" id="abc{{$index}}" class="photo-upload"
               file-model="pic{{$index}}" accept="image/*">
    </td>
</tr>

Now I want to make the input type file required if the value of is_photo is true in the JSON I am receiving. For each row if the value if is_photo is false then it will be not required.

From the given JSON the condition will be the first input type file will be not required as first row is_photo is false, but the second one will be required as the value of is_photo is true.

How will I do that?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Rahul
  • 159
  • 2
  • 15
  • 1
    Please post your form's code as well. – 31piy Nov 15 '17 at 10:32
  • If you want your `file-model` directive to integrate with the [ng-model controller](https://docs.angularjs.org/api/ng/type/ngModel.NgModelController) and the [ng-form controller](https://docs.angularjs.org/api/ng/type/form.FormController), you need to post your code for that directive. – georgeawg Nov 15 '17 at 18:59

3 Answers3

2

You can use "ng-required"

For documentation you could read this: https://docs.angularjs.org/api/ng/directive/ngRequired

Like this

<input name="myInput" ng-model="myInput" ng-required="myVar == 2">

//If photo is true required will be true else false
<input name="myInput" ng-model="myInput" ng-required="_photo">

<input name="myInput" ng-model="myInput" ng-required="choice.is_photo">

//Or use some function which returns boolean
<input name="myInput" ng-model="myInput" ng-required="isRequired(choice)">

//This is how you would use it with form and stop form from submittion    
<form ng-app="myApp" ng-controller="validateCtrl"
      ng-init="isRequired=true"
      name="myForm" novalidate ng-submit="myForm.$valid && submit()">
        Username: <input type="text" name="user" ng-model="user" 
                         ng-required="isRequired">
        Email : <input type="email" name="email" ng-model="email" required>
        <input type="submit" ng-click="isRequired=!isRequired;" />
</form>

    <script>
       var app = angular.module('myApp', []);
       app.controller('validateCtrl', function($scope) {
           $scope.user = 'John Doe';
           $scope.email = 'john.doe@gmail.com';
           $scope.submit = () => {console.log("s");}
       });
    </script>
Hey24sheep
  • 1,172
  • 6
  • 16
0

you use this use

it will set automatically if _photo is true it make as required

<input name="myInput" ng-model="myInput" ng-required="_photo">
Vinoth
  • 972
  • 1
  • 16
  • 47
0

You can use ng-required property. ref ng-required

<input type="file" id="abc{{$index}}" class="photo-upload"
       file-model="pic{{$index}}" accept="image/*"
       ng-required="choice.is_photo == true" />
georgeawg
  • 48,608
  • 13
  • 72
  • 95
Sathiyaraj
  • 343
  • 3
  • 11