对于我目前正在进行的项目,我需要用户能够添加逗号分隔的电子邮件地址列表(稍后我将使用 selectize )。
我在验证地址时遇到问题,ZF2 自动添加电子邮件地址验证器 ( \Zend\Validator\EmailAddress )。
因为我在其属性中设置了 'multiple' => true
,ZF2 还添加了一个爆炸验证器 ( \Zend\Validator\Explode ),它应该为使用分隔符爆炸后的每个值运行电子邮件地址验证器 (默认为 ',' )。根据ZF2 FormElements manual那就是
验证器适用于 1 个有效或无效的电子邮件地址,但是当我输入 2 个无效的电子邮件地址时,我收到一条通知:“通知:\project\中的数组到字符串转换” vendor\zendframework\zendframework\library\Zend\Validator\AbstractValidator.php 位于第 159 行”。
为了排除我在其他地方做错的事情,我在标准 ZendSkeletonApplication 的单独模块中重新创建了该错误。
(您可以从 github 克隆我的测试:git clone https://github.com/Satsume/ZendSkeletonApplication-Tests.git --recursive
。安装后,转到/tests/查看形式。)
Controller
创建 Controller 非常简单(简化):
<?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,
));
}
}
?>
View
景色也不难。 我在表单标签上添加了“novalidate”,以阻止 html5 浏览器验证电子邮件字段,这样我们可以更轻松地测试 zf2 的验证。
<?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); ?>
结论
你们知道我做错了什么吗?我认为采埃孚不应该受到责备,但是可以吗?
更新
正如 @carlos-robles 指出的,它似乎在 ZF 2.1.5 中工作,我正在使用 dev-master (尽管 2.2 及更高版本的所有版本都有这个问题)
最佳答案
为我工作,使用 ZF 2.1.5。
这里有我得到的屏幕截图,我认为这是预期的结果,对吗?
您使用的是哪个版本的 ZF? 在实际项目中您是否使用自定义错误消息? 如果你看line where you get the error ,它是:
/**
* Returns array of validation failure messages
*
* @return array
*/
public function getMessages()
{
return array_unique($this->abstractOptions['messages']);
}
所以这与错误消息有关
我建议做的是转到该文件(它应该位于/vendor/ZF2//library/Zend/Validator/AbstractValidator.php 中)并编辑函数以查看内容该变量的内容
public function getMessages()
{
print_r($this->abstractOptions['messages']);
die();
return array_unique($this->abstractOptions['messages']);
}
有了这个,您可能会得到有关正在发生的事情的任何线索
关于php - ZendFramework 2 - 表单 - 具有多个无效电子邮件地址的电子邮件字段导致 "Array to string conversion",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21603747/