Search code examples
formszend-framework2

Only require textfield when radio is selected. ZF2


In ZF2, is it possible to require a textfield only when a radio button is selected? like so..

enter image description here


Solution

  • No, There is no way in ZF2 for this, You can use jQuery of javascript to add the validation for this:

    Like this:

    $('radio').click(function(){
      if($(this).val() == 'other') {
         $('textfield').prop('required',true);
      }
    });
    

    Onclick on any radio button a client side event and zf2 runs on server side. So you cann't do this untill you are submitting the data to server.