Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ...
- public function addAction()
- {
- $categories = $this->getCategoryTable()->fetchAll();
- $category_options = array();
- foreach ($categories as $category) {
- $category_options[$category->id] = $category->name;
- }
- $form = new ProductForm($category_options);
- $form->get('submit')->setValue('Pridať inzerát');
- $request = $this->getRequest();
- if ($request->isPost()) {
- $post = array_merge_recursive(
- $request->getPost()->toArray(),
- $request->getFiles()->toArray()
- );
- $form->setData($post);
- $product = new Product();
- $form->setInputFilter($product->getInputFilter());
- if ($form->isValid())
- {
- $data = $form->getData();
- echo var_dump($data);
- $product->exchangeArray($form->getData());
- $this->getProductTable()->saveProduct($product);
- //return $this->redirect()->toRoute('product');
- }
- }
- return array(
- 'form' => $form,
- );
- }
- class ProductForm extends Form
- {
- public function __construct($category_options)
- {
- parent::__construct('product');
- $this->add(array(
- 'name' => 'id',
- 'type' => 'Hidden',
- ));
- $this->add(array(
- 'name' => 'title',
- 'type' => 'text',
- 'options'=> array(
- 'label' => 'Názov inzerátu:',
- ),
- ));
- $this->add(array(
- 'name' => 'description',
- 'type' => 'text',
- 'options' => array(
- 'label' => 'Popis inzererátu:',
- ),
- ));
- $this->add(array(
- 'name' => 'category_id',
- 'type' => 'ZendFormElementSelect',
- 'options' => array(
- 'label' => 'Kategória inzerátu:',
- 'value_options' => $category_options
- ),
- ));
- $this->add(array(
- 'name' => 'phone',
- 'type' => 'text',
- 'options' => array(
- 'label' => 'Telefónne číslo:',
- ),
- ));
- $this->add(array(
- 'name' => 'email',
- 'type' => 'text',
- 'options' => array(
- 'label' => 'Email:',
- ),
- ));
- $this->add(array(
- 'name' => 'price',
- 'type' => 'text',
- 'options' => array(
- 'label' => 'Cena:',
- ),
- ));
- $this->add(array(
- 'name' => 'location',
- 'type' => 'text',
- 'options' => array(
- 'label' => 'Mesto:',
- ),
- ));
- $this->add(array(
- 'name' => 'shipping',
- 'type' => 'text',
- 'options' => array(
- 'label' => 'Odosiela do:',
- ),
- ));
- $this->add(array(
- 'name' => 'top',
- 'type' => 'ZendFormElementSelect',
- 'options' => array(
- 'label' =>'Topovať inzerát?',
- 'value_options' => array(
- 'nie' => 'Nie',
- 'ano' => 'Áno'
- ),
- ),
- ));
- $this->addPhotos();
- $this->addInputFilter();
- $this->add(array(
- 'name' => 'submit',
- 'type' => 'Submit',
- 'attributes' => array(
- 'value' => 'Choď',
- 'id' => 'submitbutton',
- 'class' => 'btn btn-primary'
- ),
- ));
- }
- public function addPhotos()
- {
- // File Input
- $file = new ElementFile('image-file');
- $file->setLabel('Foto pre inzerát')
- ->setAttribute('id', 'image-file')
- ->setAttribute('name', 'image-file')
- ->setAttribute('multiple', true);
- $this->add($file);
- }
- public function addInputFilter()
- {
- $inputFilter = new InputFilterInputFilter();
- // File Input
- $fileInput = new InputFilterFileInput('image-file');
- $fileInput->setRequired(true);
- $fileInput->getValidatorChain()
- ->attachByName('filesize', array('max' => 204800))
- ->attachByName('filemimetype', array('mimeType' => 'image/jpeg, image/x-png'))
- ->attachByName('fileimagesize', array('maxWidth' => 100, 'maxHeight' => 100));
- $fileInput->getFilterChain()->attachByName(
- 'filerenameupload',
- array(
- 'target' => './public/img/product.jpeg',
- 'randomize' => true,
- )
- );
- $inputFilter->add($fileInput);
- $this->setInputFilter($inputFilter);
- }}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement