wesside
wesside

Reputation: 5740

Zend Framework - Validating contents of upload (secondary validation)

I'm currently having users upload a MS Word Document where I am checking a version within the XML. The controller currently checks isValid() and then hits a library that does the parsing and extraction (since word is an archive). Now since it's technically "valid" already, I need to check the validity again through the library. What is the best course of action in Zend Framework for this?

Cheers from Kohana Land ;)

Upvotes: 0

Views: 153

Answers (1)

RockyFord
RockyFord

Reputation: 8519

I think I understand what you are looking for.
You are currently calling is valid against the form, your file passes the form validation (correct size, extension ...) now you need to unpack the file and validate the contents.

I'm going to assume you already have the code to validate the contents and just want to understand how that might be used in the controller.
'

public function anyAction() {

$form = new Form();
//test for $_POST
if ($this->getRequest()->isPost(){
    //Test form for validity
    if ($form->isValid($this->getRequest()->getPost()){
        //will receive file upload (unless disabled in element) and filter form values,
        //based on filters attached to the elements.
        $data = $form->getValues();
        //placeholder for whatever code is required to validate contents of file
        $validateFile = new MyFileValidator();
        //test for valid file contents
        if ($validateFile->isValid($data['file']){
            //Do some Stuff 
        }
        //if file contents is not valid, display form and populate values with unfiltered values
        $form->populate($this->getRequest()->getPost());
    }
   //if form is not valid, it should stay populated and display element errors
  }
//if not post send form to view
$this->view->form = $form;
}

This example should provide the basic controller workflow for this type of problem. I hope it addresses your question.

Upvotes: 2

Related Questions