Quantcast
Channel: Insane in the Main Frame
Viewing all articles
Browse latest Browse all 310

Extbase Validator – Errormessages are duplicated

$
0
0

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:

  1. /**
  2. * @param Contactform $contactform
  3. */
  4. public function showAction( $contactform = null) {
  5. $this->view->assign('contactform', $contactform);
  6. $this->view->assign('timestamp', time());
  7. }

with the following template:

  1. <f:form action="create" name="contactform" objectname="contactform" object="{contactform}">
  2. <f:render partial="FormErrors" arguments="{object:contactform}"/>
  3. <f:form.textfield id="name" property="name" name="name" required="true"/>
  4. <f:form.textfield id="email" property="email" name="email" required="true"/>
  5. ....
  6. </f:form>

This form is supposed to lead to the createAction, which looks like this:

  1. /**
  2. * @param Contactform $contactform
  3. * @validate $contactform \Package\Extension\Domain\Validator\ContactformValidator
  4. */
  5. public function createAction(Contactform $contactform){
  6. $contactform->setPid($GLOBALS['TSFE']->id); //set pid to current page ID
  7. $this->contactformRepository->add($contactform);
  8. //redirect to thank you page...
  9. }

And the ContactformValidator looks like this:

  1. <?php
  2. namespace Package\Extension\Domain\Validator;
  3. use Package\Extension\Domain\Model\Contactform;
  4. class ContactformValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator {
  5. public function isValid($contactform) {
  6. $isValid = true;
  7. if(!$contactform->getName()){
  8. $this->addError(
  9. $this->translateErrorMessage(
  10. 'tx_extension_domain_model_contactform.errors.required',
  11. 'extension'
  12. ), 111);
  13. $isValid = false;
  14. }
  15. if(!$contactform->getEmail()){
  16. $this->addError(
  17. $this->translateErrorMessage(
  18. 'tx_extension_domain_model_contactform.errors.required',
  19. 'extension'
  20. ), 111);
  21. $isValid = false;
  22. }
  23. return $isValid;
  24. }
  25. }

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.


Viewing all articles
Browse latest Browse all 310

Trending Articles