8

I have a problem with my form,i have this error every time i want to save a comment :

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

firstly i create my 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');
}

I customise my form :

   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);
}

then in my controller i call my form and i save data in DB :

$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)));

and in my twig i do :

<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>  

I remark when i change in my cotroller the object "$comment" with "null" in the line above i didn't have the error but i can"t save data in database...

Anyone have an idea?

1 Answer 1

6

With a collection constraint you can only validate arrays (or anything that implements Traversable). You have to move to annotation in your Comments class.

Sign up to request clarification or add additional context in comments.

3 Comments

that mean I remove getDefaultOptions from my form and I validate all my fields of form in comments Entity using annotation...
@elec1984 yes, exactly. It will work, don't worry. And i think that having a field definition plus validation in the same place (meaning in Comments class) is better for editing.
Hello, I have the same problem. How did you solve this? Expected argument of type "array or (\Traversable and \ArrayAccess)", "string" given

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.