Skip to content
36 changes: 21 additions & 15 deletions docs/content/cookbook/advancedform.ngdoc
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,31 @@ detection, and preventing invalid form submission.
<pre>form={{form}}</pre>
</div>
</doc:source>
<doc:scenario>
<doc:protractor>
var saveButton = element(by.css('[ng-click="save()"]'));
var cancelButton = element(by.css('[ng-click="cancel()"]'));
var nameInput = element(by.model('form.name'));

it('should enable save button', function() {
expect(element(':button:contains(Save)').attr('disabled')).toBeTruthy();
input('form.name').enter('');
expect(element(':button:contains(Save)').attr('disabled')).toBeTruthy();
input('form.name').enter('change');
expect(element(':button:contains(Save)').attr('disabled')).toBeFalsy();
element(':button:contains(Save)').click();
expect(element(':button:contains(Save)').attr('disabled')).toBeTruthy();
expect(saveButton.getAttribute('disabled')).toBeTruthy();
nameInput.clear();
nameInput.sendKeys('');
expect(saveButton.getAttribute('disabled')).toBeTruthy();
nameInput.sendKeys('change');
saveButton.click();
expect(saveButton.getAttribute('disabled')).toBeTruthy();
});

it('should enable cancel button', function() {
expect(element(':button:contains(Cancel)').attr('disabled')).toBeTruthy();
input('form.name').enter('change');
expect(element(':button:contains(Cancel)').attr('disabled')).toBeFalsy();
element(':button:contains(Cancel)').click();
expect(element(':button:contains(Cancel)').attr('disabled')).toBeTruthy();
expect(element(':input[ng\\:model="form.name"]').val()).toEqual('John Smith');
expect(cancelButton.getAttribute('disabled')).toBeTruthy();
nameInput.clear();
nameInput.sendKeys('change');
expect(cancelButton.getAttribute('disabled')).toBeFalsy();
cancelButton.click();
expect(cancelButton.getAttribute('disabled')).toBeTruthy();
expect(nameInput.getAttribute('value')).toEqual('John Smith');
});
</doc:scenario>
</doc:protractor>
</doc:example>


Expand Down
23 changes: 16 additions & 7 deletions src/ng/directive/form.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,18 +305,27 @@ function FormController(element, attrs) {
<tt>myForm.$error.required = {{!!myForm.$error.required}}</tt><br>
</form>
</doc:source>
<doc:scenario>
<doc:protractor>
it('should initialize to model', function() {
expect(binding('userType')).toEqual('guest');
expect(binding('myForm.input.$valid')).toEqual('true');
var userType = element(by.binding('userType'));
var valid = element(by.binding('myForm.input.$valid'));

expect(userType.getText()).toContain('guest');
expect(valid.getText()).toContain('true');
});

it('should be invalid if empty', function() {
input('userType').enter('');
expect(binding('userType')).toEqual('');
expect(binding('myForm.input.$valid')).toEqual('false');
var userType = element(by.binding('userType'));
var valid = element(by.binding('myForm.input.$valid'));
var userInput = element(by.model('userType'));

userInput.clear();
userInput.sendKeys('');

expect(userType.getText()).toEqual('userType =');
expect(valid.getText()).toContain('false');
});
</doc:scenario>
</doc:protractor>
</doc:example>
*/
var formDirectiveFactory = function(isNgForm) {
Expand Down
Loading