On March 26, 2019 we launched new MODX Forums. Please join us at the new MODX Community Forums.
Subscribe: RSS
  • Hi There,

    I am trying to build a custom validator that checks, if one of several checkboxes is checked. The idea is, that I can provide a string to the validator and the validator iterates over all fields and only considers fields containing the provided string in their key.

    My code so far looks like this:

    Form Template
    <div class="form-item">
    	<input type="hidden" value="" name="attendNothing" />
    	<input type="checkbox" id="attend-nothing" [[+fi.attendNothing:notempty=`checked="checked"`]] value="Ja" name="attendNothing">
    	<label for="attend-nothing">können leider nicht teilnehmen</label>
    <div class="form-item">
    	<input type="hidden" value="" name="attendCeremony" />
    	<input type="checkbox" id="attend-ceremony" [[+fi.attendCeremony:notempty=`checked="checked"`]] value="Ja" name="attendCeremony">
    	<label for="attend-ceremony">nehmen an der Trauungszeremonie teil</label>
    <div class="form-item">
    	<input type="hidden" value="" name="attendReception" />
    	<input type="checkbox" id="attend-reception" [[+fi.attendReception:notempty=`checked="checked"`]] value="Ja" name="attendReception">
    	<label for="attend-reception">nehmen am Apero teil</label>
    <div class="form-item">
    	<input type="hidden" value="" name="attendParty" />
    	<input type="checkbox" id="attend-party" [[+fi.attendParty:notempty=`checked="checked"`]] value="Ja" name="attendParty">
    	<label for="attend-party">nehmen am Essen / Fest teil</label>
    	<div class="field-msgs">
    		<span class="error">

    FormIt Code
    $scriptProperties['customValidators'] = 'requiredOneOfMany';
    $scriptProperties['validate'] = 'attendNothing:requiredOneOfMany';
    $scriptProperties['attendNothing.vTextRequiredOneOfMany'] = 'One Attend Option needs to be choosen';
    $modx->runSnippet('FormIt', $scriptProperties);

    Custom Validator Code
    $fields = $modx->getOption('fields',$scriptProperties,'');
    $fields = explode(',',$fields);
    $success = false;
    foreach ($fields as $field){
    	// only check fields who's key contains the string "attend"
    	if(strpos($field->$key,'attend') >= 0){
     		$fieldrequest = $modx->getOption($field, $_REQUEST);
    	  	if (!empty($fieldrequest)){
    		  	$success = true;
    return $success;

    I searched the forum and my code is inspired from this posts:

    It seems , that the custom validator is not getting called at all, since just returning false does not do anything at all (No validation erros).

    Any Ideas what I am doing wrong?


    This question has been answered by janwidmer. See the first response.

    [ed. note: janwidmer last edited this post 5 years, 10 months ago.]
    • do you have


        you can buy me a beer, if you like MIGX


      • oh yeah, sorry, I forgot to paste it into the code snippet. I updated the snippet.
        • discuss.answer
          I ended up with this validator which is working, but not as dynamically, as I originally intended..

          //$modx->log(modX::LOG_LEVEL_ERROR, 'in my validator' ); // just to be sure you fire the validator
          $fields = $validator->fields;
          $success = false;
          if($fields['attendNothing'] == 'Ja' || $fields['attendCeremony'] == 'Ja' || $fields['attendReception'] == 'Ja' || $fields['attendParty'] == 'Ja'){
          	//$modx->log(modX::LOG_LEVEL_ERROR, 'At least one field is set');
          	$success = true;
          	$validator->addError('attendNothing','Eine der Optionen muss ausgewählt werden');
          return $success;