ZF2 use view helper in Validator class - localization

UPDATED!
ZF2, l10n view helper. I can't understand how to use my view helper inside of a class.
I want to use it like: $this->t('STRING TO TRANSLATE');
example bellow
NB! i'm only localizing project, i'm not allowed to change code structure or smth like that.Also i'm absolute newb in ZF2.
my Class -
class Project extends InputFilter{
as i understood i have to implement ServiceLocatorAwareInterface interface, tried this:
use Zend\ServiceManager\ServiceLocatorInterface as ServiceLocator;
class Project extends InputFilter implements ServiceLocator
{
protected $services;
public function __construct(Connection $p4, $mode, ServiceLocator $services)
{
$this->services = $services;
//some code
$this->add(...);
$this->add(
array(
'name' => 'name',
'filters' => array('trim'),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'message' => "Name is required and can't be empty."
)
),
array(
'name' => '\Application\Validator\Callback',
'options' => array(
'callback' => function ($value) use ($p4, $toId, $mode, $reserved) {
$id = $toId($value);
if (!$id) {
return $this->t('STRING TO TRANSLATE');
}
// more code here
return true;
}
)
)
)
)
);
//some code
$this->add(...);
}
public function setServiceLocator(ServiceLocatorInterface $serviceLocator) {
$this->serviceLocator = $serviceLocator;
}
public function getServiceLocator() {
return $this->serviceLocator;
}
//how to get this method work ???
public function t($msg) {
$translate = $this->services->get('ViewHelperManager')->get('t');
return $translate($msg);
}
}
Usage in Controller:
use Projects\Filter\Project as ProjectFilter;
...
protected function doAddEdit($mode, $project = null)
{
$p4Admin = $this->getServiceLocator()->get('p4_admin');
$request = $this->getRequest();
// process add request.
if ($request->isPost()) {
// pull out the data
$data = $request->getPost();
// configure our filter with the p4 connection and add/edit mode
$filter = new ProjectFilter($p4Admin, $mode); //
$filter->setData($data);
// if the data is valid, setup the project and save it
$isValid = $filter->isValid();
if ($isValid) {
$values = $filter->getValues();
$project = new Project($p4Admin);
$project->set($values)
->save();
}
return new JsonModel(
array(
'isValid' => $isValid,
'messages' => $filter->getMessages(), // THESE array of messages i want to localize
'redirect' => '/projects/' . $filter->getValue('id')
)
);
}
// prepare view for form.
$view = new ViewModel;
$view->setVariables(
array(
'mode' => $mode,
'project' => $project ?: new Project
)
);
return $view;
}
What am i doing wrong ?

You're calling tr method inside a class constructor. tr method calls $this->getServiceLocator(). $this->getServiceLocator() will not return a service locator instance since it'll only be injected by service manager after it has created the object that implements ServiceLocatorAwareInterface.
So you'd have to pass a service locator instance to the constructor, or don't depend on it in your __construct method. Probably the easiest way to go is to move your code from the constructor to the init method. Init is supposed to get called automatically as long as you get your input filter through the InputFilterManager.
Also I don't think you need the translator view helper. You should be able to get it from the service manager like so: $serviceManager->get('translator')

There is no need to do this at all the validation message will be translated by the validator anyway. But your config is a bit wrong I think
$this->add(
array(
'name' => 'name',
'filters' => array('trim'),)
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'YOUR_TRANSLATION_STRING_IS_EMPTY',
\Zend\Validator\NotEmpty::INVALID => 'YOUR_TRANSLATION_STRING_INVALID',
)
)
)
),
Have a read of https://zf2-docs.readthedocs.org/en/latest/modules/zend.validator.html#translating-messages
You will need to ensure you are managing your dependencies correctly for this to work so really depends how you are using the input filter.
If your not directly adding to a form or using InputFilterAwareInterface on your model you'll need to make sure your InputFilter is registered with InputFilterPluginManager and you retrieve it using InputFilterPluginManager rather than instantiating directly

Related

ZF2 model classes not found when moved to src\MyApp\Model\Repository

