material icon indicating copy to clipboard operation
material copied to clipboard

Validating radio buttons in md-radio-group?

Open demisx opened this issue 11 years ago • 17 comments

Hi. I am not clear on how to make a radio group required. Taking this as an example:

<md-radio-group ng-model="vm.color" name="color">
  <md-radio-button value="1">
    Blue
  </md-radio-button>
  <md-radio-button value="2">
    White
  </md-radio-button>
</md-radio-group>

I've tried adding reqiured to the group tag and individual button tags, but nothing happens. Thank you.

demisx avatar Feb 10 '15 17:02 demisx

Bump. Do you guys know of a way to make an md radio button group required?

demisx avatar Feb 11 '15 22:02 demisx

Adding ng-required="true" to md-radio-group makes the radio group required:

<md-radio-group ng-required="true" ng-model="vm.color" name="color">
  <md-radio-button value="1">
    Blue
  </md-radio-button>
  <md-radio-button value="2">
    White
  </md-radio-button>
</md-radio-group>

Though, as mentioned in another post, not clear how to show the error message for it, since ng-messages aren't triggered until a user interacts with an input field and once he/she does, it's no longer invalid. I would like to provide clear visual guidance, so a user knows that the reason the submit button is still disabled, because he/she didn't select the radio button. Currently, the radio button group is just silently in invalid state. Any clues are appreciated.

demisx avatar Mar 04 '15 02:03 demisx

Bump. Would love to see a solution for showing ng-messages for required radios.

davidtmnolan avatar Mar 11 '15 16:03 davidtmnolan

We need to take a look at the required style for radio groups. Adding to our list of UX questions.

marcysutton avatar Apr 17 '15 22:04 marcysutton

@marcysutton - can you elaborate here what the UX Questions are for RadioGroup validations?

ThomasBurleson avatar Apr 18 '15 15:04 ThomasBurleson

The current proposal to the UX Design team is a solution where a message can appear below with the checkbox/radio turning red.

ThomasBurleson avatar Apr 22 '15 18:04 ThomasBurleson

@demisx Is ng-required="true" stopping forms from submitting for you? I tried that and several other ways to require it and nothing stopped the form.

epelc avatar May 05 '15 17:05 epelc

@epelc As I recall, the issue here was mainly that it wasn't clear how to deal with validation and displaying error message for the md-radio-group.

demisx avatar May 05 '15 17:05 demisx

+1 cant validate required with md-radio-group

stewones avatar May 11 '15 14:05 stewones

+1 Same issue md-radio-group +ng-requiered and ng-message

diegolaciar avatar Jul 18 '15 15:07 diegolaciar

For me the solution was set the attribute name="someName" to the md-radio-group and then on ng-messages

diegolaciar avatar Jul 18 '15 15:07 diegolaciar

I use ng-class, like this (inside my directive):

    <td style="width:700px;"><span ng-class="condition.cls">{{condition.questions}}</span></td> 
    <td><md-radio-button value="yes">yes</md-radio-button></td>
    <td><md-radio-button value="no">no</md-radio-button></td>

Then, I use an object for my questions, like this:

       $scope.conditions = [
            {questions: 'will alcohol be sold?', answers: navigatorService.q[0], cls: ''},
            {questions: 'will alcohol be served?', answers: navigatorService.q[1], cls: ''}
            _and so on.  (you probably don't need the navigatorService thing.  I'm just using that to access info in another controller.)_
        ];

and this is the stuff that makes it work:

        $scope.processConditions = function () {
            $scope.validation();
            $scope.navigation();
        };

    $scope.validation = function () {
        for (var i=0;i<$scope.conditions.length;i++) {
            if ($scope.conditions[i].answers != undefined) {
                $scope.conditions[i].cls = '';
            } else {
                $scope.conditions[i].cls = 'radio-input-invalid';
            }
        }
    };

    $scope.navigation = function () {
        var proceed = true;
        for (var i=0;i<$scope.conditions.length;i++) {
            if (!$scope.conditions[i].answers) { proceed = false; }
        }
        if (proceed != false) {
            navigatorService.storage($scope.conditions);
            $state.go("portal.newEvent.step1");
        }
    };

and finally, radio-input-invalid just makes the associated text turn red: (and again, you probably can ignore the transition stuff, which just eases the change in color.)

md-radio-button.md-default-theme .md-on {
  background-color:#14a014;
}
md-radio-button.md-default-theme.md-checked .md-off {
  border-color:#14a014;
}
.radio-input-invalid-add, .radio-input-invalid-remove {
  -webkit-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.2s;
  -moz-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.2s;
  -o-transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.2s;
  transition:all cubic-bezier(0.250, 0.460, 0.450, 0.940) 0.2s;
}
.radio-input-invalid,
.radio-input-invalid-add.radio-input-invalid-add-active {
  color:#d82b00;_  // this is the red color, here._
}
.radio-input-invalid-remove.radio-input-invalid-remove-active {
  color:dimgray;
}

I hope this helps someone to apply validation to their radio buttons while waiting for a real fix.

PhilipCurra avatar Feb 19 '16 22:02 PhilipCurra

This issue is closed as part of our deprecation effort. For details, see our post Spring Cleaning 2016.

ThomasBurleson avatar Apr 28 '16 20:04 ThomasBurleson

This is still an issue.

rvarbanov avatar May 19 '16 19:05 rvarbanov

+1

jpstone avatar Jun 21 '16 13:06 jpstone

@diegolaciar do you have a working pen or fiddle for your example with name="someName" ?

gaurav5430 avatar Sep 15 '16 06:09 gaurav5430

@gaurav5430 No I don't. just posted that solution.. more than a year ago..

diegolaciar avatar Sep 15 '16 13:09 diegolaciar