2011-07-18 8 views
0

En ce moment je travaille sur un formulaire dans Zend Framework. Il doit devenir un formulaire où une entreprise peut remplir les détails du candidat. Je suis assez nouveau avec Zend, c'est pourquoi je poste ma question.Zend Framework: Télécharger un fichier en utilisant Zend Form Element

C'est ce que la forme ressemble (certains champs supprimés pour rendre le code plus court)

<?php 

class Application_Form_Validate_ContactMethodSelected extends Zend_Validate_Abstract { 
const INVALID = 'invalid'; 

protected $_messageTemplates = array(
    self::INVALID => 'Ten minste 1 vorm van contact invullen, telefoon, mobiel of e-mail' 
); 

public function isValid($value, $context = array()) 
{ 
// You need to use your element names, consider making these dynamic 
$checkFields = array('telefoon','mobiel','mail'); 
// Check if all are empty 
foreach ($checkFields as $field) { 
if (isset($context[$field]) && !empty($context[$field])) { 

    if (!empty($value)) { 
     // This is the element with content... validate as true 
     return true; 
    } 
    // we are going to return false and no error 
    // to break validation chain on other empty values 
    // This is a quick hack, don't have time to invest in this 
    return false; 
    } 
} 

// All were empty, set your own error message 
$this->_error(self::INVALID); 
return false; 
} 

} 


class Application_Form_Nieuwkandidaat extends Zend_Form { 

public function init() { 
    $this->setMethod('post'); 

    $DB = Zend_Db_Table::getDefaultAdapter(); 

    $id = $this->createElement('hidden', 'id'); 
    $voornaam = $this->createElement('text', 'voornaam'); 
    $voornaam->setLabel('Voornaam:') 
      ->setAttrib('size', 50)->addValidator('StringLength', false, array(2, 30)) 
      ->setRequired(true); 
    $telefoon = $this->createElement('text', 'telefoon'); 
    $telefoon->setLabel('Telefoon:') 
      ->setAttrib('size', 50)->setAllowEmpty(false) 
      ->addValidator(new Application_Form_Validate_ContactMethodSelected(), true) 
      ->addValidator('StringLength', false, array(10, 10)); 
    $mobiel = $this->createElement('text', 'mobiel'); 
    $mobiel->setLabel('Mobiel:') 
      ->setAttrib('size', 50)->setAllowEmpty(false) 
      ->addValidator(new Application_Form_Validate_ContactMethodSelected(), true) 
      ->addValidator('StringLength', false, array(10, 10)); 
    $mail = $this->createElement('text', 'mail'); 
    $mail->setLabel('E-mail:') 
      ->setAttrib('size', 50)->setAllowEmpty(false) 
      ->addValidator(new Application_Form_Validate_ContactMethodSelected(), true) 
      ->addValidator('StringLength', false, array(6, 40))->addValidator('EmailAddress', true); 
    $register = $this->createElement('submit', 'register'); 
    $register->setLabel("Verstuur") 
      ->setIgnore(true); 
    $reset = $this->createElement('reset', 'reset'); 
    $reset->setLabel("Reset") 
      ->setIgnore(true); 
    $this->addElements(array(
     $voornaam, 
     $mobiel, 
     $telefoon, 
     $mail, 
     $id, 
    )); 

    $this->addDisplayGroup(array(
     'voornaam', 
     'mobiel', 
     'telefoon', 
     'mail', 
     'telefoon' 
      ), 'contacts', array('legend' => 'Contact Informatie')); 

    $contacts = $this->getDisplayGroup('contacts'); 
    $contacts->setDecorators(array(
     'FormElements', 
     'Fieldset', 
     array('HtmlTag', array('tag' => 'div', 'style' => 'width:50%;;float:left;')) 
    )); 

    $this->setDecorators(array(
     'FormElements', 
     array('HtmlTag', array('tag' => 'div', 'style' => 'width:98%')), 
     'Form' 
    )); 

    $this->addElement($register); 
} 

} 

Comment puis-je ajouter un élément de formulaire où je peux sélectionner un fichier et le télécharger, il sera stocké (disons par exemple dans/application/tmp)

Si je dois placer le code de mon contrôleur, s'il vous plaît laissez-moi savoir aussi bien

Merci à l'avance!

+0

Je suis confus, vous posez des questions sur un élément de formulaire et nous montrez votre code sur une classe de validation? –

+0

-1 Je ne reçois pas de réponse. – JellyBelly

Répondre

4

essayez ceci:

//Element Attachment 
$this->addElement('file', 'attachment', array('label' => 'Attachment')); 
$this->attachment->setDestination(APPLICATION_PATH . "/tmp/"); 
$this->attachment->addValidator('NotExists', false); 

ou

$attachment = $this->createElement('file', 'attachment', array('label' => 'Attachment')) 
->attachment->setDestination(APPLICATION_PATH . "/tmp/") 
->addValidator('NotExists', false); 
+0

Quand j'ajoute ceci à mon code je reçois une erreur – JorritK

+0

j'ajoute une autre méthode! – JellyBelly

8

d'abord, plutôt que d'étendre Zend_Validate_Abstract, vous devriez étendez Zend_Form avec une méthode init() où vous pouvez créer vos éléments de formulaire:

<?php 

class Application_Form_ContactMethodSelected extends Zend_Form 
{ 
    public function init() 
    { 
    } 
} 

Maintenant que vous avez un numéro forme uel de travailler avec, vous pouvez ajouter des choses comme votre élément de chargement du fichier:

<?php 

class Application_Form_ContactMethodSelected extends Zend_Form 
{ 
    public function init() 
    { 
     $this->setAttrib('enctype', 'multipart/form-data'); 

     $file = new Zend_Form_Element_File('file'); 
     $file->setLabel('File') 
      ->setDestination(APPLICATION_PATH . '/tmp') 
      ->setRequired(true); 

     $submit = new Zend_Form_Element_Submit('submit'); 
     $submit->setLabel('Upload'); 

     $this->addElements(array($file, $submit)); 
    } 
} 

Maintenant que vous avez un formulaire avec certains éléments, vous pouvez l'utiliser dans votre contrôleur:

<?php 

class IndexController extends Zend_Controller_Action 
{ 
    public function indexAction() 
    { 
     $form = new Application_Form_ContactMethodSelected(); 

     if ($this->_request->isPost()) { 
      $formData = $this->_request->getPost(); 
      if ($form->isValid($formData)) { 

       // success - do something with the uploaded file 
       $uploadedData = $form->getValues(); 
       $fullFilePath = $form->file->getFileName(); 

       Zend_Debug::dump($uploadedData, '$uploadedData'); 
       Zend_Debug::dump($fullFilePath, '$fullFilePath'); 

       echo "done"; 
       exit; 

      } else { 
       $form->populate($formData); 
      } 
     } 

     $this->view->form = $form; 
    } 
} 

Et votre script de vue, vous pouvez afficher votre formulaire:

<h1>My upload form</h1> 
<?php echo $this->form; ?> 
Questions connexes