I'm trying to move database table classes to the recommended location of \Model\Repository instead of \Model for better segmentation of model code. But when I do, it can't find the classes. I'm rather new to ZF2, so it's probably something simple. I'm assuming that it's possible to place model classes into segmented directories for better classification of model class files.
This works:
module\Client\src\Client\Model\ClientTable.php
Paths to ClientTable.php from Module.php:
public function getServiceConfig() {
return array(
// setup multiple table access by model as {tablename}Table
'factories' => array(
'Client\Model\ClientTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new Model\ClientTable($dbAdapter);
return $table;
},
),
);
}
But trying to call from Repository breaks it and results in the class method not being found, but it should be found if it's within the Module directory, right?
module\Client\src\Client\Model\Repository\ClientTable.php
Paths to ClientTable.php from Module.php:
public function getServiceConfig() {
return array(
// setup multiple table access by model as {tablename}Table
'factories' => array(
'Client\Model\Repository\ClientTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$table = new Model\Repository\ClientTable($dbAdapter);
return $table;
},
),
);
}
You have an issue with the class autoload paths. You should go to your module config (module.config.php and module.php) an update the namespaces mapping to point to the new location of the files.
for example, you will have a function like this in module.php
public function getAutoloaderConfig() {
return array (
'Zend\Loader\ClassMapAutoloader' => array (),
'Zend\Loader\StandardAutoloader' => array (
'namespaces' => array (
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
"DBAL\\Entity" => __DIR__ . '/src/DBAL/Entity',
"DBAL\\Entity\\User" => __DIR__ . '/src/DBAL/Entity/User'
)
)
)
;
}
Thats a piece of code of one of my projects. If you post your module.config.php and your module.php i can give you more specific help
probably you had some mappings pointing to src\MyApp\Model and you have to change them to src\MyApp\Model\Repository
Also, be sure that you have the correct Client\Model\Repository namespaces in your moved files

ZF2 Fieldsets and Form Binding

I'm trying to create one page with a Form with two fieldsets that should each populate a different table.
I can easily create One form as in the Album tutorial, and bind the data like this:
$pageForm = new PageForm();
$pageForm->bind($page);
with my PageForm class as follows:
class PageForm extends Form
{
public function __construct($name = null)
{
// we want to ignore the name passed
parent::__construct('page');
$this->setAttribute('method', 'post');
$this->add(array(
'name' => 'id',
'attributes' => array(
'type' => 'hidden',
),
));
} /// and a bunch of other elements
but if I put these elements into fieldsets the bind no longer works, besides I would need to bind each fieldset to a separate table, and they need to save into the separate tables once the form is submited.
How would I go about this, I think I can do it using two forms but that is probably not the right way to go about it (If I understand the concept of fieldsets correctly)?
you have to use setObject in each Fieldset and provide a hydrator to it. eg:
<?php
// file My/Form/Product.php
namespace My\Form;
use Zend\Form\Fieldset;
use My\Entity\Product as ProductEntity;
use Zend\Stdlib\Hydrator\ClassMethods();
class Product extends Fieldset
{
public function __construct($name = 'product')
{
parent::__construct($name);
$this->setObject(new ProductEntity())
->setHydrator(new ClassMethods());
$this->add(array(
'name' => 'name',
'options' => array('label' => 'Product name'),
));
// Brand fieldset
$brand = new Brand();
$this->add($brand);
}
}
// File My/Form/Brand.php
namespace My\Form;
use Zend\Form\Fieldset;
use My\Entity\Brand as BrandEntity;
use Zend\Stdlib\Hydrator\ClassMethods();
class Brand extends Fieldset
{
public function __construct($name = 'brand')
{
parent::__construct($name = 'brand');
$this->setObject(new BrandEntity())
->setHydrator(new ClassMethods());
$this->add(array(
'name' => 'name',
'options' => array('label' => 'Brand name'),
));
}
}
// File My/Form/ProductForm.php
namespace My\Form;
use Zend\Form\Form;
use My\Entity\Product as ProductEntity;
use Zend\Stdlib\Hydrator\ClassMethods();
class ProductForm extends Form
{
public function __construct($name = 'product')
{
parent::__construct($name);
$this->setObject(new ProductEntity())
->setHydrator(new ClassMethods());
// Product Fieldset
// Here, we define Product fieldset as base fieldset
$product = new Product();
$product->setUseAsBaseFieldset(true);
$this->add($product);
}
}
// In Module.php
// ...
public function getServiceConfig()
{
return array(
'invokables' => array(
'My\Form\Product' => 'My\Form\Product',
),
);
}
// ...
// In Controller
// You don't need to use $form->bind($productEntity), except if you're editing a product.
// The form already has an Object, do you remenber??? "$this->setObject(new ProductEntity())" on form???
// ...
$form = $this->getServiceLocator()->get('My\Form\Product');
// ...

Is there any Zend2 library that provides filesystem abstraction layer?

I'm looking for something similar to: http://knplabs.com/blog/give-your-projects-a-gaufrette
Thanks.
Just use Gaufrette, I do it too (even in ZF2 projects)!
php composer.phar require knplabs/gaufrette:0.1.*
You can then use it anywhere in your application and eventually use it as a service by defining it in your YourNamespace\Module#getServiceConfig:
namespace YourNamespace;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Gaufrette\Filesystem;
use Gaufrette\Adapter\Local as LocalFs;
class Module implements ServiceProviderInterface, ConfigProviderInterface
{
public function getConfig()
{
return array(
'your_namespace' => array(
'filesystem' => array(
'base_path' => 'data/files',
),
),
);
}
public function getServiceConfig()
{
return array(
'factories' => array(
'YourNamespace\Filesystem' => function (ServiceLocatorInterface $sl) {
$config = $sl->get('Config');
$basePath = $config['your_namespace']['filesystem']['base_path'];
return new Filesystem(new LocalFs($basePath, true));
},
),
);
}
}
You can then use service YourNamespace\Filesystem across all your application.
I also use it in conjunction with Symfony\Filesystem to handle file moving/copying/checking operations. Not everything must come from Zend Framework 2 to be used in a ZF2 application.

ZF2 - The Forward Plugin returns the ViewModel Object. How to make it to return other values, e.g. simple or associative array?

I call the Forward plugin from one Controller's action method to get the value from the other Controller's action method:
namespace Foo/Controller;
class FooController {
public function indexAction() {
// I expect the $result to be an associative array,
// but the $result is an instance of the Zend\View\Model\ViewModel
$result = $this->forward()->dispatch('Boo/Controller/Boo',
array(
'action' => 'start'
));
}
}
And here's Boo Controller I apply to:
namespace Boo/Controller;
class BooController {
public function startAction() {
// I want this array to be returned,
// but an instance of the ViewModel is returned instead
return array(
'one' => 'value one',
'two' => 'value two',
'three' => 'value three',
);
}
}
And if I print_r($result) it is the ViewModel of the error/404 page:
Zend\View\Model\ViewModel Object
(
[captureTo:protected] => content
[children:protected] => Array
(
)
[options:protected] => Array
(
)
[template:protected] => error/404
[terminate:protected] =>
[variables:protected] => Array
(
[content] => Page not found
[message] => Page not found.
[reason] => error-controller-cannot-dispatch
)
[append:protected] =>
)
What is going on? How to change this behavior and get the required data type from the Forward plugin?
UPD 1
For now found only this here:
The MVC registers a couple of listeners for controllers to automate
this. The first will look to see if you returned an associative array
from your controller; if so, it will create a View Model and make this
associative array the Variables Container; this View Model then
replaces the MvcEvent‘s result.
And this doesn't work:
$this->getEvent()->setResult(array(
'one' => 'value one',
'two' => 'value two',
'three' => 'value three',
));
return $this->getEvent()->getResult(); // doesn't work, returns ViewModel anyway
It means instead of to get just an array I have to put variable into a ViewModel, return a ViewModel and get those variable from the ViewModel. Very good design, I can say.
You have to disable view in your action in ZF2. You can do this in this way:
namespace Application\Controller;
use Zend\Mvc\Controller\AbstractActionController;
class IndexController extends AbstractActionController
{
public function indexAction()
{
$result = $this->forward()->dispatch('Application/Controller/Index', array( 'action' => 'foo' ));
print_r($result->getContent());
exit;
}
public function fooAction()
{
$response = $this->getResponse();
$response->setStatusCode(200);
$response->setContent(array('foo' => 'bar'));
return $response;
}
}

Zend FrameWork 2 Get ServiceLocator In Form and populate a drop down list

I need to get the adapter from the form, but still could not.
In my controller I can recover the adapter using the following:
// module/Users/src/Users/Controller/UsersController.php
public function getUsersTable ()
{
if (! $this->usersTable) {
$sm = $this->getServiceLocator();
$this->usersTable = $sm->get('Users\Model\UsersTable');
}
return $this->usersTable;
}
In my module I did so:
// module/Users/Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'Users\Model\UsersTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$uTable = new UsersTable($dbAdapter);
return $uTable;
},
//I need to get this to the list of groups
'Users\Model\GroupsTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$gTable = new GroupsTable($dbAdapter);
return $gTable;
},
),
);
}
Could someone give me an example how to get the adapter to the table from the group form?
I have followed this example to my form users:
http://framework.zend.com/manual/2.0/en/modules/zend.form.collections.html
EDITED from here...
Maybe I expressed myself wrong to ask the question.
What I really need to do is populate a select (Drop Down) with information from my table groups.
So I need to get the services inside my userForm class by ServiceLocatorAwareInterface (see this link) implemented because By default, the Zend Framework MVC registers an initializer That will inject into the ServiceManager instance ServiceLocatorAwareInterface Implementing any class.
After retrieving the values ​​from the table groups and populate the select.
The problem is that of all the ways that I've tried, the getServiceLocator() returns this:
Call to a member function get() on a non-object in
D:\WEBSERVER\htdocs\Zend2Control\module\Users\src\Users\Form\UsersForm.php
on line 46
I just wanted to do this in my UserForm...
namespace Users\Form;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Zend\Form\Element;
use Zend\Form\Form;
class UsersForm extends Form implements ServiceLocatorAwareInterface
{
protected $serviceLocator;
public function getServiceLocator ()
{
return $this->serviceLocator;
}
public function setServiceLocator (ServiceLocatorInterface $serviceLocator)
{
$this->serviceLocator = $serviceLocator;
}
public function __construct ($name = null)
{
parent::__construct('users');
$this->setAttribute('method', 'post');
$sm = $this->getServiceLocator();
$groups = $sm->get('Users\Model\GroupsTable')->fetchAll(); // line 46
$select = new Element\Select('groups');
$options = array();
foreach ($groups as $group) {
$options[$group->id] = $group->name;
}
$select->setValueOptions($options);
$this->add($select);
// and more elements here...
The other various answers here generally correct, for ZF < 2.1.
Once 2.1 is out, the framework has a pretty nice solution. This more or less formalizes DrBeza's solution, ie: using an initializer, and then moving any form-bootstrapping into an init() method that is called after all dependencies have been initialized.
I've been playing with the development branch, it it works quite well.
This is the method I used to get around that issue.
firstly, you want to make your form implement ServiceLocatorInterface as you have done.
You will then still need to manually inject the service locator, and as the whole form is generated inside the contrstuctor you will need to inject via the contructor too (no ideal to build it all in the constructor though)
Module.php
/**
* Get the service Config
*
* #return array
*/
public function getServiceConfig()
{
return array(
'factories' => array(
/**
* Inject ServiceLocator into our Form
*/
'MyModule\Form\MyForm' => function($sm) {
$form = new \MyModule\Form\MyFormForm('formname', $sm);
//$form->setServiceLocator($sm);
// Alternativly you can inject the adapter/gateway directly
// just add a setter on your form object...
//$form->setAdapter($sm->get('Users\Model\GroupsTable'));
return $form;
},
),
);
}
Now inside your controller you get your form like this:
// Service locator now injected
$form = $this->getServiceLocator()->get('MyModule\Form\MyForm');
Now you will have access to the full service locator inside the form, to get hold of any other services etc such as:
$groups = $this->getServiceLocator()->get('Users\Model\GroupsTable')->fetchAll();
In module.php I create two services. See how I feed the adapter to the form.
public function getServiceConfig()
{
return array(
'factories' => array(
'db_adapter' => function($sm) {
$config = $sm->get('Configuration');
$dbAdapter = new \Zend\Db\Adapter\Adapter($config['db']);
return $dbAdapter;
},
'my_amazing_form' => function ($sm) {
return new \dir\Form\SomeForm($sm->get('db_adapter'));
},
),
);
}
In the form code I use that feed to whatever:
namespace ....\Form;
use Zend\Form\Factory as FormFactory;
use Zend\Form\Form;
class SomeForm extends Form
{
public function __construct($adapter, $name = null)
{
parent::__construct($name);
$factory = new FormFactory();
if (null === $name) {
$this->setName('whatever');
}
}
}
We handle this in the model, by adding a method that accepts a form
public function buildFormSelectOptions($form, $context = null)
{
/**
* Do this this for each form element that needs options added
*/
$model = $this->getServiceManager()->get('modelProject');
if (empty($context)){
$optionRecords = $model->findAll();
} else {
/**
* other logic for $optionRecords
*/
}
$options = array('value'=>'', 'label'=>'Choose a Project');
foreach ($optionRecords as $option) {
$options[] = array('value'=>$option->getId(), 'label'=>$option->getName());
}
$form->get('project')->setAttribute('options', $options);
}
As the form is passed by reference, we can do something like this in the controller where the form is built:
$builder = new AnnotationBuilder();
$form = $builder->createForm($myEntity);
$myModel->buildFormSelectOptions($form, $myEntity);
$form->add(array(
'name' => 'submitbutton',
'attributes' => array(
'type' => 'submit',
'value' => 'Submit',
'id' => 'submitbutton',
),
));
$form->add(array(
'name' => 'cancel',
'attributes' => array(
'type' => 'submit',
'value' => 'Cancel',
'id' => 'cancel',
),
));
Note: The example assumes the base form is build via annotations, but it doesn't matter how you create the initial form.
An alternative method to the other answers would be to create a ServiceManager Initializer.
An example of an existing Initializer is how the ServiceManager is injected if your instance implements ServiceLocatorAwareInterface.
The idea would be to create an interface that you check for in your Initialiser, this interface may look like:
interface FormServiceAwareInterface
{
public function init();
public function setServiceManager(ServiceManager $serviceManager);
}
An example of what your Initializer may look like:
class FormInitializer implements InitializerInterface
{
public function initialize($instance, ServiceLocatorInterface $serviceLocator)
{
if (!$instance instanceof FormServiceAwareInterface)
{
return;
}
$instance->setServiceManager($serviceLocator);
$instance->init();
}
}
Anything that happens in init() would have access to the ServiceManager. Of course you would need to add your initializer to your SM configuration.
It is not perfect but it works fine for my needs and can also be applied to any Fieldsets pulled from the ServiceManager.
This is the way I used get around that issue.
firstly, In Module.php, create the service (just as you have done):
// module/Users/Module.php
public function getServiceConfig()
{
return array(
'factories' => array(
'Users\Model\UsersTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$uTable = new UsersTable($dbAdapter);
return $uTable;
},
//I need to get this to the list of groups
'Users\Model\GroupsTable' => function($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
$gTable = new GroupsTable($dbAdapter);
return $gTable;
},
),
);
}
Then in the controller, I got a reference to the Service:
$users = $this->getServiceLocator()->get('Test\Model\TestGroupTable')->fetchAll();
$options = array();
foreach ($users as $user)
$options[$user->id] = $user->name;
//get the form element
$form->get('user_id')->setValueOptions($options);
And viola, that work.

Resources