I extended the sfValidatorSchemaCompare in order to add one new
operator named FORCE_TO_FILL.
Then, I can do this:
$this->validatorSchema->setPostValidator(
new sfValidatorSchemaCompareExtension('firstname',
sfValidatorSchemaCompareExtension::FORCE_TO_FILL, 'phone')
)
Then, If the user complete "firstname" field only, the error like "The
contact must have Phone" is triggered.
We can use this validator combined with "AND" validator and/or "OR"
validator to be able to have more complex validation.
In my project I'm using it as follow:
$this->validatorSchema->setPostValidator(
new sfValidatorOr(array(
new sfValidatorAnd(array(
new
sfValidatorSchemaCompareExtension('firstname',
sfValidatorSchemaCompareExtension::FORCE_TO_FILL, 'phone'),
new
sfValidatorSchemaCompareExtension('lastname',
sfValidatorSchemaCompareExtension::FORCE_TO_FILL, 'phone')
)
),
new sfValidatorAnd(array(
new
sfValidatorSchemaCompareExtension('firstname',
sfValidatorSchemaCompareExtension::FORCE_TO_FILL, 'email'),
new
sfValidatorSchemaCompareExtension('lastname',
sfValidatorSchemaCompareExtension::FORCE_TO_FILL, 'email')
)
)
), array(), array('invalid' => 'Each contact must have email or
phone (or both)'))
);
The Class:
-----------------------------------------------
<?php
class sfValidatorSchemaCompareExtension extends
sfValidatorSchemaCompare
{
const FORCE_TO_FILL = 'force_to_fill';
protected function doClean($values)
{
if (is_null($values)) {
$values = array();
}
if (!is_array($values)) {
throw new InvalidArgumentException('You must pass an array
parameter to the clean() method');
}
$leftValue = isset($values[$this->getOption('left_field')]) ?
$values[$this->getOption('left_field')] : null;
$rightValue = isset($values[$this->getOption('right_field')]) ?
$values[$this->getOption('right_field')] : null;
switch ($this->getOption('operator')) {
case self::FORCE_TO_FILL:
$valid = ( ($leftValue != null) && ($rightValue == null)) ?
false :
true;
break;
default:
parent::doClean($values);
return;
break;
}
if (!$valid) {
$error = new sfValidatorError($this, 'invalid', array(
'left_field' => $leftValue,
'right_field' => $rightValue,
'operator' => $this->getOption('operator'),
));
if ($this->getOption('throw_global_error')) {
throw $error;
}
throw new sfValidatorErrorSchema($this, array($this-
>getOption('left_field') => $error));
}
return $values;
}
}
---------------------------------------
I hope this help!
Bye
Lionel
On Oct 15, 7:33 pm, adrive <[EMAIL PROTECTED]> wrote:
> If I pass validatorSchema like as argument of my custom validator, then I can
> modify options on that fields. The example I posted earlier works. But when
> I embed myForm into another form, it stops working due to validatorSchema
> cloning.
>
> I am going to create enhancement ticket on this topic.
>
> Dňa St 15. Október 2008 21:34 adrive napísal:
>
> > myForm extends sfForm {
> > public function setUp() {
> > ...
> > $this->validatorSchema['column']->setOption('required', false);
> > $this->setPreValidator(new
> > sfValidatorCallback(array('arguments'=>array('vs'=>$this->validatorSchema),
> > 'callback'=>array($this, 'validateGroup'))); }
> > public function validateGroup($validator, $values, $args) {
> > // this pass without error, but column is not required
> > $args['vs']['column']->setOption('required', true);
> > return $values;
> > }
> > }
>
> --
> adrive
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---