Ver Mensaje Individual
  #51 (permalink)  
Antiguo 21/06/2012, 06:59
Avatar de masterpuppet
masterpuppet
Software Craftsman
 
Fecha de Ingreso: enero-2008
Ubicación: Montevideo, Uruguay
Mensajes: 3.550
Antigüedad: 16 años, 3 meses
Puntos: 845
Respuesta: Zend Framework 2 Beta 1

Buenas,

Hace un par de dias se hizo el último gran update de los forms, el cual trae Annotations , un pequeño ejemplo por si quieren meter mano:

Entity
Código PHP:
Ver original
  1. namespace Application\Entity;
  2.  
  3. use Doctrine\ORM\Mapping as ORM,
  4.     Zend\Form\Annotation as Form,
  5.     Zend\Stdlib\ArraySerializableInterface,
  6.     Application\Model\PostInterface,
  7.     DateTime;
  8.  
  9. /**
  10.  * @ORM\Entity()
  11.  * @ORM\HasLifecycleCallbacks
  12.  */
  13. class Post implements PostInterface, ArraySerializableInterface
  14. {
  15.     const NOT_DRAFT = 0;
  16.     const DRAFT     = 1;
  17.     const NOT_TRASH = 0;
  18.     const TRASH     = 1;
  19.  
  20.     /**
  21.      * @ORM\Id
  22.      * @ORM\Column(type="integer")
  23.      * @ORM\GeneratedValue    
  24.      * @Form\Attributes({"type":"hidden"})
  25.      * @Form\AllowEmpty
  26.      */
  27.     protected $id;
  28.  
  29.     /**  
  30.      * @ORM\Column()
  31.      * @Form\Required
  32.      * @Form\Filter({"name":"StripTags"})
  33.      * @Form\Filter({"name":"StringTrim"})
  34.      * @Form\Filter({"name":"StringToUpper"})
  35.      * @Form\Validator({"name":"Alnum"})         
  36.      */
  37.     protected $title;
  38.  
  39.     /**        
  40.      * @ORM\Column()     
  41.      * @Form\Attributes({"type":"textarea"})
  42.      * @Form\Filter({"name":"StripTags"})        
  43.      * @Form\Filter({"name":"StringTrim"})   
  44.      */
  45.     protected $content;
  46.  
  47.     /**
  48.      * @ORM\Column(name="created_at", type="datetime")   
  49.      * @Form\AllowEmpty
  50.      * @Form\Validator({"name":"Date"})
  51.      */
  52.     protected $createdAt;
  53.  
  54.     /**
  55.      * @ORM\Column(name="updated_at", type="datetime")   
  56.      * @Form\AllowEmpty
  57.      * @Form\Validator({"name":"Date"})
  58.      */    
  59.     protected $updatedAt;
  60.  
  61.     /**
  62.      * @ORM\Column(type="boolean")
  63.      */    
  64.     protected $draft;
  65.  
  66.     /**
  67.      * @ORM\Column(type="boolean")
  68.      */        
  69.     protected $trash;
  70. }

Controller
Código PHP:
Ver original
  1. public function indexAction()
  2. {              
  3.     $request = $this->getRequest();
  4.     $builder = new AnnotationBuilder();
  5.     $entity  = new Post();
  6.     $form    = $builder->createForm($entity);
  7.     $form->add(array(
  8.         'name' => 'submit',
  9.         'attributes' => array(
  10.             'type' => 'submit'
  11.         )
  12.     ));
  13.     $form->bind($entity);
  14.     if($request->isPost()){        
  15.         $form->setData($request->post());
  16.         if($form->isValid()) {          
  17.             $em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
  18.             $em->persist($form->getData());
  19.             $em->flush();              
  20.             $this->flashMessenger()->addMessage('Entity persisted.');
  21.             $this->redirect()->toRoute('home');
  22.         }
  23.     }
  24.     return new ViewModel(array('form' => $form));
  25. }

sencillamente fantástico :alabanza:, tengan en cuenta que es la primera aproximación.

Saludos.
__________________
http://es.phptherightway.com/
thats us riders :)