Cụ thể hơn là như trong một form ta có hai fields là email và phone mà hệ thống của ta chỉ yêu cầu bắt buộc phải có ít nhất một trong hai thông tin đó để liên lạc vơi khách hàng. Nếu như khách hàng không nhập bắt kì thông tin nào thì ta sẽ báo lỗi require field. Nếu như nhập email và bỏ trống phone hoặc nhập phone mà bỏ trống email thì thông tin đó là hợp lệ. Vậy trong trường hợp này ta sẽ validate nó như ví dụ bên dưới.
<form name="myForm"> <input type="text" ng-model="fields.one" name="firstField" ng-required="!(fields.one.length || fields.two.length || fields.three.length)" /> <input type="text" name="secondField" ng-required="!(fields.one.length || fields.two.length || fields.three.length)" ng-model="fields.two" /> <input type="text" ng-model="fields.three" name="thirdField" ng-required="!(fields.one.length || fields.two.length || fields.three.length)" /> <button type="submit" ng-disabled="!myForm.$valid">Submit</button> </form>2. Tham khảo
https://stackoverflow.com/questions/24227903/form-validation-required-one-of-many-in-a-group
No comments:
Post a Comment