Disabling a field when user selects a radio button

Disabling a field when user selects a radio button

caneco2013caneco2013 Posts: 6Questions: 0Answers: 0
edited August 2013 in Editor
Hello. Is there a way to disable a field or fields when a user clicks a checkbox or radio button or even choosing an option in a dropdown box/select field? It seems I can't find any similar discussion here in the forum.

Here is that code that I am having a problem right now:

[code]
Field::inst( 'basement_student_room' )

->validator( 'Validate::required' ),

Field::inst( 'basement_condition' )

->validator( 'Validate::required' ),
[/code]

'basement_student_room' and 'basement_condition' are both radio buttons. 'basement_student_room' is a "Yes" and "No" radio buttons while 'basement_condition' is a set of typical basement conditions like musty etc. Now, what I want to do is disable 'basement_condition' whenever 'basement_student_room' returns a "No" value.

Now what I am thinking right now is that I need to create a custom validation that will handle such scenario arriving to this kind of idea:

[code]
Field::inst( 'basement_student_room' )

->validator( function($val){
if($val == 0){
'basement_condition' == disabled;
}
else{
'basement_condition' == enabled;
}

}),
Field::inst( 'basement_condition' )

->validator( ),
[/code]

Am I on the right track here?

Replies

  • caneco2013caneco2013 Posts: 6Questions: 0Answers: 0
    can someone please help me with this?
  • allanallan Posts: 61,657Questions: 1Answers: 10,094 Site admin
    Do you want to disable it on the client-side (user interface - Javascript), or on the server-side (PHP)? If you want it only in PHP then, yes looks like a valid approach, but you might be better of doing it on the client-side (possibly in addition to your validation).

    You can use the `node()` method to get the radio fields so you can attach event handlers and the `enable()` and `disable()` methods to enable and disable the sub-field as needed. For example:

    [code]
    $( 'input', editor.node( 'basement_condition' ) ).on( 'change', function () {
    if ( editor.get( 'basement_condition' ) == 0 ) {
    editor.disable( 'base_student_room' );
    }
    else {
    editor.enable( 'base_student_room' );
    }
    } );
    [/code]

    Allan
This discussion has been closed.