xPDOMaxValueValidationRule
Last updated Oct 20th, 2026 | Page history | Improve this page | Report an issue
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$194 per month—let's make that $500!
Learn moreWhat Does the Rule Do?¶
This rule compares the numeric value of a field with a maximum given by the value attribute in the schema.
[!CAUTION]
xPDOMaxValueValidationRulenever fails validation in xPDO 3.x. ItsisValid()method computes the comparison and adds the message, but does not return the result, so the rule scores as passed.xPDOValidator::validate()returnstrueeven when the maximum is exceeded.The message is still written to
getMessages(), so a caller that inspects the messages unconditionally sees it, while a caller that only checks the return value ofvalidate()does not. Use thecallablerule below instead if you need a maximum.
Without value, the maximum defaults to 0.
Working Alternative: a callable Rule¶
Declare the maximum as a callable rule, which returns its result correctly:
<model package="Test" baseClass="xPDO\Om\xPDOObject" platform="mysql"
defaultEngine="InnoDB" version="3.0">
<object class="myTest" table="test" extends="xPDO\Om\xPDOSimpleObject">
<field key="number" dbtype="int" precision="10"
phptype="integer" default="0" null="false" />
<validation>
<rule field="number"
name="myMaxValRule"
type="callable"
rule="StoreFinder\Model\Validation\myMaxValue"
value="100"
message="The number cannot be greater than 100."
/>
</validation>
</object>
</model>
The rule attribute must be fully qualified, because a bare name is resolved against the xPDO\Validation\ namespace.
<?php
namespace StoreFinder\Model\Validation;
class myMaxValue {
public function __invoke($value, $parameters) {
return $value <= (int) $parameters['value'];
}
}
Generate the model from the XML schema, then validate an object in a Snippet:
$output = '';
$modx->addPackage('Test', '/path/to/components/test/model/');
$obj = $modx->newObject('Test\myTest');
$obj->set('number', 101);
$validator = $obj->getValidator();
if (!$validator->validate()) {
foreach ($validator->getMessages() as $errorMsg) {
$output .= $errorMsg['message'];
}
}
return $output;
This will display:
The number cannot be greater than 100.
See Also¶
- xPDOForeignKeyConstraint
- xPDOMaxLengthValidationRule
- xPDOMaxValueValidationRule
- xPDOMinLengthValidationRule
- xPDOMinValueValidationRule
- xPDOObjectExistsValidationRule
Support the team building MODX with a monthly donation.
The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.
Backers
Budget
$194 per month—let's make that $500!
Learn more










