Validadores de formularios
Versión 3 (Guillermo Zdanowicz, 01/06/2012 19:56)
| 1 | 1 | Guillermo Zdanowicz | h1. Validadores de formularios  | 
|---|---|---|---|
| 2 | 1 | Guillermo Zdanowicz | |
| 3 | 2 | Guillermo Zdanowicz | (sacado de:http://groups.google.com/group/symfony-es/browse_thread/thread/b9c6540d63ed06af?pli=1)  | 
| 4 | 1 | Guillermo Zdanowicz | |
| 5 | 1 | Guillermo Zdanowicz | TuClaseForm.class.php  | 
| 6 | 1 | Guillermo Zdanowicz | public function configure()  | 
| 7 | 1 | Guillermo Zdanowicz | {  | 
| 8 | 1 | Guillermo Zdanowicz | ...  | 
| 9 | 1 | Guillermo Zdanowicz | }  | 
| 10 | 1 | Guillermo Zdanowicz | |
| 11 | 1 | Guillermo Zdanowicz | $this->validatorSchema->setPostValidator(  | 
| 12 | 1 | Guillermo Zdanowicz |     new sfValidatorCallback(array('callback' => array($this, 'validarPago')))  | 
| 13 | 1 | Guillermo Zdanowicz | );  | 
| 14 | 1 | Guillermo Zdanowicz | public function validarPago($validator, $values)  | 
| 15 | 1 | Guillermo Zdanowicz | {  | 
| 16 | 1 | Guillermo Zdanowicz |   $cuota_minima = Doctrine_Query::create()->from('Contrato c')->where('c.id = ?',$values['contrato_id'])->fetchOne()->getCuotaMinima();  | 
| 17 | 1 | Guillermo Zdanowicz | // Otra forma de hacer la consulta anterior  | 
| 18 | 1 | Guillermo Zdanowicz | // $cuota_minima = Doctrine_Core::getTable('Contrato')->find($values['contrato_id'])->get('cuo ta_minima');  | 
| 19 | 1 | Guillermo Zdanowicz | if ($values['valor_pago'] < $cuota_minima)  | 
| 20 | 1 | Guillermo Zdanowicz |   {  | 
| 21 | 1 | Guillermo Zdanowicz | $error = new sfValidatorError($validator, 'Debe ingresar un pago mayor o igual al monto minimo');  | 
| 22 | 1 | Guillermo Zdanowicz |     throw new sfValidatorErrorSchema($validator, array('campo_que_mostrara_el_error' => $error));  | 
| 23 | 1 | Guillermo Zdanowicz | }  | 
| 24 | 1 | Guillermo Zdanowicz | return $values;  | 
| 25 | 1 | Guillermo Zdanowicz | }  | 
| 26 | 1 | Guillermo Zdanowicz | -------------------------------------------  | 
| 27 | 1 | Guillermo Zdanowicz | |
| 28 | 1 | Guillermo Zdanowicz | otra forma (sacado de:http://www.symfony-project.org/more-with-symfony/1_4/pt/05-Custom-Widgets-and-Validators)  | 
| 29 | 1 | Guillermo Zdanowicz | |
| 30 | 1 | Guillermo Zdanowicz | |
| 31 | 1 | Guillermo Zdanowicz | |
| 32 | 1 | Guillermo Zdanowicz | class sfWidgetFormTrilean extends sfWidgetForm  | 
| 33 | 1 | Guillermo Zdanowicz | { | 
| 34 | 1 | Guillermo Zdanowicz | public function configure ($options = array(), $attributes = array())  | 
| 35 | 1 | Guillermo Zdanowicz |   { | 
| 36 | 1 | Guillermo Zdanowicz | |
| 37 | 1 | Guillermo Zdanowicz |     $this-addOption>('choices', array( | 
| 38 | 1 | Guillermo Zdanowicz | 0 => 'Não',  | 
| 39 | 1 | Guillermo Zdanowicz | 1 => 'Sim',  | 
| 40 | 1 | Guillermo Zdanowicz | 'null' => 'Nulo'  | 
| 41 | 1 | Guillermo Zdanowicz | ));  | 
| 42 | 1 | Guillermo Zdanowicz | }  | 
| 43 | 1 | Guillermo Zdanowicz | |
| 44 | 1 | Guillermo Zdanowicz | public function render($name, $value = null, $atributos = array(), $errors = array ())  | 
| 45 | 1 | Guillermo Zdanowicz |   { | 
| 46 | 1 | Guillermo Zdanowicz | $valor = $ valor === null? 'null': $valor;  | 
| 47 | 1 | Guillermo Zdanowicz | |
| 48 | 1 | Guillermo Zdanowicz | $options = array();  | 
| 49 | 1 | Guillermo Zdanowicz |     foreach($this->getOption('choices') as $key => $opção) | 
| 50 | 1 | Guillermo Zdanowicz |     { | 
| 51 | 1 | Guillermo Zdanowicz |       $attributes = array ('value' = self::escapeOnce($key)); | 
| 52 | 1 | Guillermo Zdanowicz | if($ key == $value)  | 
| 53 | 1 | Guillermo Zdanowicz |       { | 
| 54 | 1 | Guillermo Zdanowicz | $attributes ['selected'] = 'selected';  | 
| 55 | 1 | Guillermo Zdanowicz | }  | 
| 56 | 1 | Guillermo Zdanowicz | |
| 57 | 1 | Guillermo Zdanowicz | $options [] = $this->renderContentTag(  | 
| 58 | 1 | Guillermo Zdanowicz | 'option',  | 
| 59 | 1 | Guillermo Zdanowicz | self::escapeOnce($option),  | 
| 60 | 1 | Guillermo Zdanowicz | $attributes  | 
| 61 | 1 | Guillermo Zdanowicz | );  | 
| 62 | 1 | Guillermo Zdanowicz | }  | 
| 63 | 1 | Guillermo Zdanowicz | |
| 64 | 1 | Guillermo Zdanowicz | return $this->renderContentTag(  | 
| 65 | 1 | Guillermo Zdanowicz | 'select',  | 
| 66 | 1 | Guillermo Zdanowicz |       "\n". implode ("\n", $options). "\n", | 
| 67 | 1 | Guillermo Zdanowicz |       array_merge(array('name' => $name), $attributes | 
| 68 | 1 | Guillermo Zdanowicz | ));  | 
| 69 | 1 | Guillermo Zdanowicz | }  | 
| 70 | 1 | Guillermo Zdanowicz | }  | 
| 71 | 3 | Guillermo Zdanowicz | |
| 72 | 3 | Guillermo Zdanowicz | ----------------  | 
| 73 | 3 | Guillermo Zdanowicz | |
| 74 | 3 | Guillermo Zdanowicz | class sfValidatorTrilean extends sfValidatorBase  | 
| 75 | 3 | Guillermo Zdanowicz | { | 
| 76 | 3 | Guillermo Zdanowicz | class sfValidatorTrilean extends sfValidatorBase  | 
| 77 | 3 | Guillermo Zdanowicz |   { | 
| 78 | 3 | Guillermo Zdanowicz | $ this-addOption> ( 'matriz true_values', ( 'true', 't', 'yes', 'y', 'on', '1 '));  | 
| 79 | 3 | Guillermo Zdanowicz |     $this->addOption('false_values', array('false', 'f', 'no', 'n', 'off', '0')); | 
| 80 | 3 | Guillermo Zdanowicz |     $this->addOption('null_values', array('null', null)); | 
| 81 | 3 | Guillermo Zdanowicz | }  | 
| 82 | 3 | Guillermo Zdanowicz | |
| 83 | 3 | Guillermo Zdanowicz | protected function doClean($value)  | 
| 84 | 3 | Guillermo Zdanowicz |   { | 
| 85 | 3 | Guillermo Zdanowicz |     if (in_array($value, $this->getOption('true_values'))) | 
| 86 | 3 | Guillermo Zdanowicz |     { | 
| 87 | 3 | Guillermo Zdanowicz | return true;  | 
| 88 | 3 | Guillermo Zdanowicz | }  | 
| 89 | 3 | Guillermo Zdanowicz | |
| 90 | 3 | Guillermo Zdanowicz |     if (in_array($value, $this->getOption('true_values'))) | 
| 91 | 3 | Guillermo Zdanowicz |     { | 
| 92 | 3 | Guillermo Zdanowicz | return false;  | 
| 93 | 3 | Guillermo Zdanowicz | }  | 
| 94 | 3 | Guillermo Zdanowicz | |
| 95 | 3 | Guillermo Zdanowicz |     if (in_array($value, $this->getOption('null_values'))) | 
| 96 | 3 | Guillermo Zdanowicz |     { | 
| 97 | 3 | Guillermo Zdanowicz | return null;  | 
| 98 | 3 | Guillermo Zdanowicz | }  | 
| 99 | 3 | Guillermo Zdanowicz | |
| 100 | 3 | Guillermo Zdanowicz |     throw new sfValidatorError($this, 'invalid', array('value' => $value)); | 
| 101 | 3 | Guillermo Zdanowicz | }  | 
| 102 | 3 | Guillermo Zdanowicz | |
| 103 | 3 | Guillermo Zdanowicz | public function isEmpty($value)  | 
| 104 | 3 | Guillermo Zdanowicz |   { | 
| 105 | 3 | Guillermo Zdanowicz | return false;  | 
| 106 | 3 | Guillermo Zdanowicz | }  | 
| 107 | 3 | Guillermo Zdanowicz | }  |