« Anterior - Versión 2/3 (diferencias) - Siguiente » - Versión actual
Guillermo Zdanowicz, 01/06/2012 19:43


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
));
}
}
Redmine Appliance - Powered by TurnKey Linux