Jump to main content Jump to doc navigation

What Does the Rule Do?

This rule verifies that a field has less than X number of chars, where X is defined by the "value" attribute in the XML schema.

Using the Rule

First, our model:

<model package="test" baseClass="xPDOObject" platform="mysql"
       defaultEngine="MyISAM" tablePrefix="test_">
    <object class="myTest" table="test" extends="xPDOSimpleObject">
        <field key="name" dbtype="varchar" precision="255"
               phptype="string" default="" null="false" />

            <rule field="name"
                  message="The name must be less than 10 characters."

From there, go ahead and generate the model from the XML schema. And now in a Snippet we'll call Test:

$output = '';
$obj = $modx->newObject('myTest');
$obj->set('name','This is a really long string that will fail.');
$validator = $obj->getValidator();
if ($validator->validate() == false) {
    $messages = $validator->getMessages();
    foreach ($messages as $errorMsg) {
        $output .= $errorMsg['message'];

This will display:

The name must be less than 10 characters.

See Also

  1. xPDOForeignKeyConstraint
  2. xPDOMaxLengthValidationRule
  3. xPDOMaxValueValidationRule
  4. xPDOMinLengthValidationRule
  5. xPDOMinValueValidationRule
  6. xPDOObjectExistsValidationRule