This is what I did in one of my forms:
inside the form configure method:
$post_validators [] = new gStep3Validator (
sfConfig::get
("app_procurement_wizard_select_prefix").$provision -> id,
sfConfig::get
("app_procurement_wizard_explanation_prefix").$provision -> id,
array(),
array ('invalid' => 'Explanation is
required') );
The explanation field is required only for certain values of the
wizard select field.
and this is my validator, Ii an be easily extended to specify the
values of the left fiedl for wich the reigth field is requeried.
[code]
<?php
class gStep3Validator extends sfValidatorSchema
{
/**
* Constructor.
*
* Available options:
*
* * left_field: The left field name
* * right_field: The right field name
* * throw_global_error: Whether to throw a global error (false by
default) or an error tied to the left field
*
* @param string $leftField The left field name
* @param string $operator The operator to apply
* @param string $rightField The right field name
* @param array $options An array of options
* @param array $messages An array of error messages
*
* @see sfValidatorBase
*/
public function __construct($leftField, $rightField, $options =
array(), $messages = array())
{
$this->addOption('left_field', $leftField);
$this->addOption('right_field', $rightField);
$this->addOption('throw_global_error', false);
parent::__construct(null, $options, $messages);
}
/**
* @see sfValidatorBase
*/
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;
// the actual verification
$valid = $leftValue == "Yes" || strlen ($rightValue) > 0;
if (!$valid)
{
$error = new sfValidatorError($this, 'invalid', array(
'left_field' => $leftValue,
'right_field' => $rightValue,
));
if ($this->getOption('throw_global_error'))
{
throw $error;
}
throw new sfValidatorErrorSchema($this,
array($this->getOption('right_field') => $error));
}
return $values;
}
/**
* @see sfValidatorBase
*/
public function asString($indent = 0)
{
$options = $this->getOptionsWithoutDefaults();
$messages = $this->getMessagesWithoutDefaults();
unset($options['left_field'], $options['right_field']);
$arguments = '';
if ($options || $messages)
{
$arguments = sprintf('(%s%s)',
$options ? sfYamlInline::dump($options) : ($messages ? '{}' : ''),
$messages ? ', '.sfYamlInline::dump($messages) : ''
);
}
return sprintf('%s%s %s%s %s',
str_repeat(' ', $indent),
$this->getOption('left_field'),
$arguments,
$this->getOption('right_field')
);
}
}
On 4/4/09, Sylvain Deloux <[email protected]> wrote:
>
> Hi,
>
> I'm using sfForms (with symfony 1.2) to create a form.
>
> I'd like to set a field required ONLY if a checkbox is checked,
> otherwise it doesn't not to be required.
>
> I'm looking on the forms documentation, but without success. Anybody
> could help me ?
>
> Thanks,
> Sylvain
> >
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---