We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 41642
    • 10 Posts
    Hello !

    I have several checkbox and i would like required one box minimum... It's possible ?

    If mycheckbox.name1 is checked OR mycheckbox.name2 is checked ... etc


    Thank's a lot ! [ed. note: beejeridou last edited this post 13 years, 11 months ago.]
      • 23018
      • 353 Posts
      Welcome beejeridou to MODx. I hope you enjoy your stay!

      Use a validator (http://rtfm.modx.com/display/ADDON/FormIt.Validators):

      [[!FormIt? &validate=`colors:required`]]
      // "colors" is the name of the field you want to check
      
      


      Unfortunatly I'm not sure :required works with arrays. If this is not the case, create your own custom validator instead (untested...):

      [[!FormIt? 
         &customValidators=`checkColors` 
         &validate=`colors:checkColors` 
      ]]
      


      Create a new snippet:

      <?php
          
          /* Snippet: "checkColors" */
          
          // Set to $debug = "0" if everything works as expected. If not, check your error messages for further information.
          $debug = "1";
      
          $fields = $hook->getValues();
          
          $colors = $fields['colors'];
          
          $number = count($colors);
          
          $success = $number > 0;
      
          if($debug == "1")
          {    
              foreach($colors as $key => $value)
              {
                  $values .= $value."/";
              }
              
              $modx->log(xPDO::LOG_LEVEL_ERROR,'Number of colors: '.$number.'('.$values.')') ;
          }
          
          if($success)
          {
              return true;
          }
          else
          {
              $errorMsg = 'Sorry, you have to choose at least one favourite color.';
              $hook->addError('colors',$errorMsg);
              return false;
          }
      


      Regards,

      pepebe

      BTW: I just noticed that the whole thing is explained in the second half of the formit.validator documentation: http://rtfm.modx.com/display/ADDON/FormIt.Validators

      [ed. note: pepebe last edited this post 13 years, 11 months ago.]
        Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe
        • 41642
        • 10 Posts
        Great solution ! it's perfect !

        Thank you for that and for my welcome in modx ! wink
          • 23018
          • 353 Posts
          Your welcome,

          Please add Solved: to the title of your first post and come back to the forum if you have further questions. You'll find out that there are a lot of nice (and much more knowledgeable) people around here. smiley

          Regards,

          BTW Which one was the right one?
            Homepage: pepebe.de | MODX snippets (and other stuff) at github: https://gist.github.com/pepebe