FormIt, Custom Validator does not validate

Dear community members,
I am evaluating FormIt in order to migrate an old Etomite 0.6 site to MODX. I carefully read docs.modx.com, The Official Guide as well as numerous community posts. Unfortunately I did not find a solution yet - the issue is custom validator is not triggered at all.

I’m an IT pro and my environment is:
WAMP; PHP 7.1.33, mySQL 5.7.14
Eclipse for PHP Developers Oxygen.3a Release (4.7.3a), with PHP Development Tools 5.3.0.201803070838 (supports PHP versions 5.1 up to 7.4).
MODX 2.7.3 with FormIt 4.2.6.

Here the resource document

code

[[!FormIt?
&validate=`name:required,
age:required:isNumber:minValue=^14^:maxValue=^120^,
department:required`
&customValidators=`department:allowedDepartments=^Procurement, Warehouse, Sales^`
]]
<h2>Profile</h2>
<form action="[[~[[*id]]]]" method="post" class="form">
<p class="form"><label for="name">Name:<span class="error">[[!+fi.error.name]]</span></label>
<input type="text" name="name" id="name" value="[[!+fi.name]]" placeholder="e.g. Christine Doe (letters, dashes only)" />
<br>
</p><p></p>
<p class="form"><label for="age">Age:<span class="error">[[!+fi.error.age]]</span></label>
<input type="text" name="age" id="age" value="[[!+fi.age]]" placeholder="e.g. 35 (numbers only)" />
<br>
</p><p></p>
<p class="form"><label for="department">Department:<span class="error">[[!+fi.error.department]]</span></label>
<input type="text" name="department" id="department" value="[[!+fi.department]]" placeholder="e.g. Procurement (department names only)" />
<br>
</p><p></p>
<br class="clear" />
<div class="form-buttons">
<input type="submit" value= "Send Department Member" />
</div>
</form>

Please use triple backticks (```), on their own line, to start and end a code block. :slight_smile:

The problem seems to be that you’re mixing up what goes in &validator and what goes in &customValidators.

As described in this section of the docs, you first specify the name of a snippet in &customValidators to make it available. Then, in &validate, you apply it to a certain field. Granted, it’s kinda odd the documentation doesn’t show a single, full example.

So your formit call should look something like this assuming you’ve named your validator snippet allowedDepartments:

[[!FormIt?
	&validate=`name:required,
		age:required:isNumber:minValue=^14^:maxValue=^120^,
		department:required:allowedDepartments=^Procurement, Warehouse, Sales^`
	&customValidators=`allowedDepartments`
]]

@markh
many thx, snippet is now triggered, although it only positively validates the 1st item in the list i.e. Procurement, but Warehouse and Sales are reproted invalid, even when spaces after commas have been removed from list.
Here the snippet code which looks fine to me:

<?php
/* Code for allowedDepartments Snippet */
$departments = explode(',',$param);
if (in_array($value, $departments)) {
return true;
} else {
$validator->addError($key,
'Invalid Department Name.');
return false;
}

Hmm, the spaces are all I can think of why that wouldn’t work. Perhaps trimming the array values helps despite having removed them from the call?

$departments = explode(',',$param);
$departments = array_map('trim', $departments);

Due to no success with the method originally used, alternatively applied 2nd method as outlined in Official Guide and added list of valid dept names to snippet default properties, which finally solved the issue.

<?php
/* Code for allowedDepartments Snippet */
$departments = explode(',', $scriptProperties['legalDepts']);
if (in_array($value, $departments)) {
return true;
} else {
$validator->addError($key,
'Invalid Department Name.');
return false;
}

And due to now obsolete removed list from FormIt declaration i.e. from

&validate=`name:required,
age:required:isNumber:minValue=^14^:maxValue=^120^,
department:required:allowedDepartments=^Procurement, Warehouse, Sales^`

to

&validate=`name:required,
age:required:isNumber:minValue=^14^:maxValue=^120^,
department:required:allowedDepartments`

Note the no spaces after commas in the Snippet property value list

Procurement,Warehouse,Sales

var_dump($departments) to file showed indented values, removed spaces and the values “trimmed” properly then.

C:\wamp64\www\modx-tests-diabolo\core\cache\includes\elements\modsnippet\27.include.cache.php:6:
array (size=3)
  0 => string 'Procurement' (length=11)
  1 => string 'Warehouse' (length=9)
  2 => string 'Sales' (length=5)

Glad you got it working, and thanks for sharing back your solution! :slight_smile:

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.