public function testSetAttribsOverwritesExistingAttribs() { $this->testCanAddAndRetrieveMultipleAttribs(); $array = array('bogus' => 'value', 'not' => 'real'); $this->form->setAttribs($array); $this->assertSame($array, $this->form->getAttribs()); }
public function getSaveProductForm($id) { $form = new Zend_Form(); //get product whitch want update $productMapper = new Application_Model_ProductMapper(); $product = new Application_Model_Product(); if ($id) { $product = $productMapper->getProductById($id); } // Set the method for the display form to POST $form->setMethod('post'); $form->setAttribs(array('class' => 'form-horizontal', 'enctype' => 'multipart/form-data')); $decoratorField = new My_Decorator_Field(); $elements = array(); //Add id hidden field $input = new Zend_Form_Element_Hidden('id', array('value' => $id)); $elements[] = $input; // Add name field $input = new Zend_Form_Element_Text('name', array('required' => true, 'label' => 'Name:', 'id' => 'name', 'placeholder' => 'Type something..', 'value' => $product->getName(), 'class' => 'form-control')); $input->addValidators(array(new Zend_Validate_Alnum(), new Zend_Validate_NotEmpty())); $input->addDecorator($decoratorField); $elements[] = $input; // Add category field $select = new Zend_Form_Element_Select('category_id', array('required' => true, 'label' => 'Category:', 'id' => 'category', 'class' => 'form-control')); $categoryMapper = new Application_Model_CategoryMapper(); $categories = $categoryMapper->fetchAll(); foreach ($categories as $category) { $select->addMultiOption($category->getId(), $category->getName()); } // set selected option $select->setValue($product->getCategoryId()); $select->addDecorator($decoratorField); $elements[] = $select; $currencyMapper = new Application_Model_CurrencyMapper(); $currency = $currencyMapper->getDefaultCurrency(); // Add Price field $input = new Zend_Form_Element_Text('price', array('required' => true, 'label' => 'Price in ' . $currency->getCode() . ':', 'id' => 'price', 'placeholder' => 'Type something..', 'value' => number_format((double) $product->price, 2), 'class' => 'form-control', 'min' => self::MIN, 'max' => self::MAX, 'step' => 'any', 'type' => 'number')); $min = new Zend_Validate_LessThan(self::MAX); $max = new Zend_Validate_GreaterThan(self::MIN); $input->addValidators(array(new Zend_Validate_Float(), $min, $max, new Zend_Validate_NotEmpty())); $input->addDecorator($decoratorField); $elements[] = $input; if ($id) { //Add File field if ($product->file) { $input = new Zend_Form_Element('file', array('label' => 'File:', 'id' => 'file', 'class' => 'form-control', 'value' => $product->file)); $input->addDecorator(new My_Decorator_AnchoraFileForm()); $elements[] = $input; } else { $input = new Zend_Form_Element_File('file', array('label' => 'File:', 'id' => 'file', 'class' => 'form-control')); $input->addDecorator($decoratorField); $elements[] = $input; } //Add Image field if ($product->image) { $input = new Zend_Form_Element('image', array('label' => 'Image:', 'id' => 'image', 'class' => 'form-control', 'value' => $product->image)); $input->addDecorator(new My_Decorator_ImageForm()); $elements[] = $input; } else { $input = new Zend_Form_Element_File('image', array('label' => 'Image:', 'id' => 'image', 'class' => 'form-control')); $input->addDecorator($decoratorField); $elements[] = $input; } } else { //Add File field $input = new Zend_Form_Element_File('file', array('label' => 'File:', 'id' => 'file', 'class' => 'form-control')); $input->addDecorator($decoratorField); $elements[] = $input; //Add Image field $input = new Zend_Form_Element_File('image', array('label' => 'Image:', 'id' => 'image', 'class' => 'form-control')); $input->addDecorator($decoratorField); $elements[] = $input; } //Add Description field $input = new Zend_Form_Element_Textarea('description', array('label' => 'Description:', 'id' => 'description', 'class' => 'form-control', 'value' => $product->description)); $input->addDecorator($decoratorField); $elements[] = $input; //Add Submit button if (!$id) { $input = new Zend_Form_Element_Submit('submit', array('Label' => ' ', 'class' => 'btn btn-success', 'value' => 'Add New Product')); } else { $input = new Zend_Form_Element_Submit('submit', array('Label' => ' ', 'class' => 'btn btn-info', 'value' => 'Update Product')); } $input->addDecorator($decoratorField); $elements[] = $input; $form->addElements($elements); $form->addDisplayGroup(array('name', 'category_id', 'price', 'currency_id', 'file', 'image', 'description', 'submit'), 'displgrp', array('legend' => 'Add Products', 'decorators' => array('FormElements', 'Fieldset'))); return $form; }