3

In Extbase usually I handle form validation myself within the controller, especially when I need advanced scenarios, but now I've simple, but large form with many fields, so I decided not to waste time and just use TYPO3's validators. So far so good in general it works, anyway I cannot force Extbase to trim values before validation and in result Extbase saves lot of spaces... so it's invalid, sample:

/**
 * @var string
 * @validate StringLength(minimum=2, maximum=255)
 * @validate NotEmpty
 */
protected $fooName = '';

As I said I've tens of fields and would love to avoid manual validating it... is there any solution?

Note: I tried extbase_filter ext, which would be great solution if it worked (unfortunately doesn't take any effect at TYPO3 ver.: 6.2.6.

Also for obvious reasons using JS for trimming values before form send isn't a solution too.

biesior
  • 55,576
  • 10
  • 125
  • 182

1 Answers1

9

You can do trim-ing inside your set* methods. Validation in Extabase's MVC process happens after set-ers invoked.

So, your example would be:

/**
 * @var string
 * @validate StringLength(minimum=2, maximum=255)
 * @validate NotEmpty
 */
protected $fooName = '';

public function setFooName($fooName)
{
    $this->fooName = trim($fooName);
}
Viktor Livakivskyi
  • 3,178
  • 1
  • 21
  • 33
  • That's right, I just discovered it after some debugging ;) It ensures that validation will be correct (finally!) anyway still doesn't trim values which are displayed in reloaded form (even with `trim()` added to getters as well). – biesior Nov 12 '15 at 10:16
  • This can be tricky, but you may play around in Controller with `$this->request->getReferringRequest()->getArgument('your_argument_name')`, which shouldn't be empty in case of validation error. As I understand, values form there are used as values for from fields to restore user input. – Viktor Livakivskyi Nov 12 '15 at 10:59
  • 1
    Yes, that I usually do with small forms... anyway while BE validation works now properly with this trimming in setters, then for _cosmetic_ trimming in form I'll use JS (similar like in BE TCE forms) so actually this topic is resolved for me now. – biesior Nov 12 '15 at 11:04