biesior
biesior

Reputation: 55798

TYPO3/Extbase - How to trim values before validation/saving objects?

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.

Upvotes: 3

Views: 731

Answers (1)

Viktor Livakivskyi
Viktor Livakivskyi

Reputation: 3228

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);
}

Upvotes: 9

Related Questions