Javier Larios
Javier Larios

Reputation: 312

AngularJS - Form Custom Validation - Check if at least one input is filled

Good morning everyone,

Its possible create a directive (validation) to form level?

what I need is the following:

I have a form which has several fields and also has a button that is disabled while the validation of each of the fields is failed.

I need to create another validation, to check that at least one field of the form is filled.

I found one similar question here: link, but the solution is not the best (i think)

Thank you in advance

Upvotes: 2

Views: 2052

Answers (1)

Shawn C.
Shawn C.

Reputation: 6841

My simple answer without knowing more details would be to add a simple function to your controller to check to see if it is valid.

For example:

$scope.isValid = function isValid(){
    return (field1 || field2 || field3 || undefined) !== undefined;
}

Then update your ng-disabled to include it.

ng-disabled="your_form.$invalid || !isValid()"

With more details I could get a better answer.

Upvotes: 2

Related Questions