[TYPO3-english] Validate input field in TYPO3 BE
Steffen Müller
typo3 at t3node.com
Wed May 4 20:37:22 CEST 2011
Hi.
On 03.05.2011 12:14 Rayuth You wrote:
> Hi all
>
> I use typo3 4.5, I want to validate my field in BE with regular
> expression. I tried in model with @validate RegularExpression(). But it
> not work.
>
You use extbase, right? Next time ask in the typo3.projects.typo3v4mvc
list.
What arguments did you use in RegularExpression()?
You should put the regex pattern into the clauses, e.g.
/**
* @var string $title
* @validate RegularExpression('/[a-z]+/')
*/
protected $title;
... will result in:
preg_match('/[a-z]+/', $title);
You can of course write a validator by yourself. e.g. if you need the
regex repeatedly or you want to hide complexity in your model. Find an
example in SjrOffers extension in forge:
Model:
----------
class Tx_SjrOffers_Domain_Model_AttendanceFee extends
Tx_Extbase_DomainObject_AbstractEntity {
/**
* @var string The amount.
* @validate Tx_SjrOffers_Domain_Validator_AmountValidator
**/
protected $amount = '0.00';
http://forge.typo3.org/projects/extension-sjr_offers/repository/entry/trunk/Classes/Domain/Model/AttendanceFee.php
Validator:
--------------
class Tx_SjrOffers_Domain_Validator_AmountValidator extends
Tx_Extbase_Validation_Validator_AbstractValidator {
/**
* Returns TRUE, if the given value is a valid amount.
*
* If at least one error occurred, the result is FALSE.
*
* @param mixed $value The value that should be validated
* @return boolean TRUE if the value is an amount, otherwise FALSE
*/
public function isValid($value) {
$this->errors = array();
if (preg_match("/^([0-9]{1,3}(\.[0-9]{3})*(,[0-9]+)?|,[ 0-9]+)$/",
$input, $matches)) {
return TRUE;
}
}
}
http://forge.typo3.org/projects/extension-sjr_offers/repository/entry/trunk/Classes/Domain/Validator/AmountValidator.php
--
cheers,
Steffen
TYPO3 Blog: http://www.t3node.com/
Twitter: http://twitter.com/t3node
More information about the TYPO3-english
mailing list