Validadores de formularios¶
(sacado de:http://groups.google.com/group/symfony-es/browse_thread/thread/b9c6540d63ed06af?pli=1)
TuClaseForm.class.php
public function configure()
{
...
}
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback' => array($this, 'validarPago')))
);
public function validarPago($validator, $values)
{
$cuota_minima = Doctrine_Query::create()->from('Contrato c')->where('c.id = ?',$values['contrato_id'])->fetchOne()->getCuotaMinima();
// Otra forma de hacer la consulta anterior
// $cuota_minima = Doctrine_Core::getTable('Contrato')->find($values['contrato_id'])->get('cuo ta_minima');
if ($values['valor_pago'] < $cuota_minima)
{
$error = new sfValidatorError($validator, 'Debe ingresar un pago mayor o igual al monto minimo');
throw new sfValidatorErrorSchema($validator, array('campo_que_mostrara_el_error' => $error));
}
return $values;
}
-------------------------------------------
otra forma (sacado de:http://www.symfony-project.org/more-with-symfony/1_4/pt/05-Custom-Widgets-and-Validators)
class sfWidgetFormTrilean extends sfWidgetForm
{
public function configure ($options = array(), $attributes = array())
{
$this-addOption>('choices', array(
0 => 'Não',
1 => 'Sim',
'null' => 'Nulo'
));
}
public function render($name, $value = null, $atributos = array(), $errors = array ())
{
$valor = $ valor === null? 'null': $valor;
$options = array();
foreach($this->getOption('choices') as $key => $opção)
{
$attributes = array ('value' = self::escapeOnce($key));
if($ key == $value)
{
$attributes ['selected'] = 'selected';
}
$options [] = $this->renderContentTag(
'option',
self::escapeOnce($option),
$attributes
);
}
return $this->renderContentTag(
'select',
"\n". implode ("\n", $options). "\n",
array_merge(array('name' => $name), $attributes
));
}
}
class sfValidatorTrilean extends sfValidatorBase
{
class sfValidatorTrilean extends sfValidatorBase
{
$ this-addOption> ( 'matriz true_values', ( 'true', 't', 'yes', 'y', 'on', '1 '));
$this->addOption('false_values', array('false', 'f', 'no', 'n', 'off', '0'));
$this->addOption('null_values', array('null', null));
}
protected function doClean($value)
{
if (in_array($value, $this->getOption('true_values')))
{
return true;
}
if (in_array($value, $this->getOption('true_values')))
{
return false;
}
if (in_array($value, $this->getOption('null_values')))
{
return null;
}
throw new sfValidatorError($this, 'invalid', array('value' => $value));
}
public function isEmpty($value)
{
return false;
}
}