gpt4 book ai didi

symfony - 类型数组或 Traversable 和 ArrayAccess 的预期参数,给定对象

转载 作者:行者123 更新时间:2023-12-04 08:13:10 25 4
gpt4 key购买 nike

我的表单有问题,每次我想保存评论时都会出现此错误:

Expected argument of type array or Traversable and ArrayAccess, object given



首先我创建我的类(class) commentType :
    public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('comment', 'textarea')
->add('commentedName', 'text')
->add('idNews','hidden', array('error_bubbling'=>true))
->add('country', 'text')
->add('email','email')
->add('captcha', 'captcha');
}

我自定义我的表单:
   public function getDefaultOptions(array $options){

$collectionConstraint = new Collection (array (
'email' => new Email(array('message' => 'invalid address email')),
'commentedName' => array(new MaxLength(array('limit' => 30, 'message' => ''))),
'comment' => array(new MinLength(array('limit' => 20, 'message' => ''))),
'country' => new MinLength(array('limit' => 3, 'message' => '')),
'idNews' => array(new MaxLength(array('limit' => 30, 'message' => ''))),
));

return array('validation_constraint' => $collectionConstraint);
}

然后在我的 Controller 中调用我的表单并将数据保存在数据库中:
$comment =  new \Mybundle\BackendBundle\Entity\Comments();

$form = $this->createForm(new \Mybundle\MainBundle\Form\CommentType(), $comment);

$request = $this->get('request');
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);

if ($form->isValid()) {
$idNews= $this->getDoctrine()
->getRepository('Mybundle\BackendBundle\Entity\News')
->find($id);

$comment->setIdNews($idNews);

$em = $this->getDoctrine()->getEntityManager();
$em->persist($comment);
$em->flush();

return $this->redirect($this->generateUrl('News',array('id' => $id,'slug'=>$slug)));

在我的 Twig 上,我这样做:
<table style="float: right;" >
<tbody>
<tr>
<td>{{ form_widget(form.commentedName) }} </td>
<td>{{ form_label(form.commentedName, "name") }}</td>
</tr>
<tr>
<td class="comment-error">{{ form_errors(form.commentedName) }}</TD>
</tr>
<tr>
<td>{{ form_widget(form.country) }} </td>
<td>{{ form_label(form.country, "country") }}</td>
</tr>
<tr>
<td class="comment-error">{{ form_errors(form.country) }}</TD>
</tr>
<tr>
<td class="large_text">{{ form_widget(form.email) }} </td>
<td>{{ form_label(form.email, "address") }}</td>
</tr>
<tr>
<td class="comment-error">{{ form_errors(form.email) }}</td>
</tr>
<tr>
<td>{{ form_widget(form.comment) }} </td>
<td>{{ form_label(form.comment, "comment") }}</td>
</tr>
<tr>
<td class="comment-error">{{ form_errors(form.comment) }}</td>
</tr>
<tr>
<td>{{ form_widget(form.captcha) }} </td>
</tr>

<tr>
<td class="comment-error">{{ form_errors(form) }}</td>
</tr>


当我在我的 Controller 中更改对象“$comment”和上面一行中的“null”时,我说我没有错误,但我无法将数据保存在数据库中...

有人有想法吗?

最佳答案

使用集合约束,您只能验证数组(或任何实现 Traversable 的东西)。您必须移至 annotation在您的 Comments类(class)。

关于symfony - 类型数组或 Traversable 和 ArrayAccess 的预期参数,给定对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10575309/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com