Can you add an error decorator to a Zend subform? - zend-framework

I have a custom validator that checks all the values in a subform to make sure that they make sense in relation to each other. In the event that this validator fails, I'd like to have an error decorator at the top of the subform to display the error message. Is this possible?
I've already set up the decorators like so:
protected $_decorators = array(
array(
'decorator' => 'FormElements',
'options' => array()
),
array(
'decorator' => 'HtmlTag',
'options' => array(
'tag' => 'ul',
'class' => 'test'
)
),
);
And it seems like I should be able to add
array(
'decorator' => 'Errors',
'options' => array(
'tag' => 'ul',
'class' => 'errors',
'placement' => 'prepend',
)
),
but that causes Zend to fail with the error "htmlspecialchars() expects parameter 1 to be string, array given". What am I doing wrong then? Thanks!

I believe nothing is wrong in your code, just ZF doesn't handle the Errors decorator within Zend_Form_SubForm properly. I hope they will fix this soon.

Related

Zf2 : Controller forwarding/calling generate 404 Error

I am implementing separate auth solution for two different modules described in another question by me.
Zend framework 2 : Add different authentication adapter for two different modules
Now in AuthListener file I write code for forward/call to an different controller/action if authentication failed. That is
$result = $this->adapter->authenticate();
if (!$result->isValid()) {
$response = $event->getResponse();
// Set some response content
$response->setStatusCode(401);
$routeMatch = $event->getRouteMatch();
$routeMatch->setParam('controller', 'First\Controller\Error');
$routeMatch->setParam('action', 'Auth');
}
Now I am getting 404 error - "The requested controller was unable to dispatch the request". First I think I do not added route for Error/Auth, but then I verified it got 404 for all other controller/action too. All are directly accessible through their respective route. But forwarding resulting in 404 error. One important thing - I sending authentication request through phpunit to make unit test cases.
UPDATE : route details :
'routes' => array(
'rest' => array(
'type' => 'Zend\Mvc\Router\Http\Segment',
'options' => array(
'route' => '/rest[/:id]',
'constraints' => array(
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'First\Controller\Index'
),
),
),
'error' => array(
'type' => 'segment',
'options' => array(
'route' => '/rest-error/[/:action][/:id]',
'constraints' => array(
'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[0-9]+',
),
'defaults' => array(
'controller' => 'First\Controller\Error',
'action' => 'auth',
),
),
)
),
'controllers' => array(
'invokables' => array(
'First\Controller\Auth' => 'First\Controller\AuthController',
'First\Controller\Error' => 'First\Controller\ErrorController'
),
),
Module.php
$listener = $serviceManager->get('First\Service\AuthListener');
$listener->setAdapter($serviceManager->get('Rest\Service\BasicAuthAdapter'));
$eventManager->getSharedManager()->attach('First', 'dispatch', $listener, 100);
I also tried to use forward instead of above solutio, But that gives error for circular forward Circular forwarding detected: greater than 10 nested forwards. I think event called when forward called.
try to call your action like that :
$routeMatch->setParam('action', 'auth');
I think the problem might occure because you are listening to MvcEvent::EVENT_DISPATCH. In your listener you set new controller and action variables for RouteMatch but since you are already passed the route event changing those parameters will not have any effect.
You should listen to MvcEvent::EVENT_ROUTE instead and then it will probably work.

forms - Symfony 2 - default attributes for labels and fields in all forms

I am using Symfony2 and Twitter Bootstrap along with some customized styles. I would like to have default classes added to 'text' and 'textarea' fields for the whole project unless specified not to use them.
This is the manual way of achieving what I want in one form Type, however it is not efficient at all.
$builder
->add('name', 'text', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'col-md-4 control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('description', 'textarea', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('referenceNo', 'text', array(
'label' => 'Name',
'label_attr' => array(
'class' => 'col-md-4 control-label'
),
'attr' => array(
'class' => 'form-control input-md'
)
))
->add('client','text',array()) //Don't use styling
Should I create a service and call it inside every form Type to get default settings and then pass it as argument?
Should I extend form_div_layout.html.twig and modify it (form customization)? Or is there a better and more efficient way of achieving this task?
First idea: use traits (http://php.net/traits)
Second idea: extend a base form. Call the parent function to get default values.
Both ways are better than creating a service and calling it every time. I prefer using traits to achieve this.

How do I allow html tags in label for Zend form element using addElement()?

I am brand new to Zend and I've been given a project to make adjustments on. I'd like to add html to the labels for my form elements but I can't seem to get it right.
Here's what I have:
$this->addElement('text', 'school_name', array(
'filters' => array('StringTrim'),
'validators' => array(
array('StringLength', false, array(0, 150)),
),
'required' => true,
'label' => 'Name* :<img src="picture.png">,
'size' => '90',
));
As is, of course, the <img src="picture.png"> text gets escaped and the whole string is displayed.
I've read that I need to use 'escape' => false in some capacity but I can't figure out where/how to use it in my specific case.
Any help would be great. Thanks!
After calling addElement fetch the label's decorator and change the escape setting:
$form->getElement('school_name')->getDecorator('label')->setOption('escape', false);
If you use this type of label a lot, you should consider writing a custom decorator.
You can also use the disable_html_escape in 'label_options' when adding an element to the form:
$this->add(array(
....
'options' => array(
'label' => '<span class="required">Name</span>,
'label_options' => array(
'disable_html_escape' => true,
)
),
...
));
Credit to Théo Bouveret's post 'Button content in ZF2 forms' for the answer.

same controller name in different modules in zf2

I just disvovered today that zf2 doesn't realy like when 2 controllers have the same name even if they are not in the same module.
However, I need to be able to call
localhost/users/types
and
localhost/messages/types
For the moment, my two controllers have the same names.
I also discovered that whatever the name of the module is, I always get the result of messages/types, even with localhost/nonexistingmodule/types oO
Here is what my module.config.php looks like :
return array(
'controllers' => array(
'invokales' => array(
'messages' => 'Messages\Controller\MessagesController,
'messages' => 'Messages\Controller\TypesController,
),
),
'di' => array(
'instance' => array(
'alias' = array(),
),
),
'router' => array(
'routes => array(
'restful' => array(
'type' => 'Zend\Mvc\Router\Http\Segment'
'options' => array(
'route' => '/Messages/:controller[.:formatter][/:id],
'constraints' => array(
'module' => '[a-zA-Z][a-zA-Z0-9_-]*',
'controller' => '[a-zA-Z][a-zA-Z0-9_-]*',
'formater' => '[a-zA-Z][a-zA-Z0-9_-]*',
'id' => '[a-zA-Z0-9_-]*',
),
'defaults' => array(
'module' => 'Messages',
),
),
),
),
),
'view_manager' => array( ... ),
);
I have tried to set 'module' => 'Messages' in constraints (we never know :p) but I had a 404 error.
The module.config.php of the Tasks module is the same but for tasks.
I originaly had aliases but I removed them after I read somewhere this was not very recomanded.
One more thing, this is a REST API, all my controllers extends AbstractRestfulController (in case this is important)
Anyone has an idea of how to make my 2 url to work?
Thanks :)
Aliases for invokables or services and anything else should be unique. If they are not unique they may be overwritten by another module in the order the modules themselfes are loaded. That means: when setting up invokables or any sorts of aliases, make sure that the aliases are unique and meaningful. Personally i name my Controllers like this:
'invokables' => array(
'mymodule-controller-controllername' => 'Mymodule\Controller\ControllernameController'
)
Same with Services or any other sorts of aliases
'services' => array(
'mymodule-service-servicename' => 'Mymodule\Service\Classname'
)
The Documentation follows a Namespace-styled syntax like...
'invokables' => array(
'Mymodule\Controller\Controllername' => 'Mymodule\Controller\ControllernameController'
)
...which personally i find totally confusing, as it resembles a Namespace way too much and doesn't really auto-imply that it is just an alias / key
Now your comments-question i do not understand. You want to match one route to two different controllers? That would be impossible and senseless.
Answer Update with routing config
As for routing configuration you have several possible ways. Personally i put lots of effort into building up literal routes, since they are the fastest, but also require a lot of manual attention. Alternatively there is segment routes which inhibit a little more magic to them. I will cover the literal approach for you:
Module Messages
'controllers' => array(
'invokables' => array(
'messages-controller-index' => 'Messages\Controller\IndexController',
)
),
'router' => array(
'routes' => array(
'messages' => array(
'type' => 'literal',
'options' => array(
'route' => '/messages',
'defaults' => array(
'controller' => 'messages-controller-index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'types' => array(
'type' => 'literal',
'options' => array(
'route' => '/types',
'defaults' => array(
'action' => 'types'
)
)
)
)
)
)
)
Module Tasks
'controllers' => array(
'invokables' => array(
'tasks-controller-index' => 'Tasks\Controller\IndexController',
)
),
'router' => array(
'routes' => array(
'tasks' => array(
'type' => 'literal',
'options' => array(
'route' => '/tasks',
'defaults' => array(
'controller' => 'tasks-controller-index',
'action' => 'index'
)
),
'may_terminate' => true,
'child_routes' => array(
'types' => array(
'type' => 'literal',
'options' => array(
'route' => '/types',
'defaults' => array(
'action' => 'types'
)
)
)
)
)
)
)
So what's happening there is:
if route is /messages if routes to controller-alias messages-controller-index with indexAction()
if route is /messages/types it stays at controller-alias messages-controller-index but goes to typesAction()
if route is /tasks if routes to controller-alias tasks-controller-index with indexAction()
if route is /tasks/types it stays at controller-alias tasks-controller-index but goes to typesAction()
You could obviously change controller aliases and meanings behind that. If you'd want to add an ID for a route like /messages/types/1 you'd build a child-route to the types-route that is of type segment and check for an [:id] parameter whose constraints should be numeric :) Check the official ZF2 Manual for more information, i'm lazy now :P
tl;dr: Make sure you have a __NAMESPACE__ default in your route definition, and then make sure all your service names are prefixed with it. Usually, the value will match your module namespace.
For the longer explanation:
I'm going to be blunt: it's a really bad practice to have a dynamic segment in your route that maps to the controller (:controller in your example). The reason is that if somebody discovers this, they can do controller injection, and request a controller that should not be routed by this particular route.
Additionally, the practice can easily lead to collisions, particularly if you omit a __NAMESPACE__ default in your route configuration -- which, based on the question, you've already discovered.

How to remove Zend Form error messages?

I have changed decorator:
private function _addErrorDecorator($form)
{
$form->setDecorators(array(
'FormElements',
new Zend_Form_Decorator_FormErrors(array
(
'ignoreSubForms' => true,
'markupElementLabelEnd' => '</b>',
'markupElementLabelStart' => '<b>',
'markupListEnd' => '</div>',
'markupListItemEnd' => '</span>',
'markupListItemStart' => '<span>',
'markupListStart' => '<div id="Form_Errors">'
)
),
'Form'
));
return $form;
}
But now i need to remove error messages under form fields. How do i make it?
Each element, subform and display group in your form has a decorator stack as well, so you will need to modify the stack for the elements you want to not display the error messages.
There's a lot of ways to do this:
$form->setElementDecorators(array(
'ViewHelper',
'HtmlTag',
'Label'
));
Is the way to go if you want to keep the default element decorator stack, but with the error decorator removed. You can also do it on an individual element basis:
$element->setDecorators(array(
'ViewHelper',
'HtmlTag',
'Label'
));
Or when you are adding the element:
$form->addElement($type, $name, array(
'decorators' => $decorators
))