/** * @group ZF-12210 */ public function testAutoInsertNotEmptyValidator() { $this->testElementShouldAllowSpecifyingAdapterUsingConcreteInstance(); $this->element->setRequired(true); // Test before validation $this->assertNull($this->element->getValidator('NotEmpty')); // Test after validation $this->element->isValid('foo.jpg'); $this->assertTrue($this->element->getValidator('NotEmpty') instanceof Zend_Validate_NotEmpty); }
/** * * Change network background * */ public function init() { $cname = explode('_', get_class()); $this->preInit(end($cname)); // use template file $this->setDecorators(array(array('ViewScript', array('viewScript' => 'forms/CustomBackground.phtml')))); // load settings $Profiles = new Application_Model_Profiles(); $ProfilesMeta = new Application_Model_ProfilesMeta(); $request = Zend_Controller_Front::getInstance()->getRequest(); $request_profile_id = $request->getParam('id', false); $profile = $Profiles->getProfileByField('id', $request_profile_id); if (Zend_Auth::getInstance()->getIdentity()->role == 'admin' && $request_profile_id || $request_profile_id && $Profiles->getProfile($profile->name, false, true)) { // admin or own group & page $profile_id = $request_profile_id; } else { // editing profile $profile_id = Zend_Auth::getInstance()->getIdentity()->id; } $all_meta = $ProfilesMeta->getMetaValues($profile_id); // fields $background_image = new Zend_Form_Element_File('background'); $background_image->setDecorators(array('File', 'Errors'))->setLabel($this->translator->translate('Choose Picture (jpg, png or gif)'))->addValidator('Count', false, 1)->addValidator('Size', false, Zend_Registry::get('config')->get('max_file_upload_size'))->addValidator('Extension', false, 'jpg,jpeg,png,gif'); $background_image->getValidator('Count')->setMessage($this->translator->translate('File not allowed or too big')); $background_image->getValidator('Size')->setMessage($this->translator->translate('File not allowed or too big')); $background_image->getValidator('Extension')->setMessage($this->translator->translate('File not allowed or too big')); $background_repeat = new Zend_Form_Element_Checkbox('background_repeat'); $background_repeat->setDecorators(array('ViewHelper', 'Errors'))->setValue(isset($all_meta['background_repeat']) && $all_meta['background_repeat'] == 1 ? 1 : 0)->setLabel($this->translator->translate('Repeat background'))->setCheckedValue("1")->setUncheckedValue("0"); $background_scroll = new Zend_Form_Element_Checkbox('background_scroll'); $background_scroll->setDecorators(array('ViewHelper', 'Errors'))->setValue(isset($all_meta['background_scroll']) && $all_meta['background_scroll'] == 1 ? 1 : 0)->setLabel($this->translator->translate('Scroll background'))->setCheckedValue("1")->setUncheckedValue("0"); $background_stretch = new Zend_Form_Element_Checkbox('background_stretch'); $background_stretch->setDecorators(array('ViewHelper', 'Errors'))->setValue(isset($all_meta['background_stretch']) && $all_meta['background_stretch'] == 1 ? 1 : 0)->setLabel($this->translator->translate('Stretch background'))->setCheckedValue("1")->setUncheckedValue("0"); $disable_image = new Zend_Form_Element_Checkbox('background_noimage'); $disable_image->setDecorators(array('ViewHelper', 'Errors'))->setValue(isset($all_meta['background_noimage']) && $all_meta['background_noimage'] == 1 ? 1 : 0)->setLabel($this->translator->translate('Disable custom image'))->setCheckedValue("1")->setUncheckedValue("0"); $submit = new Zend_Form_Element_Submit('formsubmit'); $submit->setDecorators(array('ViewHelper'))->setLabel($this->translator->translate('Save'))->setAttrib('class', 'submit btn btn-default'); $this->addElements(array($background_image, $background_repeat, $background_scroll, $background_stretch, $disable_image, $submit)); $this->postInit(); }
error_reporting(E_ALL); ini_set('display_errors', '1'); /** * @file form.php */ // Load Zend Framework set_include_path(implode(PATH_SEPARATOR, array(get_include_path(), __DIR__ . '/'))); // Create the auto loader so zend can load the rest automatically require_once 'Zend/Loader/Autoloader.php'; $autoloader = Zend_Loader_Autoloader::getInstance(); $form = new Zend_Form(); // Set form name, id, method and action $form->setName('fileUpload')->setAttrib('id', 'fileUpload')->setMethod('post')->setAttrib('enctype', 'multipart/form-data')->setAction('http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI']); $file = new Zend_Form_Element_File('file'); $file->setLabel('File')->setRequired(true)->setValueDisabled(False)->addValidator('Extension', FALSE, 'jpg,jpeg')->addValidator('Size', false, array('min' => 20, 'max' => 20000000)); $file->getValidator('Extension')->setMessage('JPEGs only'); // Create text area for message $message = new Zend_Form_Element_Textarea('message'); $message->setRequired(TRUE)->setAttrib('id', 'message')->addErrorMessage('Please specify a message'); // Create a submit button $submit = new Zend_Form_Element_Submit('submit'); $submit->setLabel('Submit')->setAttrib('id', 'submit'); // Set the ViewScript decorator for the form and tell it which // template file to use $form->setDecorators(array(array('ViewScript', array('viewScript' => 'form-contact.tpl.php')))); // Add the form elements AFTER the viewscript decorator has been set $form->addElements(array($file, $message, $submit)); // Get rid of all element decorators except for ViewHelper to render // the individual elements and Errors decorator to render the errors. $form->setElementDecorators(array('ViewHelper', 'Errors')); // The file element requires the File decorator instead of the
public function addFile($name, $arrayOrKey1 = null, $value1 = null, $key2 = null, $value2 = null) { $options = func_get_args(); $options = \MUtil_Ra::pairs($options, 1); $options = $this->_mergeOptions($name, $options, self::DISPLAY_OPTIONS, self::FILE_OPTIONS, self::TEXT_OPTIONS); $filename = $this->_moveOption('filename', $options); $count = $this->_moveOption('count', $options); $size = $this->_moveOption('size', $options); $extension = $this->_moveOption('extension', $options); $element = new \Zend_Form_Element_File($name, $options); if ($filename) { $count = 1; // When // 1) an extension filter was defined, // 2) it concerns a single extension and // 3) $filename does not have an extension // then add the extension to the name. if ($extension && false === strpos($extension, ',') && false === strpos($name, '.')) { $filename .= '.' . $extension; } $element->addFilter(new \Zend_Filter_File_Rename(array('target' => $filename, 'overwrite' => true))); } if ($count) { $element->addValidator('Count', false, $count); } if ($size) { $element->addValidator('Size', false, $size); } if ($extension) { $element->addValidator('Extension', false, $extension); // Now set a custom validation message telling what extensions are allowed $validator = $element->getValidator('Extension'); $validator->setMessage('Only %extension% files are accepted.', \Zend_Validate_File_Extension::FALSE_EXTENSION); } return $this->_addToForm($name, $element); }