Reputation: 1080
For a project I'm currently working on, I need users to be able to add a comma seperated list of e-mailaddresses ( I will be using selectize later on ).
I'm having trouble validating the addresses, ZF2 automatically adds a e-mailaddress validator ( \Zend\Validator\EmailAddress).
Because I set 'multiple' => true
in it's attributes, ZF2 also adds a explode validator ( \Zend\Validator\Explode), which should run the e-mailaddress validator for every value once exploded using the seperator ( by default a ',' ). According to the ZF2 FormElements manual that is
The validator works for 1 valid or invalid e-mailaddress, when I however enter 2 invalid e-mailaddress I get a notice: "Notice: Array to string conversion in \project\vendor\zendframework\zendframework\library\Zend\Validator\AbstractValidator.php on line 159".
To rule out anything I did wrong elsewhere, I recreated the bug in a seperate module for the standard ZendSkeletonApplication.
( You can clone my tests from github: git clone https://github.com/Satsume/ZendSkeletonApplication-Tests.git --recursive
. Once installed go to /tests/ to see the form. )
Creating the controller is pretty easy ( simplified ):
<?php
namespace Tests\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
class IndexController extends AbstractActionController
{
public function indexAction()
{
// Lets build a form the quick-way:
$form = new \Zend\Form\Form();
// Add the multiple email field:
// This adds a \Zend\Validator\EmailAddress
// And because it's a multiple, a \Zend\Validator\Explode
$form->add(
array(
'type' => 'Zend\Form\Element\Email',
'name' => 'emails',
'options' => array(
'label' => 'E-mailaddresses',
),
'attributes' => array(
'multiple' => true,
),
)
);
$validates = false;
// Check if this is a POST request
$request = $this->getRequest();
if ($request->isPost()) {
// Set the data in the form to validate it:
$form->setData($request->getPost());
// Now validate it:
$validates = $form->isValid();
}
// Send the variables to the view:
return new ViewModel(array(
'validates' => $validates,
'form' => $form,
));
}
}
?>
The view isn't that hard either. I added a 'novalidate' on the form tag to stop the html5 browsers from validating the email field, this way we can test zf2's validation more easily.
<?php
/* @var $this \Zend\View\Renderer\PhpRenderer */
/* @var $form \Zend\Form\Form */
$form = $this->form;
$form->setAttribute('action', '?');
$form->setAttribute('method', 'post');
$form->setAttribute('novalidate', 'novalidate');
$form->prepare();
?>
<?php if($this->validates): ?>
<div class="alert alert-success">It seems the form validates! Well done!</div>
<?php else: ?>
<div class="alert alert-danger"><strong>Ow snap!</strong> It seems the form doesn't validate...</div>
<?php endif; ?>
<?php echo $this->form()->openTag($form); ?>
<?php foreach ($form as $element): ?>
<?php $element->setAttribute('class', 'form-control'); ?>
<div class="form-group">
<?php echo $this->formRow($element) ?>
</div>
<?php endforeach ?>
<div class="form-group">
<button type="submit" class="btn btn-primary">Submit</button>
</div>
<?php echo $this->form()->closeTag($form); ?>
Do any of you know what it is I'm doing wrong? I assume ZF isn't to blame, but could it?
As @carlos-robles pointed out, it does seem to work in ZF 2.1.5, I'm using dev-master ( Although everything from 2.2 and up has this issue )
Upvotes: 2
Views: 576
Reputation: 1080
After some fiddling ( partially per Carlos' suggestion ) I found that the contents of $this->abstractOptions['messages']
in AbstractValidator.php:159
contained a multi-dimensional array. As was to be expected by the message in the notice.
array_unique, by default, tries to convert the values to string for comparison (SORT_STRING
). And thus with a multi-dimensional array it tries to convert an array to a string...
The solution was to change this default behavior by passing a second parameter to array_unique: SORT_REGULAR
, this will let the values remain of their original type
A pull-request was made for the zf2 master branch to correct this: https://github.com/zendframework/zf2/pull/5808
Upvotes: 1
Reputation: 10947
working for me, with ZF 2.1.5.
Here you have an screenshot of what i get, i think this is the expected result, righ?
What version of ZF are you using? in the real project are you using custom error messages? If you look at the line where you get the error, it is:
/**
* Returns array of validation failure messages
*
* @return array
*/
public function getMessages()
{
return array_unique($this->abstractOptions['messages']);
}
so it is something related with error messages
What i recommend to do is to go to that file (it should be in /vendor/ZF2//library/Zend/Validator/AbstractValidator.php) and edit the function to see what is the contents of that variable
public function getMessages()
{
print_r($this->abstractOptions['messages']);
die();
return array_unique($this->abstractOptions['messages']);
}
With that probably you will get any clue about what is happening
Upvotes: 1