Validadores personalizados¶
Como crear un post validador
Ejemplo: PersonasForm.class.php
public function configure()
{
parent::configure();
$this->disableLocalCSRFProtection();
// Añado un post validator
$this->validatorSchema->setPostValidator(new formatoDocumentoValidatorSchema());
}
en lib/validator/ se crea el archivo formatoDocumentoValidatorSchema.class.php
el cual contiene
<?php
class formatoDocumentoValidatorSchema extends sfValidatorSchema
{
protected function doClean($values)
{
$errorSchema = new sfValidatorErrorSchema($this);
$errorSchema= $this->formato($values,'Personas');
// lanza un error para el formulario principal
if (count($errorSchema))
{
throw new sfValidatorErrorSchema($this, $errorSchema);
}
return $values;
}
protected function formato($values,$formulario)
{
$errorSchema = new sfValidatorErrorSchema($this);
$errorSchemaLocal = new sfValidatorErrorSchema($this);
// aqui se armaria el control donde se controla el formato
if(condicion???){
$errorSchemaLocal->addError(new sfValidatorError($this, 'Formato incorrecto DNI.'), $formulario['personas_numerodoc']);
$errorSchema->addError($errorSchemaLocal, (string) $key);
}
return $errorSchema;
}
}
si el control es medinte claves de base de datos, se puede usar declaracion en el esquema
Expedientes:
actAs:
Timestampable: ~
fzblameable: ~
columns:
numero:
type: string(80)
notnull: true
anio:
type: string(4)
notnull: true
caratula:
type: string(200)
notnull: true
objeto:
type: string()
vigenciade:
type: date()
notnull: false
a:
type: date()
notnull: false
finalizado:
type: boolean()
visado:
type: boolean()
ultimo:
type: boolean()
user_id:
type: integer
notnull: true
default: 1
indexes:
owner_name:
fields: [numero, anio] <------ esto indica que los campos numero y anio no deben repetirse
type: unique
relations:
User:
class: sfGuardUser
local: user_id