xPDOObjectExistsValidationRule
Last updated Apr 12th, 2019 | 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
$306 per month—let's make that $500!
Learn moreWhat Does the Rule Do?¶
This rule checks to see if another object exists. If that object doesn't exist, then the validation on this current object fails. The object can be specified by stating the "classKey" and "pk" values in the rule schema definition.
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" />
<validation>
<rule field="name"
name="chunkExists"
type="xPDOValidationRule"
rule="xPDOObjectExistsValidationRule"
pk="12"
classKey="modChunk"
message="The Chunk does not exist, so this object cannot be saved."
/>
</validation>
</object>
</model>
From there, go ahead and generate the model from the XML schema. And now in a Snippet we'll call Test:
$output = '';
$modx->addPackage('test','/path/to/my/test/model/','test_');
$obj = $modx->newObject('myTest');
$validator = $obj->getValidator();
if ($validator->validate() == false) {
$messages = $validator->getMessages();
foreach ($messages as $errorMsg) {
$output .= $errorMsg['message'];
}
}
This will display:
The Chunk does not exist, so this object cannot be saved.
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
$306 per month—let's make that $500!
Learn more