I'm having problems implementing the IsImage File validator in a Form class in Zend Framework 2-beta5.
In general I'm having problems using any File validator in a Zend Form class in Zend framework 2.
I couldn't find any relevant documentation.
I found that for example Float validator that resides at Library/Zend/Validator
can be used with the following code:
$this->setInputFilter($inputFactory->createInputFilter(array(
'alcohol_vol' => array(
'name' => 'alcohol_vol',
'required' => true,
'validators' => array(
array(
'name' => 'Float',
),
),
),
)));
the IsImage file validator resides at /Library/Zend/Validator/File and can't find a way to use it. any information regarding the issue would be greatly appreciated.
thanks!
Kfir
Try to add this snippet under validators key, like this:
'validators' => array(
array(
'name' => '\Zend\Validator\File\IsImage',
'options' => array(
'break_chain_on_failure' => true,
),
),
),
But sometimes, depends on server configuration, IsImage might not working. Then use Extension validator instead:
'validators' => array(
array(
'name' => '\Zend\Validator\File\Extension',
'options' => array(
'extension' => array(
'png', 'jpeg', 'jpg',
),
'break_chain_on_failure' => true,
),
),
),
Upload file validate/filter should use Zend\File\Transfer but not Zend\Form
Try below way to add file validator
$fileTransfer = new Zend\File\Transfer\Transfer();
$fileTransfer->addValidators(array(
array('IsImage', true)
));
if($fileTransfer->isValid()){
$fileTransfer->receive();
}
Related
I would like to validate the file extension only if a file is uploaded.
I have a collection of fieldsets that include a File Input element. If I want to upload a file for only one of the fieldsets and leave the rest of the File Input elements empty the form is not validated, although they have required = false. This validation triggers the "fileExtensionNotFound" error.
Is there a way to add the AllowEmpty to or before the Extension validator?
'file' => array(
'required' => false,
'validators' => array(
array(
'name' => 'Zend\Validator\File\Extension',
'options' => array(
'extension' => array('pdf', 'xls','doc'),
)
)
)
)
I had to specify the type to be FileInput.
'file' => array(
'type' => '\Zend\InputFilter\FileInput',
'allow_empty' => true,
'required' => false,
'validators' => array(
array(
'name' => 'Zend\Validator\File\Extension',
'options' => array(
'extension' => array('pdf', 'xls','doc')
),
),
),
),
I have a problem with uploading Images in a Zend Framework 2 Application and i think i have a configuration error somewhere in my code.
This is my current Code. I created the Form and Filter like the ZfcUser Form.
Form extends ProvidesEventsForm(ZfcBase)
$file = new Element\File('image');
$file->setLabelAttributes(array('class' => 'control-label col-sm-4'));
$file->setLabel('image');
$this->add($file);
Filter extends ProvidesEventsInputFilter(ZfcBase)
$this->add(
array(
'type' => 'Zend\InputFilter\FileInput',
'name' => 'image',
'required' => true,
'validators' => array(
array(
'name' => 'File\UploadFile',
),
),
'filters' => array(
array(
'name' => 'File\RenameUpload',
'options' => array(
'target' => './public/img/uploads',
'randomize' => true,
),
),
),
)
);
In the validation process the method FileInput::isValid() is called - but the Value is always null and i have no clue why it is.
The HTML-Form is set to multipart/form-data and the Server configuration is also no problem.
The file i used for testing is only 80KB.
Anyone an idea, what is wrong?
Finally i found a solution for the Problem.
Before the Post information added to the form, they have to merged with the file information:
$files = $this->getRequest()->getFiles()->toArray();
$post = array_merge_recursive(
$this->getRequest()->getPost()->toArray(),
$files
);
$form->setData($post);
Is anyone familiar with the use of ZF2 regex validators within the factory pattern?
I have taken this code from various blogs and other stackoverflow questions, but it does not seem to work.
The addition of the regex validator blocks all changes to my form from updating the database - so the validator must be failing even when I insert a number.
However, when I check
$form -> getMessages();
I get an empty array. Any insight would be appreciated.
To illustrate I use a very simple regex that, as I understand it, would block any entry character that is not a number.
$inputFilter->add($factory->createInput(array(
'name' => 'Number',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'encoding' => 'UTF-8',
'max' => 20,
),
),
),
array(
'name' => 'Regex',
'options' => array(
'pattern' => '/^[0-9]+$',
'messages' => array(
'Invalid input, only 0-9 characters allowed'
),
),
),
)));
At a glance, Regex validator should sit inside "validators" array...
Running into a bit of a hurdle, and I can't find any supporting documentation. My use case is fairly simple. The Application module has javascript that should go into the head, and one of my other modules, Foo also has script that should go into the head. I assumed that this Assetic module could solve that. Here's what I inferred:
Application Config
/**
* Assetic
*/
'assetic_configuration' => array(
'buildOnRequest' => true,
'cacheEnabled' => false,
'webPath' => realpath('public/assets'),
'basePath' => 'assets',
'default' => array(
'assets' => array(
'#base_css',
'#head_js',
),
'options' => array(
'mixin' => true,
),
),
'modules' => array(
'application' => array(
# module root path for yout css and js files
'root_path' => __DIR__ . '/../assets',
# collection of assets
'collections' => array(
'base_css' => array(
'assets' => array(
'css/*.css',
),
'filters' => array(),
'options' => array(),
),
'head_js' => array(
'assets' => array(
'js/*.js',
),
'filters' => array(),
),
'base_images' => array(
'assets'=> array(
'images/*.png',
),
'options' => array(
'move_raw' => true,
)
),
),
),
),
),
and then in my Foo module...
Foo Module config
/**
* Assetic
*/
'assetic_configuration' => array(
'default' => array(
'assets' => array(
'#base_css',
'#head_js',
),
'options' => array(
'mixin' => true,
),
),
'modules' => array(
'foo' => array(
# module root path for yout css and js files
'root_path' => __DIR__ . '/../assets',
# collection of assets
'collections' => array(
'base_css' => array(
'assets' => array(
'css/*.css'
),
'filters' => array(),
'options' => array(),
),
'head_js' => array(
'assets' => array(
'js/*.js' // relative to 'root_path'
),
'filters' => array(),
'options' => array(),
),
'base_images' => array(
'assets'=> array(
'images/*.png'
),
'options' => array(
'move_raw' => true,
)
),
),
),
),
),
With this config, unfortunately, only the Foo module's javascript makes its way into head_js.js. I'm feeling like that meme with Milton in it, going "I was told there would be asset combining!" :)
Any help you could offer, is appreciated.
Thanks!
Ok - I've figured it out. Hopefully this helps someone else one day. The configuration keys I noted above weren't inaccurate -- but -- they weren't crafted properly when a secret undocumented feature is considered; had to crack the source open to learn that including the word 'head' in an asset bundle actually autoloads it in the head. It's a nice feature in the end, but really a head scratcher when you're not aware of it.
Hi I am trying to write a user registration form using ZfcUser module for Zend Framwork 2 and would like some advice on best practices when adding more user fields.
So far I have created my own module called "WbxUser" and as outlined in the modules wiki pages I have added a custom field called "userlastname" to ZfcUser's registration form by using the Event Manager in my modules bootstrap function like so.
Code:
//WbxUser.Module.php
namespace WbxUser;
use Zend\Mvc\MvcEvent;
class Module {
public function onBootstrap(MvcEvent $e){
$events = $e->getApplication()->getEventManager()->getSharedManager();
$events->attach('ZfcUser\Form\Register','init', function($e) {
$form = $e->getTarget();
$form->add(array(
'name' => 'userlastname',
'attributes' => array(
'type' => 'text',
),
'options' => array(
'label' => 'Last Name',
),
));
// Do what you please with the form instance ($form)
});
$events->attach('ZfcUser\Form\RegisterFilter','init', function($e) {
$filter = $e->getTarget();
$filter->add(array(
'name' => 'userlastname',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'StringLength',
'options' => array(
'min' => 3,
'max' => 255,
),
),
),
));
});
}
public function getConfig(){
return array();
}
public function getAutoloaderConfig(){
return array();
}
}
But after this I have got a bit lost on where/how to write the code to save the extra data that my new fields are gathering.
Is this the event where I can fire off save routines for the additional fields https://github.com/ZF-Commons/ZfcUser/wiki/How-to-perform-a-custom-action-when-a-new-user-account-is-created
Should I be writing my own WbxUser model that extends ZfcUser\Entity\User.php to add my new fields
I am a bit of a ZF and MVC noob so would be very great-full for a nudge in the write direction.
this one seems to be a bit more intuitive
http://juriansluiman.nl/en/article/117/use-3rd-party-modules-in-zend-framework-2
You can override the module configurations. The most elegant way is to use the zfcuser.global.php.dist in the autoload folder. Copy this file to your appliction autoload folder and change the filename to zfcuser.global.php. In here you're able to change whatever option you can find in here. This option can be used to override the zfcUser entity: 'user_entity_class' => 'ZfcUser\Entity\User'.
Then again, you may find yourself in a situation where you need to change things that cannot be changed in the configuration file. In this case you could create your own user module ( You may want to just clone the entire zfcuser module until you're sure about what files you want to replace.
After cloning the user module (and changed the namespaces accordingly) add your module to application.config.php. Make sure your module is loaded after zfcuser. Or alternatively you could remove zfcuser from the config file and put the following in module.php:
public function init($moduleManager)
{
$moduleManager->loadModule('ZfcUser');
}
Now you can override ZfcUser module configurations.
The following snippet could be used to override the template folder and UserController.
<?php
// Note most routes are managed in zfcUser config file.
// All settings here either overrides or extend that functionality.
return array(
'view_manager' => array(
'template_path_stack' => array(
// 'zfcuser' => __DIR__ . '/../view', Override template path
),
'template_map' => array(
// You may want to use templates from the original module without having to copy - paste all of them.
),
),
'controllers' => array(
'invokables' => array(
// 'zfcuser' => 'YourModule\Controller\IndexController', // Override ZfcUser controller.
),
),
'router' => array(
'routes' => array(
'zfcuser' => array(
'type' => 'Literal',
'priority' => 2500,
'options' => array(
'route' => '/user',
'defaults' => array(
// Use original ZfcUser controller.
// It may be a good idea to use original code where possible
'controller' => 'ZfcUser\Controller\UserController',
'action' => 'index',
),
),
'may_terminate' => true,
'child_routes' => array(
'authenticate' => array(
'type' => 'Literal',
'options' => array(
'route' => '/authenticate',
'defaults' => array(
// Invoke YourModule\Controller\IndexController
'controller' => 'zfcuser',
'action' => 'authenticate',
),
),
),
),
),
),
),
);
Also you can override ZfcUser services in module.php. ;-)
Altering a third party module don't really appeal to me because at times it unsettle a lot of functionality. I always try do something outside the module because if there is an update in the module i easily incorporate into my application without haven to rewrite anything.
To do something like you are trying to do I would create another table in my application and add a foreign key that extend the zfcuser table that way i can relate the information to each zf2user in my application.
It just a suggestion as i am also new in zf2.