i want to upload a .swf format and i am getting an exception "false extension" and i tried to put a validator on the extension and it still didn't work any ideas how to upload .swf and do i need a special uploader. This is my form code.
class Admin_Form_Banner extends ZendX_Form_Designed {
public function init() {
$this->setEnctype(self::ENCTYPE_MULTIPART);
$this->setMethod(self::METHOD_POST);
$this->setMethod('post');
// Add an email element
$this->addElement('text', 'banner_title', array(
'label' => 'Banner Title',
'required' => true,
"class" => 'required',
'filters' => array('StringTrim')
));
$this->addElement('select','banner_type',array(
'label'=>'type',
'required'=>TRUE,
'class'=>'required',
'multiOptions'=>array('1'=>'Image','2'=>'Flash','3'=>'HTML')
));
$this->addElement('text', 'banner_link', array(
'label' => 'Banner Link',
'required' => true,
"class" => 'required url',
'value'=>'http://www.',
'filters' => array('StringTrim'),
));
$this->addElement('select','link_open',array(
'label'=>'choose how do you want the link to open ?',
'required'=>TRUE,
'multiOptions'=>array('self'=>'Same Page','_new'=>'Tab Page')
));
$this->addElement('checkbox', 'is_active', array(
'label' => 'Is Active',
'required' => true,
"class" => 'required',
'filters' => array('StringTrim')
));
$banner_position = new Zend_Form_Element_Select('banner_position');
$banner_position->setMultiOptions($this->getBannerPositions())->setLabel('Banner Position');
$this->addElement($banner_position, 'banner_position');
$this->addElement('hidden', 'file_path', array(
'required' => true,
"class" => 'required',
'Extension'=>'.swf'
));
$this->addElement('submit', 'submit', array(
'ignore' => true,
'label' => ''
));
}
Well it's a Zend_Form_Element_Hidden how could you receive a fileupload from that element ?
Maybe you've made some customization to allow that but we can't really guess with this limited code sample.
Related
I want to create the following element :
<input type="file" name="file[]">
I have tried the following code in myproject/module/Member/src/Member/Form/EditForm.php :
$this->add(array(
'name' => 'file',
'type' => 'file',
'attributes' => array(
'class' => 'form-control col-md-7 col-xs-12',
'id' => 'file',
),'options' => array(
'multiple'=>TRUE
),
));
and
$this->add(array(
'name' => 'file[]',
'type' => 'file',
'attributes' => array(
'class' => 'form-control col-md-7 col-xs-12',
'id' => 'file',
),'options' => array(
'multiple'=>TRUE
),
));
but it is not working.
For file upload Zend Framework 2 has a special FileInput class.
It is important to use this class because it also does other important things like validation before filtering. There are also special filters like the File\RenameUpload that renames the upload for you.
Considering that $this is your InputFilter instance the code could look like this:
$this->add(array(
'name' => 'file',
'required' => true,
'allow_empty' => false,
'filters' => array(
array(
'name' => 'File\RenameUpload',
'options' => array(
'target' => 'upload',
'randomize' => true,
'overwrite' => true
)
)
),
'validators' => array(
array(
'name' => 'FileSize',
'options' => array(
'max' => 10 * 1024 * 1024 // 10MB
)
)
),
// IMPORTANT: this will make sure you get the `FileInput` class
'type' => 'Zend\InputFilter\FileInput'
);
To attach file element to a form:
// File Input
$file = new Element\File('file');
$file->setLabel('My file upload')
->setAttribute('id', 'file');
$this->add($file);
Check the documentation for more information on file upload.
Or check the documentation here on how to make an upload form
I'm following along with the Zend Framework 2.0 by example, and working on the Forms chapter now. Everything seems to be working fine, except that my 'password' and 'confirm password' fields don't seem to be rendering correctly. Here is an excerpt of my RegisterForm.php file where I am defining the password field
class RegisterForm extends Form
{
public function __construct($name = null)
{
parent::__construct('Register');
$this->setAttribute('method', 'post');
$this->setAttribute('enctype', 'multipart/form-data');
...
$this->add(array(
'name' => 'password',
'attributes' => array(
'type' => 'password',
),
'options' => array(
'label' => 'Password',
),
'attributes' => array(
'required' => 'required'
),
'filters' => array(
array('name' => 'StringTrim'),
),
));
but when it is rendered in the browser, I am getting this when I view the page source...
<dd><input name="password" required="required" type="text" value=""></dd>
I'm quite sure I've got the code down from the book correctly, but I'm not sure if there's another step where I'm accidentally overriding the RegisterForm.php file.
why are you overriding your attributes? It should be:
$this->add(array(
'name' => 'password',
'options' => array(
'label' => 'Password',
),
'attributes' => array(
'type' => 'password',
'required' => 'required'
),
'filters' => array(
array('name' => 'StringTrim'),
),
));
'attributes' => array(
'type' => 'password',
),
// ...
'attributes' => array(
'required' => 'required'
),
Think a second...
I am using formFilter method (factory method) to validate forms in Zend Framework 2. Please somebody help to add file upload validation.
Please specify how to use "IsImage validation" or "MimeType Validator" inside formfilter.
Try this
public function getInputFilter()
{
if (!$this->filter) {
$this->filter = new InputFilter();
$factory = new InputFactory();
$this->filter->add($factory->createInput(array(
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
'isEmpty' => 'Please select an icon to upload.',
),
),
),
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => array(
'messages' => array(
'fileIsImageFalseType' => 'Please select a valid icon image to upload.',
'fileIsImageNotDetected' => 'The icon image is missing mime encoding, please verify you have saved the image with mime encoding.',
),
),
),
),
)));
}
return parent::getInputFilter();
}
Link
I'm trying to build a VERY simple form and want to add the validation in the form itself. No need for million lines of code when adding it in the form just is about 3 lines.
Here are two of my fields:
$this->add(array(
'name' => 'username',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Name*',
'required' => true,
),
'filters' => array(
array('StringTrim')
),
));
$this->add(array(
'name' => 'email',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'E-Mail*',
'required' => true,
),
'validators' => array(
array('regex', true, array(
'pattern' => '/[A-Z0-9._%-]+#[A-Z0-9.-]+\.[A-Z]{2,4}/i',
'messages' => 'Bitte eine gültige E-Mailadresse angeben'))
),
'filters' => array(
array('StringTrim')
),
));
The $form->isValid() ALWAYS returns true. Even if the field is empty. I have another field with a regex-validator, same thing... WTF, Zend?
My controller looks like this:
$form = new UserForm();
$form->setHydrator(new DoctrineEntity($entityManager));
$request = $this->getRequest();
if ($request->isPost()) {
$backenduser = new User();
$form->bind($user);
$form->setData($request->getPost());
if ($form->isValid()) {
....
}
Any ideas?
Validation- and Filtering-Definitions aren't part of the Form itself. See http://framework.zend.com/manual/2.0/en/user-guide/forms-and-actions.html
Try this, pass $_POST data.
if($form->isValid($_POST)) {
// success!
} else {
// failure!
}
I had written the hide and show script for the two elements by checking checkbox in zend, but the value 1 will be sent to the function for both check and uncheck, So it only shows the element but it doesn't hides the element. how to resolve it ?
$this->addElement('checkbox', 'Change_Password',
array('decorators' => $this->elementDecoratorsTr ,'label' => 'Want to Set password ? :',
'required' => false,
'onchange' => 'passwordShow(this.value)',
'Options' => array(
'checkbx1' => 'checkbx1'),
));
$this->addElement('Password', 'User_Password',array(
'decorators' => $this->elementDecoratorsTr,
'label' => 'Password:',
'style' => 'display:none;',
'required' => false,
'filters' => array('StringTrim'),
'validators' => array(array(
'validator' => 'StringLength'))
));
$this->addElement('Password', 'Confirm_Password',array(
'decorators' => $this->elementDecoratorsTr,
'label' => 'Confirm Password:',
'style' => 'display:none;',
'required' => false,
'filters' => array('StringTrim'),
'validators' => array(array(
'validator' => 'StringLength'))
));
Here is my script function to hide and show the two elements User_Password and Confirm_Password
<script>
function passwordShow(password)
{
alert(password);
if(password)
{
$("#User_Password").show();
$("#Confirm_Password").show();
}
else
{
$("#User_Password").hide();
$("#Confirm_Password").hide();
}
}
</script>