przewiń do treści

Zend Framework od podstaw

Włodzimierz Gajda

<?php

class Application_Form_Plik extends Zend_Form
{

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

    $element = new Zend_Form_Element_File('nazwapliku', array('required' => true));
    $element
      ->setLabel('Dodaj zdjęcie:')
      ->setDestination(realpath(APPLICATION_PATH . '/../public/uploads'))
      ->addValidator('NotEmpty', true)
      ->addValidator('Count', true, 1)
      ->addValidator('Size', true, 102400)
      ->addValidator('NotExists', realpath(APPLICATION_PATH . '/../public/uploads'))
      ->addValidator('Extension', false, 'jpg,png,gif');
    $this->addElement($element, 'nazwapliku');

    $this->nazwapliku->getValidator('NotExists')->setMessages(array(
      Zend_Validate_File_NotExists::DOES_EXIST => "Plik '%value%' już istnieje!",
    ));
    $this->nazwapliku->getValidator('Upload')->setMessages(array(
      Zend_Validate_File_Upload::NO_FILE => "Nazwa pliku nie może być pusta!",
    ));
    $this->nazwapliku->getValidator('Size')->setMessages(array(
      Zend_Validate_File_Size::TOO_BIG => "Maksymalny...",
    ));

    $this->addElement('submit', 'submit', array(
      'label' => 'Zapisz',
    ));
  }

}

Listing 31.2. Formularz do dodawania zdjęć do kolekcji

Rozdział 31. Przesyłanie plików na serwer

listing-31-02.txt