SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Thread: Zend2 Forms

  1. #1
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Question Zend2 Forms

    Hi there,

    I'm getting a little tied up with Zend forms. There seems to be a number of different ways to set up a form in zf2, and I can't seem to get to the crux of what I'm trying to achieve.

    I wan't to set up a simple form with validation, so that when its processed I can check the isValid() method, and then handle the post data accordingly. Here's what I've got so far:

    PHP Code:
    <?php
    namespace MyForm\Form;

    use 
    Zend\Form\Form;

    class 
    MyForm extends Form{

        public function 
    __construct($name null){
            
    parent::__construct('MyForm');
            
    $this->setAttribute('method''post');
            
    $this->add(
                array(
                    
    'name' => 'id',
                    
    'type' => 'Hidden'
                                    'validators' 
    => array(
                                        
    'required' => true
                                    
    )
                )
            );
            
    $this->add(
                array(
                    
    'name' => 'name',
                    
    'type' => 'Text',
                    
    'attributes' => array(
                        
    'placeholder' => 'Name'
                    
    ),
                                    
    'validators' => array(
                                        
    'required' => true
                                    
    )
                )
            );
            
    $this->add(
                array(
                    
    'name' => 'email',
                    
    'type' => 'Email',
                    
    'attributes' => array(
                        
    'placeholder' => 'Email'
                    
    ),
                                    
    'validators' => array(
                                        
    'required' => true
                                    
    )
                )
            );
            
    $this->add(
                array(
                    
    'name' => 'submit',
                    
    'type' => 'Submit',
                    
    'attributes' => array(
                        
    'value' => 'Go'
                    
    )
                )
            );
        }
    }
    ...and the controller
    PHP Code:
    namespace MyForm\Controller;

    use 
    Zend\Mvc\Controller\AbstractActionController;
    use 
    Zend\View\Model\ViewModel;
    use 
    Application\Model\Application;
    use 
    MyForm\Form\MyForm;

    class 
    MyFormController extends AbstractActionController
    {
        public function 
    indexAction()
        {
            
    $form = new MyForm($status);

            
    $request $this->getRequest();
            if(
    $request->isPost()){

                
    $form->setData($request->getPost());

                   if(
    $form->isValid()){
                       echo 
    "VALID";
                   } else {
                       echo 
    "INVALID";
                   }
            }

            return new 
    ViewModel(array('form' => $form));
        }

    ..and finally the view
    PHP Code:
    <h1>MyForm</h1>

    <?php
    echo $this->form()->openTag($form);
    echo 
    $this->formHidden($form->get('id'));
    echo 
    $this->formRow($form->get('name'));
    echo 
    $this->formRow($form->get('email'));
    echo 
    $this->formRow($form->get('submit'));
    echo 
    $this->form()->closeTag();
    ?>
    Its rendering the form fine, but isValid() always returns false, and there are no error messages displayed on the form.

    Any ideas anyone?

    Many thanks in advance,
    Mike

  2. #2
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,405
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Hi Mike,

    You've got a hidden field 'id' as part of your form, which is set as a required field, but I can't see where the value of the field initially gets assigned?

  3. #3
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    It's not.

    Originally I wanted the form to fail. To test whether it passed, I just added a value to it client side.

    Either way, there are no error messages displayed...

  4. #4
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,405
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    How are you getting the error messages to display? Are you calling $form->getMessages()?

  5. #5
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ah-ha!

    Ok so $form->getMessages() returns an array of error messages, but in the Zend Getting Started guide (http://framework.zend.com/manual/2.2...d-actions.html), the error messages are printed on the form automatically.

    I guess that has something to do with using the InputFilter method they use in the tutorial??

  6. #6
    Community Advisor bronze trophy
    fretburner's Avatar
    Join Date
    Apr 2013
    Location
    Brazil
    Posts
    1,405
    Mentioned
    45 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by mickyginger View Post
    Ah-ha!

    Ok so $form->getMessages() returns an array of error messages, but in the Zend Getting Started guide (http://framework.zend.com/manual/2.2...d-actions.html), the error messages are printed on the form automatically.

    I guess that has something to do with using the InputFilter method they use in the tutorial??
    No I guess that's because in the tutorial they use $this->formCollection($form); to output all the form elements, including errors etc. Check the explanation just before the 'Editing an Album' section.

  7. #7
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by fretburner View Post
    No I guess that's because in the tutorial they use $this->formCollection($form); to output all the form elements, including errors etc. Check the explanation just before the 'Editing an Album' section.
    Yep, I see...

    So since I need to have a bit more control over the layout of my form, I'll have to get the error messages manually, or I guess write a new class to handle that.

    Thanks for your help fretburner.

  8. #8
    SitePoint Addict
    Join Date
    Oct 2009
    Location
    London, UK
    Posts
    382
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    So I just thought I'd update this thread a little after I've gained a little more understanding.

    Firstly, you can't add validators to the Form::add() method. I guess since it takes an array, it just ignores any keys that aren't relevant to the method.

    In order to add validators, you need to create a new class which extends Zend\InputFilter\InputFilter, and then add all the validators to that like so:
    PHP Code:
    <?php
    namespace MyForm\Form;

    use 
    Zend\InputFilter\InputFilter;

    class 
    MyFormFilter extends InputFilter
    {
        public function 
    __construct()
        {
            
    $this->add(
                array(
                    
    'name' => 'id',
                    
    'required' => true,
                )
            );
            
    $this->add(
                array(
                    
    'name' => 'name',
                    
    'required' => true,
                )
            );
            
    $this->add(
                array(
                    
    'name' => 'email',
                    
    'required' => true,
                )
            );
        }
    }
    Then bind it to the form in the controller script:
    PHP Code:
    namespace MyForm\Controller

    use 
    Zend\Mvc\Controller\AbstractActionController
    use 
    Zend\View\Model\ViewModel
    use 
    Application\Model\Application
    use 
    MyForm\Form\MyForm;
    use 
    MyForm\Form\MyFormFilter //import the new class here

    class MyFormController extends AbstractActionController 

        public function 
    indexAction() 
        { 
            
    $form = new MyForm($status); 

            
    $request $this->getRequest(); 
            if(
    $request->isPost()){ 
                
    $form->setInputFilter(new MyFormFilter()); //bind the filter here
                
    $form->setData($request->getPost()); 

                   if(
    $form->isValid()){ 
                       echo 
    "VALID"
                   } else { 
                       echo 
    "INVALID"
                   } 
            } 

            return new 
    ViewModel(array('form' => $form)); 
        } 

    As for outputting the error messages, in the view file, the getRow() method, will render any error message, as long as you prepare the form first:
    PHP Code:
    <h1>MyForm</h1> 

    <?php 
    $form
    ->prepare(); //prepare the form here
    echo $this->form()->openTag($form); 
    echo 
    $this->formHidden($form->get('id')); 
    echo 
    $this->formRow($form->get('name')); 
    echo 
    $this->formRow($form->get('email')); 
    echo 
    $this->formRow($form->get('submit')); 
    echo 
    $this->form()->closeTag(); 
    ?>
    I hope that helps anyone in the same boat.

    M


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •