Clear input fields when checkbox is checked - angularjs

I have a check box and two input fields. Either the check box has to be checked or the two input fields have to be filled in. I'm doing validation using Angular i.e. ng-show, ng-required.
When the checkbox is checked the two input fields are disabled i.e. ng-disabled="$parent.vm.selectAllDates".
Now I also have to clear any data that may have been entered into the textboxes.
The check box is not checked on page load. It is set in the controller like this: vm.selectAllDates = false;
Is there some way to clear the input fields when the check box is checked in Angular?
EDIT
I've added what I tried doing using your example
Check box:
<input name="dateSelectAll" type="checkbox"
ng-model="$parent.vm.selectAllDates"
ng-required="vm.selectedReport.Parameters.StartDate == null && vm.selectedReport.Parameters.EndDate == null" />All Available Dates
Start date input:
<input name="beginningDate" type="text" class="form-control form-field" datepicker-popup="dd-MMM-yyyy"
ng-disabled="$parent.vm.selectAllDates"
ng-model="$parent.vm.selectedReport.Parameters.StartDate"
is-open="beginningDateOpened"
ng-required="$parent.vm.selectAllDates == false"
ng-change="$parent.vm.selectedReport.Parameters.StartDate = $parent.vm.selectAllDates ? '' : $parent.vm.selectedReport.Parameters.StartDate"
close-text="Close" />
End date:
<input name="endDate" type="text" class="form-control form-field" datepicker-popup="dd-MMM-yyyy"
ng-disabled="$parent.vm.selectAllDates"
ng-model="$parent.vm.selectedReport.Parameters.EndDate"
is-open="endDateOpened" ng-change="$parent.vm.selectedReport.Parameters.EndDate = $parent.vm.selectAllDates ? '' : $parent.vm.selectedReport.Parameters.EndDate"
ng-required="$parent.vm.selectAllDates == false && $parent.vm.selectedReport.Parameters.EndDate == null"
close-text="Close" />
One really odd thing was I wanted to see what was happening so I added
{{$parent.vm.selectedReport.Parameters.StartDate = $parent.vm.selectAllDates ? '' : $parent.vm.selectedReport.Parameters.StartDate}}
after the StartDate input field. When I selected a date, the date was added to the input field as well as underneath the input. When I clicked the check box, the date was removed from the input field as well as from under the input field. So it worked! But the minute I removed the above code from under the input field it stopped working...I was like wha?

The simple approach is to use ngChange directive on checkbox and set text input model to empty string if checkbox is checked. Something like this:
<input type="text" ng-model="data.test" ng-disabled="data.check">
<input type="checkbox" ng-model="data.check" value="one"
ng-change="data.test = data.check ? '' : data.test">
Demo: http://plnkr.co/edit/pKGui2LkP487jP0wOfHf?p=preview

I ended up building off of what you showed me #dfsq. Thank you very much for your help!
I ended up creating a function for ng-change and putting it on the check box:
<input name="dateSelectAll" type="checkbox"
ng-model="$parent.vm.selectAllDates"
ng-change="vm.clearFields()"
ng-required="vm.selectedReport.Parameters.StartDate == null && vm.selectedReport.Parameters.EndDate == null" />All Available Dates
Then in my controller I added the code to clear both start and end date input fields:
vm.clearFields = function () {
vm.selectedReport.Parameters.StartDate = vm.selectAllDates ? '' : vm.selectedReport.Parameters.StartDate;
vm.selectedReport.Parameters.EndDate = vm.selectAllDates ? '' : vm.selectedReport.Parameters.EndDate;
}
If there is a better way, please let me know. Thanks again!

Related

set ng-model to null on disabled an input

new in angular here.
I have an input field disabled base on the radio button, if the radio button was selected, I first want the input field value to null and then disabled it.
Here is my current example:
<input type="radio" ng-model="vm.radio" value="selected" /> Sample
<input type="text" ng-model="vm.input" ng-disabled="vm.radio='selected'" />
When input have the value, it just disabled and keep the input value.
I can do check inside the controller like
if(vm.radio=='selected') { vm.input = null }
But it only work when I active the function via the button. Is there any good way to handle it at HTML page, on the change event? Not in a controller script.
take a look at this plnkr: https://plnkr.co/edit/IygRaPlBmLzdnI0jhAQn?p=preview
You can use ng-change specifying an expression
<input type="radio" ng-model="vm.radio" value="selected" ng-change="vm.radio === 'selected' ? vm.input = '' : '' " /> Sample
<input type="radio" ng-model="vm.radio" value="b" /> Sample2
<input type="text" ng-model="vm.input" ng-disabled="vm.radio === 'selected'" />

ng-invalid after selecting date from datepicker

ng-invalid after selecting date from datepicker, but becomes valid if typed. Please help.
I worked around this by hiding the datepicker and showing an empty input text field when the value is null and then swapping/showing the datepicker when it is clicked.
<input
ng-if="my_date || set_my_date"
type="text"
id="my_date"
name="my_date"
datetime-picker="MM/dd/yyyy h:mma"
is-open="my_date"
ng-focus="set_my_date = true"
class="form-control"
ng-model="my_date"
placeholder="my date"
ng-readonly="true"
ng-required="true"
/>
<input
ng-if="!my_date && !set_my_date"
class="form-control"
placeholder="my date"
ng-click="set_my_date = true"
/>
ng-invalid class is added from angular when a required field is invalid (e.g empty), make sure the ng-model relative field , formvalue.workshopeDate, is correctly populated when you trigger the click.

ng-model binding with item id

case:
I need to bind an input field with a specific hidden field, and I want to refer to it by ID because I have many of those input vs hidden input in my page.
Something like this:
HTML:
<input type="text" ng-model="item.name-{{item.ID}}" >
<input type="hidden" name="item_name" value="{{item.name-{{item.ID}}}}" >
Item will be an object, where it looks like this:
$scope.item = {ID: '11111', name:'itemname'}
The item name can be edited through this input field where we will insert the name and it will be binded to a hidden field to be submitted with the form.
Thank you.
You can create another object for storing key-value pairs with data from your dynamic inputs. I think this approach is more cleaner.
<input type="text" ng-model="itemsValues[firstItem.name + '-' + firstItem.ID]" >
<input type="hidden" name="item_name" value="{{itemsValues[firstItem.name + '-' + firstItem.ID]}}" >
Demo on plunker (with three inputs).

Input and checkbox validation using AngularJS

I have a page with a check box representing "All dates" and two input fields which represent the start date and end dates. When the page first loads the check box is unchecked. The requirement is if the check box is unchecked and there is nothing in the start and end date fields a message must display to the user.
This is what I have for the check box and input fields and the error message:
<input name="dateSelectAll" type="checkbox" ng-model="$parent.vm.selectAllDates" ng-required="$parent.vm.selectedReport.Parameters.StartDate == null || $parent.vm.selectedReport.Parameters.EndDate == null" />All Available Dates
<input name="beginningDate" type="text" class="form-control form-field" datepicker-popup="dd-MMM-yyyy"
ng-disabled="$parent.vm.selectAllDates"
ng-model="$parent.vm.selectedReport.Parameters.StartDate"
is-open="beginningDateOpened"
ng-required="$parent.vm.selectAllDates == false"
close-text="Close" />
<input name="endDate" type="text" class="form-control form-field" datepicker-popup="dd-MMM-yyyy"
ng-disabled="$parent.vm.selectAllDates"
ng-model="$parent.vm.selectedReport.Parameters.EndDate"
is-open="endDateOpened"
ng-required="$parent.vm.selectAllDates == false"
close-text="Close" />
Please select
This is what I have in the controller at the beginning:
vm.selectAllDates = false;
This is what I have in the submit function:
if ($scope.reportForm.$valid) {
//do stuff
}
else
{
$scope.reportForm.submitted = true;
}
If the form is "valid" when I hit the submit button a modal window will display.
What's happening is the page loads and if I don't enter in dates or check the check box and I hit submit, the message appears and I can't submit which is fine.
When I check off the check box the message stays but I can submit.
When I uncheck the check box and enter dates the message stays but I can submit.
How do I hide the message once any of the conditions have been met? Sorry! I'm still a newbie at Angular.
I'm not sure this is "best practice", but you could use your form's validity to show/hide the message (since you've already setup the validation conditions).
You should be able to use something like
<p ng-show='reportForm.$valid'>Error Message</p>
Alternatively, you can manually setup a $watch on your form's validity to do any other logic you might need
$scope.$watch('reportForm.$valid', function(isValid) {
//change some value to show/hide the message
//any other logic for a change in validity
});
Okay, I figured it out.
For the checkbox I put this:
<input name="dateSelectAll" type="checkbox" ng-model="$parent.vm.selectAllDates" ng-required="vm.selectedReport.Parameters.StartDate == null && vm.selectedReport.Parameters.EndDate == null" />All Available Dates
For the start date I put this:
<input name="beginningDate" type="text" class="form-control form-field" datepicker-popup="dd-MMM-yyyy"
ng-disabled="$parent.vm.selectAllDates"
ng-model="$parent.vm.selectedReport.Parameters.StartDate"
is-open="beginningDateOpened"
ng-required="$parent.vm.selectAllDates == false"
close-text="Close" />
For the end date I put this:
<input name="endDate" type="text" class="form-control form-field" datepicker-popup="dd-MMM-yyyy"
ng-disabled="$parent.vm.selectAllDates"
ng-model="$parent.vm.selectedReport.Parameters.EndDate"
is-open="endDateOpened"
ng-required="$parent.vm.selectAllDates == false"
close-text="Close" />
And for the message I put this:
<div ng-show="reportForm.submitted || reportForm.dateSelectAll.$touched || reportForm.beginningDate.$touched
|| reportFrom.endDate.$touched">
<span class="error" ng-show="reportForm.dateSelectAll.$error.required || reportForm.beginningDate.$error.required || reportFrom.endDate.$error.required">Please select a Dissolved date</span>
</div>
I kept the code in the controller the same as what I put in my original post.
Thanks #ryanyuyu for answering so quickly. And thanks #DrCord for the info. I'll have to remember that!

Why doesnt the ng-show obey the field.$invalid condition

I need a form to show an error when it is dirty and is invalid according to a regex pattern how ever this does not seem to work. even though the invalid class is there when checked from inspect element
<form name="billingAddress">
<input type="text" ng-model="billingAddress.firstName" name="firstName" ng-pattern="nameRgx" required/>
<label ng-show="billingAddress.firstName.$invalid && billingAddress.firstName.$dirty">Invalid</label>
</form>
plunkr
please check this plunker i fixed your problem.
you need different form name like,
<form name="billingAddressx">
<label ng-show="billingAddressx.firstName.$invalid || billingAddressx.firstName.$error.required">Invalid</label>
here billingAddressx.firstName.$invalid check invalid state of the input by considering
form name = billingAddressx
input name = firstName

Resources