<![CDATA[ FormIt.Validators: Custom Validator one of Many - My Forums]]> https://forums.modx.com/thread/?thread=98810 <![CDATA[FormIt.Validators: Custom Validator one of Many]]> https://forums.modx.com/thread/98810/formit-validators-custom-validator-one-of-many#dis-post-534306
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>
<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>
<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>
<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>

[[!+fi.error.attendNothing:notempty=`
	<div class="field-msgs">
		<span class="error">
			[[!+fi.error.attendNothing]]
		</span>
	</div>
`]]


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
<?php

$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:
http://forums.modx.com/thread/92188/validation---two-li-039-s-with-checkboxes-one-must-be-chosen
https://gist.github.com/bertoost/4000623

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?

Best
Jan]]>
janwidmer Nov 12, 2015, 09:08 AM https://forums.modx.com/thread/98810/formit-validators-custom-validator-one-of-many#dis-post-534306
<![CDATA[Re: FormIt.Validators: Custom Validator one of Many (Best Answer)]]> https://forums.modx.com/thread/98810/formit-validators-custom-validator-one-of-many#dis-post-536351
<?php

//$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;
}

if(!$success){
	$validator->addError('attendNothing','Eine der Optionen muss ausgewählt werden');
}

return $success;
]]>
janwidmer Dec 28, 2015, 01:25 PM https://forums.modx.com/thread/98810/formit-validators-custom-validator-one-of-many#dis-post-536351
<![CDATA[Re: FormIt.Validators: Custom Validator one of Many]]> https://forums.modx.com/thread/98810/formit-validators-custom-validator-one-of-many#dis-post-534335 janwidmer Nov 13, 2015, 02:54 AM https://forums.modx.com/thread/98810/formit-validators-custom-validator-one-of-many#dis-post-534335 <![CDATA[Re: FormIt.Validators: Custom Validator one of Many]]> https://forums.modx.com/thread/98810/formit-validators-custom-validator-one-of-many#dis-post-534310
&customValidators=`requiredOneOfMany`
]]>
Bruno17 Nov 12, 2015, 11:14 AM https://forums.modx.com/thread/98810/formit-validators-custom-validator-one-of-many#dis-post-534310