zend framework 2 how disable multiple errors messages of validator - forms

I am trying to de that the EmailValidator class return me only one error messages.
In other forums I have seen that in ZF1 only is possible make it extending the Validator class.
Anybody know if in ZF2 there is a more easy way for make it?
This is my code for if anyone it help, in this I call setMessages method for overwrite the default messagestemplate, but not works.
$form = new ParticipantForm();
$mailInput = new Input('mail');
$validator = new ValidatorEmailAddres();
$validator->setMessages(
array(
ValidatorHostName::UNKNOWN_TLD => 'hola'
)
);
$mailInput->getValidatorChain()->addValidator($validator);
$mailInputFilter = new InputFilter();
$mailInputFilter->add($mailInput);
$form->setInputFilter($mailInputFilter);
$form->setData($_POST);

Try this:
$validator = new \Zend\Validator\EmailAddres();
$validator->setMessage('something broke bla');
or to set a specific message:
$validator = new \Zend\Validator\EmailAddres();
$validator->setMessage('something broke bla', \Zend\Validator\EmailAddres::LENGTH_EXCEEDED);

Related

Zf2 multicheckbox at least one element is always required

I can't validate a zf2 form with multicheckbox because at least one checkbox is always required.
I found a lot of reference to this issue (for example here - https://github.com/zendframework/zf2/issues/4845), but i didn't found a solution for this.
Does anybody know how to solve this problem ?
UPDATE: I use a doctrine 2 objectmulticheckbox which is extended from zf2 multichechbox. As is commented below the override of getInputFilterSpecification method, will solve the problem with form validation, but the values will still remain in database (values populated by objectmulticheckbox).
I found a seemingly easier way to get around this issue by setting the input filter 'required' to false inside the controller, after the form is instantiated.
<?php
$form = new CampaignForm($multiCheckboxOptions); // Setting up checkbox in form class
$form->getInputFilter()->get('my_multi_checkbox')->setRequired(false);
?>
You can override the getInputFilterSpecification function on your form to set the field to not be required. For example:
public function getInputFilterSpecification() {
return array(
[...]
'the-multi-checkbox-field' => array(
'required' => false,
),
[...]
);
}
Ok i did a little hack to solve this problem.
So I added this code in the action controller:
$form->bind($client);
/** #var $request Request */
$request = $this->getRequest();
if ($request->isPost()) {
$form->setData($request->getPost());
if ($form->isValid()) {
/** #var $client Client */
$client = $form->getData();
// hack because of - https://github.com/zendframework/zf2/issues/4694
if($request->getPost('reportSettings') === null){
$client->setReportSettings(null); // set null to remove all associations with this client
}
And also as it is described in the first answere, in form should be rewritten getInputFilterSpecification method, for field that shouldn't be required.

how to make optional parametres in zendframework URL

I am new to Zend, but very very keen to learn. This is really just a quick question on routing in Zend Framework.
I understand the basic of it but I am still confused about how I can create some optional parameters at the end of my URL. For example, I have the following default page URL:
examplesite.com/accounts/enquiry
I now want to add two additional parameters to it i.e:
userid= 6
location= 12
So, the eventual URL should look like:
examplesite.com/accounts/enquiry/6/12
but
examplesite.com/accounts/enquiry
Will get you to the same page.
I am not clear. How do it do this? I mean, this is not a bespoke URL. so, I don't need to create a custom route. It basically just the last two parameters that need to be added to the page.
How do I do this?
First 2 parameters are controller and action name, the named params.
Here you are:
examplesite.com/accounts/enquiry/userid/6/location/12
or you can define your own route like this:
$route = new Zend_Controller_Router_Route('accounts/enquiry/:userid/:location);
and then add it to router:
$router->addRoute('accounts', $route);
You could add a custom route inside your Bootstrap.php, e.g. (untested):
protected function _initRoutes()
{
[...]
$frontController = Zend_Controller_Front::getInstance();
$router = $frontController->getRouter();
$accounts = new Zend_Controller_Router_Route(
'accounts/enquiry/:userid/:location',
array(
'userid' => '[0-9]{2}',
'location' => '[0-9]{2}',
'controller' => 'accounts',
'action' => 'enquiry',
)
);
$router->addRoute('accounts', $accounts);
[...]
}
http://framework.zend.com/manual/1.12/en/zend.controller.router.html

Class Sofzo_From not found error when implementing Sozfo TinyMCE solution

I'm trying to implement TinyMCE to text areas using the solution mentioned in Sofzo. But when I try to extend the Sofzo_Form I get the following error :
Fatal error: Class 'Sozfo_Form' not found in /home/foldername/public_html/application/forms/PageForm.php on line 4
What I have done so far -
Uploaded the Sofzo files to library with following directory structure
/library
../Sozfo
../Form.php
../../Form
../../../Element
../../../../TinyMce.php
../../View
../../../Helper
../../../Exception.php
../../../../FormTinyMce.php
../../../../TinyMce.php
Loaded the classes in application.ini as
Autoloadnamaspaces[] = "Sofzo_"
And in bootstrap as
$autoLoader = Zend_Loader_Autoloader::getInstance();
$autoLoader->registerNamespace('Zend_');
$autoLoader->registerNamespace('SF_');
$autoLoader->registerNamespace('CMS_');
$autoLoader->registerNamespace('Sofzo_');
$loader = new Zend_Loader_PluginLoader();
$loader->addPrefixPath('Zend_View_Helper', 'Zend/View/Helper/')
->addPrefixPath('Storefront_View_Helper',
'application/modules/storefront/views/helpers')
->addPrefixPath('Sozfo_Form', 'Sozfo/');
$view=new Zend_View();
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
But when I try to extent the Sofzo_Form in Page_Form as
class Form_PageForm extends Sozfo_Form { }
This issue was solved thanks to Tim Fountain. But now when I load an element as
$this->addElement('tinyMce', 'message', array(
'label' => 'Message',
'required' => true,
'cols' => '50',
'rows' => '10',
'editorOptions' => new Zend_Config_Ini(APPLICATION_PATH . '/configs/tinymce.ini', 'moderator')
));
I get the following error
Plugin by name 'FormTinyMce' was not found in the registry
Read through several comments in original site and they are said to add
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
to bootstrap. I've already done that, but I'm guessing I'm not doing something right. Help is much appreciated.
I think the issue is ZF can't find the class because it doesn't know about the Sozfo_ namespace. You've attempted to register this namespace in two different ways, but both of them are incorrect.
In application.ini, you have:
Autoloadnamaspaces[] = "Sofzo_"
But this should be:
autoloaderNamespaces[] = "Sozfo_"
Then in the bootstrap you've tried to register it with:
$autoLoader->registerNamespace('Sofzo_');
but presumably this should be:
$autoLoader->registerNamespace('Sozfo_');
(note spelling). Which ever fix you apply you should only use one of these methods, as they do the same thing.
If it still doesn't work after that then there's an issue with your include_path.
Edit: To fix the view helper path, try this instead of the two lines you currently have:
$view = new Zend_View();
$viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
$stack = Zend_Controller_Action_HelperBroker::getStack();
$stack->push($viewRenderer);
$view->addHelperPath('Sozfo/View/Helper', 'Sozfo_View_Helper');
This adds the helper path to a view object like you have but also supplies it to the view renderer (which is what renders all the view scripts). If you don't do this then the view renderer uses its own view object, so the view object you setup in the bootstrap is never used for anything.
If this doesn't work, try passing a full path as the first parameter to addHelperPath instead:
$view->addHelperPath(APPLICATION_PATH.'/../library/Sozfo/View/Helper', 'Sozfo_View_Helper');

Create model instance in Form in ZF2 like ZF1?

In ZF1 it is possible to create an instance of a model and also access its properties from any form class.`
class Application_Form_Drydepot extends Zend_Form
{
$model = new Application_Model_DbTable_DrydepotModel();
$List = $model ->formationSelect();
array_unshift($List, array('key' => '', 'value' => '--Please Select--'));
$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int')
->setDecorators($this->elementDecoration);
$formation = new Zend_Form_Element_Select('formation_id');
$formation->setLabel('Formation Name')
->setRequired(true)
->setAttrib('id', 'formation')
->setAttrib('class', 'required')
->addValidator('NotEmpty', true)
->setMultiOptions($List)
->setDecorators($this->elementDecoration);
}
In here $model directly possible to call but use it easily but zf2 it is quite difficult. I am not successfull about to do it. In ZF2 how do I do it same operation.
Another ways are like here : the documentation
Programmatic Form Creation
ZF2 Coding close to ZF1
use Zend\Captcha;
use Zend\Form\Element;
use Zend\Form\Form;
$captcha = new Element\Captcha('captcha');
$captcha
->setCaptcha(new Captcha\Dumb())
->setLabel('Please verify you are human');
$form = new Form('my-form');
$form->add($captcha);

Need to even further shorten easy urls using Zend Route

I might be asking a bit too much here but I would like to know how can I further shorten urls such that they look like there's not an inclusion of a controller.
eg: I want to shorten this:
www.mysite.com/users/Bob-123
to
www.mysite.com/Bob-123
or something like www.mysite.com/bob-123-user
Any ideas here? - I'm using the Zend framework here
You can do with the help of Zend Router .
Here you need to fetch the usernames and add routes to it. This way it will recognise which controller and action it needs to fetch.
$router = $this->_front->getRouter();
$route = new Zend_Controller_Router_Route(
':username',
array(
'controller' => 'profile',
'action' => 'userinfo'
)
);
$router->addRoute('user', $route);
An example taken from zend manual
In your application.ini you could do it using Zend_Controller_Router_Route_Regex:
resources.router.routes.user.route = "(\w+)-(\d+)"
resources.router.routes.user.type = "Zend_Controller_Router_Route_Regex"
resources.router.routes.user.defaults.controller = users
resources.router.routes.user.defaults.action = index
resources.router.routes.user.map.1 = username
resources.router.routes.user.map.2 = id
resources.router.routes.user.reverse = "/%s-%d"
You would need to specify your own action and controller.
Hope this helps.