Validadores de formularios
Versión 1 (Guillermo Zdanowicz, 01/06/2012 19:41)
| 1 | 1 | Guillermo Zdanowicz | h1. Validadores de formularios |
|---|---|---|---|
| 2 | 1 | Guillermo Zdanowicz | |
| 3 | 1 | Guillermo Zdanowicz | |
| 4 | 1 | Guillermo Zdanowicz | TuClaseForm.class.php |
| 5 | 1 | Guillermo Zdanowicz | public function configure() |
| 6 | 1 | Guillermo Zdanowicz | { |
| 7 | 1 | Guillermo Zdanowicz | ... |
| 8 | 1 | Guillermo Zdanowicz | } |
| 9 | 1 | Guillermo Zdanowicz | |
| 10 | 1 | Guillermo Zdanowicz | $this->validatorSchema->setPostValidator( |
| 11 | 1 | Guillermo Zdanowicz | new sfValidatorCallback(array('callback' => array($this, 'validarPago'))) |
| 12 | 1 | Guillermo Zdanowicz | ); |
| 13 | 1 | Guillermo Zdanowicz | public function validarPago($validator, $values) |
| 14 | 1 | Guillermo Zdanowicz | { |
| 15 | 1 | Guillermo Zdanowicz | $cuota_minima = Doctrine_Query::create()->from('Contrato c')->where('c.id = ?',$values['contrato_id'])->fetchOne()->getCuotaMinima(); |
| 16 | 1 | Guillermo Zdanowicz | // Otra forma de hacer la consulta anterior |
| 17 | 1 | Guillermo Zdanowicz | // $cuota_minima = Doctrine_Core::getTable('Contrato')->find($values['contrato_id'])->get('cuo ta_minima'); |
| 18 | 1 | Guillermo Zdanowicz | if ($values['valor_pago'] < $cuota_minima) |
| 19 | 1 | Guillermo Zdanowicz | { |
| 20 | 1 | Guillermo Zdanowicz | $error = new sfValidatorError($validator, 'Debe ingresar un pago mayor o igual al monto minimo'); |
| 21 | 1 | Guillermo Zdanowicz | throw new sfValidatorErrorSchema($validator, array('campo_que_mostrara_el_error' => $error)); |
| 22 | 1 | Guillermo Zdanowicz | } |
| 23 | 1 | Guillermo Zdanowicz | return $values; |
| 24 | 1 | Guillermo Zdanowicz | } |
| 25 | 1 | Guillermo Zdanowicz | ------------------------------------------- |
| 26 | 1 | Guillermo Zdanowicz | |
| 27 | 1 | Guillermo Zdanowicz | otra forma (sacado de:http://www.symfony-project.org/more-with-symfony/1_4/pt/05-Custom-Widgets-and-Validators) |
| 28 | 1 | Guillermo Zdanowicz | |
| 29 | 1 | Guillermo Zdanowicz | |
| 30 | 1 | Guillermo Zdanowicz | |
| 31 | 1 | Guillermo Zdanowicz | class sfWidgetFormTrilean extends sfWidgetForm |
| 32 | 1 | Guillermo Zdanowicz | { |
| 33 | 1 | Guillermo Zdanowicz | public function configure ($options = array(), $attributes = array()) |
| 34 | 1 | Guillermo Zdanowicz | { |
| 35 | 1 | Guillermo Zdanowicz | |
| 36 | 1 | Guillermo Zdanowicz | $this-addOption>('choices', array( |
| 37 | 1 | Guillermo Zdanowicz | 0 => 'Não', |
| 38 | 1 | Guillermo Zdanowicz | 1 => 'Sim', |
| 39 | 1 | Guillermo Zdanowicz | 'null' => 'Nulo' |
| 40 | 1 | Guillermo Zdanowicz | )); |
| 41 | 1 | Guillermo Zdanowicz | } |
| 42 | 1 | Guillermo Zdanowicz | |
| 43 | 1 | Guillermo Zdanowicz | public function render($name, $value = null, $atributos = array(), $errors = array ()) |
| 44 | 1 | Guillermo Zdanowicz | { |
| 45 | 1 | Guillermo Zdanowicz | $valor = $ valor === null? 'null': $valor; |
| 46 | 1 | Guillermo Zdanowicz | |
| 47 | 1 | Guillermo Zdanowicz | $options = array(); |
| 48 | 1 | Guillermo Zdanowicz | foreach($this->getOption('choices') as $key => $opção) |
| 49 | 1 | Guillermo Zdanowicz | { |
| 50 | 1 | Guillermo Zdanowicz | $attributes = array ('value' = self::escapeOnce($key)); |
| 51 | 1 | Guillermo Zdanowicz | if($ key == $value) |
| 52 | 1 | Guillermo Zdanowicz | { |
| 53 | 1 | Guillermo Zdanowicz | $attributes ['selected'] = 'selected'; |
| 54 | 1 | Guillermo Zdanowicz | } |
| 55 | 1 | Guillermo Zdanowicz | |
| 56 | 1 | Guillermo Zdanowicz | $options [] = $this->renderContentTag( |
| 57 | 1 | Guillermo Zdanowicz | 'option', |
| 58 | 1 | Guillermo Zdanowicz | self::escapeOnce($option), |
| 59 | 1 | Guillermo Zdanowicz | $attributes |
| 60 | 1 | Guillermo Zdanowicz | ); |
| 61 | 1 | Guillermo Zdanowicz | } |
| 62 | 1 | Guillermo Zdanowicz | |
| 63 | 1 | Guillermo Zdanowicz | return $this->renderContentTag( |
| 64 | 1 | Guillermo Zdanowicz | 'select', |
| 65 | 1 | Guillermo Zdanowicz | "\n". implode ("\n", $options). "\n", |
| 66 | 1 | Guillermo Zdanowicz | array_merge(array('name' => $name), $attributes |
| 67 | 1 | Guillermo Zdanowicz | )); |
| 68 | 1 | Guillermo Zdanowicz | } |
| 69 | 1 | Guillermo Zdanowicz | } |