Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Validation] Validating Radio buttons #2501

Open
dwightbcoder opened this issue Jul 2, 2015 · 11 comments
Open

[Validation] Validating Radio buttons #2501

dwightbcoder opened this issue Jul 2, 2015 · 11 comments

Comments

@dwightbcoder
Copy link

It appears form validation does not include support for validating radio buttons so I have come up with a solution.

Nest your radio buttons into a field container with your default hidden input with an id you can target for validation:

<form class="ui form">
    <div class="field">
        <input type="hidden" name="confirm" id="confirm_default" value="">
        <div class="grouped fields">
            <div class="field">
                <div class="ui radio checkbox">
                    <input type="radio" name="confirm" tabindex="0" class="hidden">
                    <label>Yes</label>
                </div>
            </div>
            <div class="field">
                <div class="ui radio checkbox">
                    <input type="radio" name="confirm" tabindex="0" class="hidden">
                    <label>No</label>
                </div>
            </div>
        </div>
    </div>
</form>

Target your validation rules at the hidden input id:

validationRules = {
    confirm:
    {
        identifier : 'confirm_default',
        rules:
        [
            {
                type   : 'empty',
                prompt : 'Please confirm'
            },
            {
                type   : 'not[0]',
                prompt : 'You must confirm'
            }
        ]
    }
};

Update the hidden input of the radio set with the checked value:

$( 'form' )
    .form( validationRules, validationSettings )
    .on( 'change', '[type=radio]', function(e)
    {
        var name   = $( this ).attr( 'name' ),
            hidden = $( '[type=hidden][name='+name+']' );

        hidden.val( $(this).val() );
    });
@jlukic
Copy link
Member

jlukic commented Jul 3, 2015

Does checked validation not work for you?

@dwightbcoder
Copy link
Author

When using inline validation, checked causes multiple prompts on every matched input.

For a simple two choice radio selection with a default post value (hidden input) that results in three identical prompts, one for each radio and one for the hidden input.

This method, while far more cumbersome, allows a single error prompt.

@bryanlittlefield
Copy link

@jlukic I am having the same issue, 'checked' is not recognizing the group radios not being checked.

HTML:

<div class="inline field">
    <div class="ui radio checkbox">
        <input name="invoice_type" type="radio" value="down-payment">
        <label for="grand-total">Grand Total</label>
    </div>
    <div class="ui radio checkbox">
        <input name="invoice_type" type="radio" value="down-payment">
        <label for="name">Category Totals</label>
    </div>
    <div class="ui radio checkbox">
        <input name="invoice_type" type="radio" value="down-payment">
        <label for="name">Line Item Totals</label>
    </div>                
</div>

jQuery:

(function( $ ) {
    $(function() {
        //Semantic UI Form Validation Job - Create Invoice  =========================
        $('.ui.form.create-invoice-form').form(
            {   
                invoiceType: {
                    identifier  : 'invoice_type',
                    rules: [                       
                        {
                            type   : 'checked',
                            prompt : 'Specify the type of invoice that you would like to create'
                        },                          
                    ]                    
                }                                                             
            },
            {
                inline : true,
                on     : 'blur',                   
            }                   
        );
    });
})(jQuery);

@jlukic jlukic added this to the 2.1 milestone Jul 12, 2015
@jlukic
Copy link
Member

jlukic commented Jul 20, 2015

For anyone posting code samples please make a JSFiddle and not just copy and paste code here. Otherwise I'm manually creating test cases for every bug I'm trying to evaluate & fix.

It looks like there is a regression with #2505 as well..

This appears to be functioning correctly, albeit with peculiar formatting due to #2505 regression
http://jsfiddle.net/ebcn8xvq/

@douglasg14b
Copy link

There still seems to be some formatting issues with radio button validation when using inline errors. JSFiddle: http://jsfiddle.net/ebcn8xvq/2/

@dopeboy
Copy link

dopeboy commented Jan 16, 2016

Thanks @Aproducktion! I used this technique for a group of checkboxes where atleast one needed to be checked.

@jlukic
Copy link
Member

jlukic commented Jun 28, 2016

I see this is specifically an issue with inline: true and radio

@jlukic jlukic reopened this Jun 28, 2016
@jlukic jlukic modified the milestones: 2.2.1, 2.0.5 Jun 28, 2016
@jlukic jlukic modified the milestones: 2.2.3, 2.2.4 Aug 22, 2016
@jlukic jlukic modified the milestones: 2.2.5, 2.2.6, 2.2.7 Oct 28, 2016
@jlukic jlukic modified the milestones: 2.2.7, 2.2.6 Oct 28, 2016
@jlukic jlukic changed the title Validating Radio buttons [Form] Validating Radio buttons Feb 20, 2017
@jlukic jlukic changed the title [Form] Validating Radio buttons [Form Validation] Validating Radio buttons Feb 20, 2017
@jlukic jlukic changed the title [Form Validation] Validating Radio buttons [Validation] Validating Radio buttons Feb 21, 2017
@jlukic jlukic modified the milestones: 2.2.8, 2.2.9 Feb 21, 2017
@jlukic jlukic modified the milestones: 2.2.10, 2.2.11 Mar 28, 2017
@jlukic jlukic modified the milestones: 2.2.11, 2.2.12 Jul 11, 2017
@jlukic jlukic modified the milestones: 2.2.12, 2.2.13 Aug 6, 2017
@jlukic jlukic modified the milestones: 2.2.15, 2.2.16 Jan 29, 2018
@krisztianodor
Copy link

What's up with the fix of this problem?

@jlukic jlukic modified the milestones: 2.3.1, 2.3.3 Mar 19, 2018
@iROCKBUNNY
Copy link

Any progress?

@jameshibbard
Copy link

jameshibbard commented Mar 26, 2019

@dwightbcoder - thanks for the snippet.

It would be nice to be able to add an additional validation call to the change handler, then the error message will disappear, as soon as a user selects an option (otherwise it stays until the form is submitted).

.on('change', '[type=radio]', function(e) {
  const name = $(this).attr('name');
  const  hidden = $(`[type=hidden][name='${name}']`);
  hidden.val($(this).val());
  $('.ui.form').form('validate field', this.name);
});

This works for me. It relies on the field having the same name as the radio inputs.

@yehudasmilowitz
Copy link

The validation on radio buttons seems to be working for me, as long as the type is set to 'checked' in the rules

validationRules = {
    confirm:
    {
        identifier : 'confirm',
        rules:
        [
            {
                type   : 'checked',
                prompt : 'Please confirm'
            }
        ]
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants