2012-05-13 8 views
8

tengo un problema con mi forma, tengo este error cada vez que quiera guardar un comentario:argumento esperado de tipo array o de Traversable y ArrayAccess, objeto dado

argumento esperado de tipo array o de Traversable y ArrayAccess, objeto dado

primer lugar creo mi commentType clase:

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

personalizar mi formulario:

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

entonces en mi controlador de llamar a mi forma y puedo guardar los datos en la base de datos:

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

y en mi rama que hago:

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

hago notar, Cambio en mi cotroller el objeto "$ comment" con "null" en la línea de arriba, no tuve el error, pero no puedo guardar datos en la base de datos ...

¿Alguien tiene una idea?

Respuesta

6

Con una restricción de recopilación, solo puede validar matrices (o cualquier otra que implemente Traversable). Tienes que pasar al annotation en tu clase Comments.

+0

eso significa que elimino getDefaultOptions de mi formulario y valido todos mis campos de formulario en comentarios Entidad usando la anotación ... – Nll

+0

@ elec1984 sí, exactamente. Funcionará, no te preocupes. Y creo que tener una definición de campo más validación en el mismo lugar (es decir, en la clase de Comentarios) es mejor para la edición. – gremo

+0

OK Espero que sí Geremo, tns – Nll

Cuestiones relacionadas