Validate Radio Button AngularJS

后端 未结 4 1888
Happy的楠姐
Happy的楠姐 2020-12-04 20:32

This seems like it should be fairly easy, but I\'m not finding the answer. I have a form where I need to validate that a selection has been made from a radio group. I trie

4条回答
  •  旧时难觅i
    2020-12-04 21:13

    Try using ng-required="!color". This makes it so that the field is only required when the value is not set. If a value is set, then required is removed and it will pass validation.

      Red 
    Green
    Blue

    Here is an updated plunker that demonstrates that the form now validates correctly: http://plnkr.co/edit/EdItU2IIkO1KIsC052Xx?p=preview

    Update

    e-cloud's answer is simple and doesn't require an additional directive. I suggest everyone use that method if possible. Leaving this answer here as it does provide a working solution and demonstrates the use of the ng-required directive.

提交回复
热议问题