0

I need to validate some fields based on values other fields have, within the same model. Since a custom validator only has access to the value it is validating, I can't check other validations there. From inspecting AbstractValidator, I couldn't find a possibility to reach that object the current value is validated.

Is there a solution to validate/add errors in a controller, set errors and render the actual view by keeping the original routine instead of introducing and assigning new objects to the view? Basically I could create a custom $errors var, fill it with errors after having done custom validations and the display it along with the original form errors. But I don't like that workaround approach.

pdu
  • 10,295
  • 4
  • 58
  • 95
  • For the ones interested in this, I also submitted a feature request, see http://forge.typo3.org/issues/45822 – pdu Feb 25 '13 at 14:28

1 Answers1

0

When you add a new model validator, you have access to the other fields of that model

File: test_extension/Classes/Domain/Validator/TestModelValidator.php:

class Tx_TestExtension_Domain_Validator_TestModelValidator extends Tx_Extbase_Validation_Validator_AbstractValidator {
    /**
     * @param Tx_TestExtension_Domain_Model_TestModel $testModel
     * @return boolean
     */
    public function isValid($testModel) {
        /** @var $testModel Tx_TestExtension_Domain_Model_TestModel */
        //Access all properties from $testModel
        $field1 = $testModel->getMyField1();
        $field2 = $testModel->getMyField2();
    }
}

You can also add errors to speific fields, but this code is from TYPO3 4.5, don't know if its still valid:

$error = t3lib_div::makeInstance('Tx_Extbase_Validation_Error', 'The entered value is allready in use.', 1329936079);
$this->errors['field2'] = t3lib_div::makeInstance('Tx_Extbase_Validation_PropertyError', 'field2');
$this->errors['field2']->addErrors(array($error));
Merec
  • 2,751
  • 1
  • 14
  • 21
  • AFAIK this works if you validate a record from an association, but if you e.g just want to validate a string field, you get the string value as argument for `isValid`, and you have no chance to get the model record from there. – pdu Feb 26 '13 at 15:47