I wrote a validator for a contactform and the problem was that errors were displayed twice. The Controller has a showAction for showing the form:
- /**
- * @param Contactform $contactform
- */
- public function showAction( $contactform = null) {
- $this->view->assign('contactform', $contactform);
- $this->view->assign('timestamp', time());
- }
with the following template:
- <f:form action="create" name="contactform" objectname="contactform" object="{contactform}">
- <f:render partial="FormErrors" arguments="{object:contactform}"/>
- <f:form.textfield id="name" property="name" name="name" required="true"/>
- <f:form.textfield id="email" property="email" name="email" required="true"/>
- ....
- </f:form>
This form is supposed to lead to the createAction, which looks like this:
- /**
- * @param Contactform $contactform
- * @validate $contactform \Package\Extension\Domain\Validator\ContactformValidator
- */
- public function createAction(Contactform $contactform){
- $contactform->setPid($GLOBALS['TSFE']->id); //set pid to current page ID
- $this->contactformRepository->add($contactform);
- //redirect to thank you page...
- }
And the ContactformValidator looks like this:
- <?php
- namespace Package\Extension\Domain\Validator;
- use Package\Extension\Domain\Model\Contactform;
- class ContactformValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
- public function isValid($contactform) {
- $isValid = true;
- if(!$contactform->getName()){
- $this->addError(
- $this->translateErrorMessage(
- 'tx_extension_domain_model_contactform.errors.required',
- 'extension'
- ), 111);
- $isValid = false;
- }
- if(!$contactform->getEmail()){
- $this->addError(
- $this->translateErrorMessage(
- 'tx_extension_domain_model_contactform.errors.required',
- 'extension'
- ), 111);
- $isValid = false;
- }
- return $isValid;
- }
- }
This works, except that every error is duplicated. I googled this problem for a long time until finally I discovered this link. The author says that validators, that are named DOMAINNAMEValidator, automatically validate a domain. This means my @validator annotation was obsolete. I removed it and then it worked.