Validadores personalizados
Versión 1 (Guillermo Zdanowicz, 11/07/2012 22:03)
| 1 | 1 | Guillermo Zdanowicz | h1. Validadores personalizados  | 
|---|---|---|---|
| 2 | 1 | Guillermo Zdanowicz | |
| 3 | 1 | Guillermo Zdanowicz | Como crear un post validador  | 
| 4 | 1 | Guillermo Zdanowicz | |
| 5 | 1 | Guillermo Zdanowicz | Ejemplo: PersonasForm.class.php  | 
| 6 | 1 | Guillermo Zdanowicz | |
| 7 | 1 | Guillermo Zdanowicz | |
| 8 | 1 | Guillermo Zdanowicz | public function configure()  | 
| 9 | 1 | Guillermo Zdanowicz |   { | 
| 10 | 1 | Guillermo Zdanowicz | parent::configure();  | 
| 11 | 1 | Guillermo Zdanowicz | $this->disableLocalCSRFProtection();  | 
| 12 | 1 | Guillermo Zdanowicz | |
| 13 | 1 | Guillermo Zdanowicz | // Añado un post validator  | 
| 14 | 1 | Guillermo Zdanowicz | $this->validatorSchema->setPostValidator(new formatoDocumentoValidatorSchema());  | 
| 15 | 1 | Guillermo Zdanowicz | }  | 
| 16 | 1 | Guillermo Zdanowicz | |
| 17 | 1 | Guillermo Zdanowicz | en lib/validator/ se crea el archivo formatoDocumentoValidatorSchema.class.php  | 
| 18 | 1 | Guillermo Zdanowicz | |
| 19 | 1 | Guillermo Zdanowicz | el cual contiene  | 
| 20 | 1 | Guillermo Zdanowicz | |
| 21 | 1 | Guillermo Zdanowicz | <pre>  | 
| 22 | 1 | Guillermo Zdanowicz | <?php  | 
| 23 | 1 | Guillermo Zdanowicz | class formatoDocumentoValidatorSchema extends sfValidatorSchema  | 
| 24 | 1 | Guillermo Zdanowicz | { | 
| 25 | 1 | Guillermo Zdanowicz | |
| 26 | 1 | Guillermo Zdanowicz | protected function doClean($values)  | 
| 27 | 1 | Guillermo Zdanowicz |   { | 
| 28 | 1 | Guillermo Zdanowicz | |
| 29 | 1 | Guillermo Zdanowicz | $errorSchema = new sfValidatorErrorSchema($this);  | 
| 30 | 1 | Guillermo Zdanowicz | |
| 31 | 1 | Guillermo Zdanowicz | $errorSchema= $this->formato($values,'Personas');  | 
| 32 | 1 | Guillermo Zdanowicz | |
| 33 | 1 | Guillermo Zdanowicz | // lanza un error para el formulario principal  | 
| 34 | 1 | Guillermo Zdanowicz | if (count($errorSchema))  | 
| 35 | 1 | Guillermo Zdanowicz |     { | 
| 36 | 1 | Guillermo Zdanowicz | throw new sfValidatorErrorSchema($this, $errorSchema);  | 
| 37 | 1 | Guillermo Zdanowicz | }  | 
| 38 | 1 | Guillermo Zdanowicz | |
| 39 | 1 | Guillermo Zdanowicz | |
| 40 | 1 | Guillermo Zdanowicz | return $values;  | 
| 41 | 1 | Guillermo Zdanowicz | }  | 
| 42 | 1 | Guillermo Zdanowicz | |
| 43 | 1 | Guillermo Zdanowicz | |
| 44 | 1 | Guillermo Zdanowicz | protected function formato($values,$formulario)  | 
| 45 | 1 | Guillermo Zdanowicz |   { | 
| 46 | 1 | Guillermo Zdanowicz | $errorSchema = new sfValidatorErrorSchema($this);  | 
| 47 | 1 | Guillermo Zdanowicz | |
| 48 | 1 | Guillermo Zdanowicz | $errorSchemaLocal = new sfValidatorErrorSchema($this);  | 
| 49 | 1 | Guillermo Zdanowicz | |
| 50 | 1 | Guillermo Zdanowicz | |
| 51 | 1 | Guillermo Zdanowicz | // aqui se armaria el control donde se controla el formato  | 
| 52 | 1 | Guillermo Zdanowicz |           if(condicion???){ | 
| 53 | 1 | Guillermo Zdanowicz | $errorSchemaLocal->addError(new sfValidatorError($this, 'Formato incorrecto DNI.'), $formulario['personas_numerodoc']);  | 
| 54 | 1 | Guillermo Zdanowicz | $errorSchema->addError($errorSchemaLocal, (string) $key);  | 
| 55 | 1 | Guillermo Zdanowicz | }  | 
| 56 | 1 | Guillermo Zdanowicz | return $errorSchema;  | 
| 57 | 1 | Guillermo Zdanowicz | }  | 
| 58 | 1 | Guillermo Zdanowicz | |
| 59 | 1 | Guillermo Zdanowicz | |
| 60 | 1 | Guillermo Zdanowicz | }  | 
| 61 | 1 | Guillermo Zdanowicz | </pre>  | 
| 62 | 1 | Guillermo Zdanowicz | |
| 63 | 1 | Guillermo Zdanowicz | |
| 64 | 1 | Guillermo Zdanowicz | -------------  | 
| 65 | 1 | Guillermo Zdanowicz | |
| 66 | 1 | Guillermo Zdanowicz | si el control es medinte claves de base de datos, se puede usar declaracion en el esquema  | 
| 67 | 1 | Guillermo Zdanowicz | |
| 68 | 1 | Guillermo Zdanowicz | <pre>  | 
| 69 | 1 | Guillermo Zdanowicz | Expedientes:  | 
| 70 | 1 | Guillermo Zdanowicz | actAs:  | 
| 71 | 1 | Guillermo Zdanowicz | Timestampable: ~  | 
| 72 | 1 | Guillermo Zdanowicz | fzblameable: ~  | 
| 73 | 1 | Guillermo Zdanowicz | columns:  | 
| 74 | 1 | Guillermo Zdanowicz | numero:  | 
| 75 | 1 | Guillermo Zdanowicz | type: string(80)  | 
| 76 | 1 | Guillermo Zdanowicz | notnull: true  | 
| 77 | 1 | Guillermo Zdanowicz | anio:  | 
| 78 | 1 | Guillermo Zdanowicz | type: string(4)  | 
| 79 | 1 | Guillermo Zdanowicz | notnull: true  | 
| 80 | 1 | Guillermo Zdanowicz | caratula:  | 
| 81 | 1 | Guillermo Zdanowicz | type: string(200)  | 
| 82 | 1 | Guillermo Zdanowicz | notnull: true  | 
| 83 | 1 | Guillermo Zdanowicz | objeto:  | 
| 84 | 1 | Guillermo Zdanowicz | type: string()  | 
| 85 | 1 | Guillermo Zdanowicz | vigenciade:  | 
| 86 | 1 | Guillermo Zdanowicz | type: date()  | 
| 87 | 1 | Guillermo Zdanowicz | notnull: false  | 
| 88 | 1 | Guillermo Zdanowicz | a:  | 
| 89 | 1 | Guillermo Zdanowicz | type: date()  | 
| 90 | 1 | Guillermo Zdanowicz | notnull: false  | 
| 91 | 1 | Guillermo Zdanowicz | finalizado:  | 
| 92 | 1 | Guillermo Zdanowicz | type: boolean()  | 
| 93 | 1 | Guillermo Zdanowicz | visado:  | 
| 94 | 1 | Guillermo Zdanowicz | type: boolean()  | 
| 95 | 1 | Guillermo Zdanowicz | ultimo:  | 
| 96 | 1 | Guillermo Zdanowicz | type: boolean()  | 
| 97 | 1 | Guillermo Zdanowicz | user_id:  | 
| 98 | 1 | Guillermo Zdanowicz | type: integer  | 
| 99 | 1 | Guillermo Zdanowicz | notnull: true  | 
| 100 | 1 | Guillermo Zdanowicz | default: 1  | 
| 101 | 1 | Guillermo Zdanowicz | indexes:  | 
| 102 | 1 | Guillermo Zdanowicz | owner_name:  | 
| 103 | 1 | Guillermo Zdanowicz | fields: [numero, anio] <------ esto indica que los campos numero y anio no deben repetirse  | 
| 104 | 1 | Guillermo Zdanowicz | type: unique  | 
| 105 | 1 | Guillermo Zdanowicz | relations:  | 
| 106 | 1 | Guillermo Zdanowicz | User:  | 
| 107 | 1 | Guillermo Zdanowicz | class: sfGuardUser  | 
| 108 | 1 | Guillermo Zdanowicz | local: user_id  | 
| 109 | 1 | Guillermo Zdanowicz | </pre>  |