I have a select control and a text control in my form. When there is a change in the selection (when user selects a particular value), i want the text control to be made mandatory. This text control should not be mandatory when the form is opened. Is this possible?
Am using bootstrap 3 and AngularJS. And am a newbie to both.
here is my code:
<div class="form-group">
<label class="col-sm-2 control-label">Select an Option</label>
<div class="col-sm-4">
<select class="form-control" name="type">
<option value="A">Option A</option>
<option value="B">Option B</option>
</select>
</div>
<div class="col-sm-4">
<input type="text" class="form-control" name="text" id="text" ng-model="text" >
<span class="help-block" style="color:red" ng-show="myform.text.$dirty && myform.text.$invalid">
<span ng-show="myform.text.$error.required">Required, when Option B is selected</span>
</span>
</div>
</div>
By default "option A" will be selected, when the user selects "Option B", i want my text control to be made mandatory. This will not be mandatory when the form is opened
Any help is much appreciated
I would use ng-change in your select box to set a $scope property that will toggle the value of ng-required in your text field.
The select box:
<select class="form-control" name="type" ng-model="optionValue" ng-change="setRequired()">
<option value="A">Option A</option>
<option value="B">Option B</option>
</select>
The text field:
<input type="text" class="form-control" name="text" id="text" ng-model="text" ng-required="textRequired" />
The controller:
function mainController($scope) {
$scope.optionValue = 'A';
$scope.textRequired = false;
$scope.setRequired = function() {
if($scope.optionValue==='B') {
$scope.textRequired= true;
}
else {
$scope.textRequired = false;
}
}
}
http://plnkr.co/edit/lyMgKIEJukpgnA31hnTz?p=preview
Related
i want to dispay language as selected in options but {{selectedlan.Language}} is not working
<div>
<select class="form-control col-md-3 col-6 mb1 id="" name="languages" ng-model="selectedlan"
ng-options="languages as languages.Language for languages in lan" autocomplete="off" required>
</select>
<p>{{selectedlan.Language}}</p>
</div>
I am not able to dynamically set required attribute to input fields
My form's required attribute has to change dynamically based on a radio button field.
This is how the form looks when the radio button is set as wants to join screenshot. and when radio button is set as member/alumnus the form looks like this screenshot
I am not able to dynamically switch the required attribute for input fields in respective forms.
This is the code for contact and DOB field if new Member is checked
<label class="control-label is-imp"></label>
<div class="input-group">
<input type="radio" ng-model="joinFormData.joinType" value="wantsToJoin">I Want to join as member
<input type="radio" ng-model="joinFormData.joinType" value="alreadyMember">I am a member/alumnus
</div>
</div>
<div class="form-group no-pad-left" ng-if="joinFormData.joinType == 'wantsToJoin'">
<div class="nss-only" ng-if="clubType == 'NSS'">
<div class="form-group col-md-12 no-pad-left no-pad-right">
<div class="col-md-6 no-pad-left">
<label class="control-label is-imp">Contact:</label>
<input
type="text" class="form-control" id="body_contact"
name="body_contact" ng-model="joinFormData.contact" placeholder="1234567890" ng-pattern="/^[0-9]{10}$/" ng-minlength="10" ng-maxlength="10" integer [ng-required]="joinFormData.joinType == 'wantsToJoin'">
<span ng-show="joinForm.body_contact.$error.$invalid || joinForm.body_contact.$error.pattern"><span class = "red-color">Enter a valid number!</span></span>
</div>
<div class="col-md-6 no-pad-right">
<label class="control-label is-imp">DOB:</label>
<input
type="date" class="form-control" id="body_dob"
name="body_dob" ng-model="joinFormData.dob"
required ng-maxlength="100" required>
<span ng-show="joinForm.body_dob.$error.required && joinForm.body_dob.$touched"><span class="red-color">Required!</span></span>
</div>
</div> </div> </div>
This is the code for select type field if Member/ Alumnus is checked
<div class="form-group col-md-12 no-pad-left no-pad-right">
<div class="col-md-6 no-pad-left" ng-if="joinFormData.joinType == 'alreadyMember'">
<label class="control-label is-imp">I am already a member/alumnus:</label>
<div class="input-group">
<select ng-model="joinFormData.memberType"
class="form-control selectpicker" [required]="joinFormData.joinType == 'alreadyMember'" name="body_type">
<option value="" selected disabled>Select Type</option>
<option value="member">Member</option>
<option value="alumni">Alumni</option>
</select>
<!-- <span ng-if="!joinForm.body_type.$valid && joinForm.body_type.$touched"><span class="red-color">Required!</span></span> -->
</div>
</div></div>
I tried using [required]="joinFormData.joinType == 'alreadyMember'" conditions but that didn't work.
Kindly help me out. I am not able to figure out the mistake as i am new to angular
I'm getting a JSON value from one WebService like this
"electricity":
{ "selectedElectricityBoard":"ABCD",
"electrictyConsumerNumber":"12345",
"isElectricityBoardChecked":true
}
I generated a Drop Down list from the JSON data,after that i select the value and submit the value, the value gets submitted successfully from my UI, now the case is I'm editing the same form for which i'm making a Web Service call which returns me the same value, which i have selected from the UI, but the selected value is not being populated/reflected in the drop-down menu, any suggestion or help would be highly appreciable, My UI is in Angular JS, the code snippet is as follows :
<div class="col-xs-2 no-padding ">
<label>Consumer No.:</label>
</div>
<div class="col-xs-3">
<span ng-hide="true" class="form-control" ng-required="true"
close-text="Close">
</span>
<div class="group-3-text-boxes col-xs-3 no-padding" style="min-width: 100px;">
<input type="text" name="consumer_number"
ng-disabled="!form_data[selectedTab].electricity.isElectricityBoardChecked"
ng-model="form_data[selectedTab].electricity.electrictyConsumerNumber"
class="input-field"
ng-class="{true:'invalid-field',false:''}
form_data[selectedTab].electricity.isElectricityBoardChecked && form_data[selectedTab].invalid.electricity]" required />
<html ng-app="app" ng-controller="ctrl">
<select id="state" ng-model="states" ng-options="state for (state,states) in total_states">
<option value=''>Select State</option>
</select>
<select id="district" ng-model="districts" ng-options="district for (district,districts) in states">
<option value=''>Select District</option>
</select>
<select id="city" ng-model="cities" ng-options="cities for cities in districts">
<option value=''>Select City</option>
</select>
<br>
<br>
<br>
State :: {{states}}
<br>
<br>
<br>
District:: {{districts}}
<br>
<br>
<br>
City :: {{cities}}
for controller:
app.controller('ctrl',ctrl);
ctrl.$inject=['$scope'];
function ctrl($scope) {
$scope.total_states = {
'Andhra Pradesh': {
'Prakasam': ['Ongole', 'Chirala', 'Mrakapuram'],
'Krishna': ['Vijayawada', 'Machalipatnam'],
'Guntur': ['Guntur', 'Tenali', 'Bapatla']
},
'Telangana': {
'Hyderabad': ['Hyderabad'],
'Warangal': ['Wrangal', 'Hanmkonda'],
'Khammam': ['Khmammam', 'Badrachalam']
}
};
}
try this it works...
So my problem is as follows, i have this HTML
<div class="col-sm-4">
<p><span>Teste A/B:</span></p>
<label class="label-radio">
<input type="radio" class="radio" name="ab-test" value="true" ng-model="isTestAvailable"> Sim </label>
<label class="label-radio">
<input type="radio" class="radio" name="ab-test" value="false" ng-model="isTestAvailable"> Não </label>
</div>
<div class="col-sm-4">
<p><span>Nº de testes:</span></p>
<select class="form-control" ng-model="tests.number">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
</div>
</div>
What i'm trying to do is make it so that when the user changes the select to another value i generate the given amount of test inputs via ng-repeat
<div ng-if="isTestAvailable == 'true'" ng-repeat="test in tests">
<hr>
<div class="row">
<div class="col-sm-12">
<h3>Test {{$index}}</h3>
<p><span>Specifications:</span></p>
<textarea id="teste1" class="form-control" onkeyup="resizeTextarea('test1')" data-resizable="true"></textarea>
</div>
</div>
<hr>
</div>
My controller contains this $watch
$scope.$watch("tests.number", function(newVal, oldVal) {
$scope.tests = [{}];
var i = parseInt(newVal);
for(var x = i; x <= newVal; x++) {
$scope.tests.push({});
}
});
that i tried to use in order to change the amount of test objects when the value changes, i tried using a simple variable as well and doing ng-repeat("i in tests.number") instead but that didn't work, what can i do so that the ng-repeat works with my given select option value?
I suggest to you, to use another variable instead of tests.number because you override it in your watcher.
Please check this plunker : https://plnkr.co/edit/XnV9MKjIYe1YOL4hR6Le?p=preview
I use another variable and bind ng-change instead of watch
<select class="form-control" ng-model="testNumber" ng-change="changeTestNumber()">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
I would like to:
disable radio button (with name 'advertisementType') when select list (with name 'updateAdvertisement') has got only 1 element
disable submit button (with name 'saveButton') when selected option on select list has got null value
How can I do that in angular?
This is my html:
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" value="false">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-show="advertisementType == 'false'">
<option value>Select item</option>
<option value="1">Volvo</option>
<option value="2">Saab</option>
<option value="3">Mercedes</option>
<option value="4">Audi</option>
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="saveButton" />
</form>
</div>
This is my javascript (but I have no idea):
<script>
function AdvertisementCtrl($scope) {
}
</script>
HTML
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" ng-disabled='carTypes.length<=1' value="false">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-options="item.type for item in carTypes">
<option value>Select item</option>
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="!updateAdvertisement" />
</form>
</div>
JavaScript
function AdvertisementCtrl($scope) {
$scope.carTypes = [
{id:'1', type:'Volvo'},
{id:'2', type:'Saab'},
{id:'3', type:'Mercedes'},
{id:'4', type:'Audi'}
]
}
The JSFiddle is here http://jsfiddle.net/7Jw9B/
Populate the select from an array that you declare in the controller
Check array length and disable the radio button when array length is 1
Disable saveButton based on the value of updateAdvertisement
<script>
function AdvertisementCtrl($scope) {
$scope.cars = ['Volvo', 'Saab', 'Mercedes', 'Audi', 'BMW'];
}
</script>
Markup:
<div ng-controller="AdvertisementCtrl">
<form ng-submit="save()">
<input type="radio" ng-model="advertisementType" name="advertisementType" value="false" ng-disabled="cars.length <= 1">Update<br />
<select ng-model="updateAdvertisement" name="updateAdvertisement" ng-show="advertisementType == 'false'" ng-options="cars">
</select>
<input type="submit" value="Save" name="saveButton" ng-disabled="updateAdvertisement" />
</form>
</div>
Should probably be tested in a fiddle :-), could do that later