Reputation: 23
I have a form where you can upload a file if you select a choice, I'm trying to use a postValidator to throw an error if the user doesn't upload a file and there is not already a file uploaded
In the Form Class I have the postValidator
$this->validatorSchema->setPostValidator(
new sfValidatorCallback(array('callback' => array($this, 'checkFile'))));
public function checkFile($validator, $values){
$file = $this->getOption('file');
if($values['carta-compromiso'] && !$values['file'] && !$file){
throw new sfValidatorError($validator, 'Debe subir el archivo de carta de compromiso.');
}
return $values;
}
And in the action I pass the value $file with
$this->paso1_lleno = PostulacionTable::getInstance()->findOneByUsuarioId($this->getUser()->getGuardUser()->getId());
$this->form = new Paso1Form(array(), array('file' => $this->paso1_lleno->archivo_carta_compromiso));
The value I get in the post validator with $values['file'] is always null, even if I choose a file.
Upvotes: 2
Views: 221
Reputation: 7423
You need to bind the files as second parameter to bind()
method:
$this->form->bind($request->getParameter('paso_1'), $request->getFiles('paso_1'));
Upvotes: 1