I'm using Symfony 1.2 with Doctrine. I have an object with the i18n behaviour. If I have the following validators, it fails when saving the form with error开发者_运维百科 "SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'id' cannot be null" (it tries to save the object_translation before saving the object)
$this->validatorSchema['phone'] = new sfValidatorAnd(array(
new sfValidatorPhone(),
new sfValidatorString(array('max_length' => 50)),
), array('required'=> false));
If I have only one validator (any of both), it works:
$this->validatorSchema['phone'] = new sfValidatorPhone();
The same happens with two other fields. So there is something odd about sfValidatorAnd and i18n. Do you know what can be happening?
Thanks!
I've found a solution, but I still don't know why the original code didn't work. What I've done is having a unique custom made validator:
$this->validatorSchema['phone'] = new sfValidatorPhone(array('max_length' => 50, 'required' => false));
And sfValidatorPhone extends sfValidatorString:
<?php
class sfValidatorPhone extends sfValidatorString
{
protected function configure($options = array(), $messages = array())
{
$this->addOption('required');
$this->addMessage('required', 'The phone is required.');
$this->addMessage('international', sfContext::getInstance()->getI18n()->__('The phone must have the international country code (+/00)'));
parent::configure($options, $messages);
}
protected function doClean($value)
{
$phone = preg_replace('/[^0-9|+]/','',$value);
$phone = parent::doClean($phone);
...code...
return $phone;
}
}
精彩评论