Wednesday, 20 November 2013

Make Entry Form in Zend Framwork 2

 
Zend Framework 2 uses a module system and you organise your main application-specific code within each module. The Application module provided by the skeleton is used to provide bootstrapping, routing and error configuration to the whole application. It is usually used to provide application level controllers for, the home page of an application.
 

1.Set a New Module like :

 zf2-tutorial/
     /module
         /UserView
             /config
             /src
                 /UserView
                     /Controller
                     /Form
                     /Model
             /view
                 /user-view
                     /user-view
 
 
 2.Open application/application-config.php, add new module "UserView"

    'modules' => array(
        'Application',
        'UserView',     
    ),

3.Create new table in your database which will define in global.php2

Create New Table :

CREATE TABLE IF NOT EXISTS `tbl_teambeachbody` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `email` varchar(100) NOT NULL,
  `password` int(11) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

4.Make UserView\Form\UserViewForm;

<?php

namespace UserView\Form;

use Zend\Form\Form;

class UserSignupForm extends Form {
    public function __construct($name = null) {
        // we want to ignore the name passed
        parent::__construct('UserSignUp');
        $this->setAttribute('method', 'post');
       
     $this->add(array(
            'name' => 'email',
            'type' => 'Email',
            'options' => array(
                'label' => 'Email',
            ),
        ));
    
      $this->add(array(
            'name' => 'password',
            'type' => 'password',
            'options' => array(
                'label' => 'Password',
            ),
        ));
    
      $this->add(array(
            'name' => 'cpassword',
            'type' => 'password',
            'options' => array(
                'label' => 'Confirm Password',
            ),
        ));
      $this->add(array(
            'name' => 'signup',
            'type' => 'submit',
            'options' => array(
                'label' => 'Sign up',
            ),
        ));
     
    }
}
?>

5.Make view/user-view/user-view/signup.phtml

<?php
// module/UserView/view/userview/userview/edit.phtml:

$title = 'SIGNUP';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>

<?php
$form = $this->form;
//var_dump($form);die;
$form->setAttribute('action', $this->url(
                'userview', array(
            'action' => 'signup',
                )
));
$form->prepare();
?>
<?php
echo $this->form()->openTag($form);
?>
<table class="table-bordered">

    <tr class="row">
        <td><?php echo $this->formRow($form->get('email')); ?></td>
    </tr>
    <tr class="row">
        <td><?php echo $this->formRow($form->get('password')); ?></td>
    </tr>

    <tr class="row">
        <td><?php echo $this->formRow($form->get('cpassword')); ?></td>
    </tr>
    <tr class="row">
        <td><?php echo $this->formRow($form->get('signup')) ?></td>
    </tr>

</table>
<?php
echo $this->form()->closeTag($form);
?>

6. Make UserView/Model/UserSignUp.php 

<?php

namespace UserView\Model;

// Add these import statements
use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilterInterface;
use Zend\Db\Adapter\Adapter;

class UserSignUp implements InputFilterAwareInterface {

    public $id;
    public $email;
    public $password;
  
    protected $inputFilter;

    public function __construct() {
       
    }

        public function exchangeArray($data) {
        $this->id = (isset($data['id'])) ? $data['id'] : null;
        $this->email = (isset($data['email'])) ? $data['email'] : null;
        $this->password = (isset($data['password'])) ? $data['password'] : null;
    }

    public function getArrayCopy() {
        return get_object_vars($this);
    }

    // Add content to these methods:
    public function setInputFilter(InputFilterInterface $inputFilter) {
        throw new \Exception("Not used");
    }

    public function getInputFilter() {
        if (!$this->inputFilter) {
            $inputFilter = new InputFilter();
            $factory = new InputFactory();

            $inputFilter->add($factory->createInput(array(
                        'name' => 'id',
                        'required' => true,
                        'filters' => array(
                            array('name' => 'Int'),
                        ),
            )));
           
            $inputFilter->add($factory->createInput(array(
                        'name' => 'email',
                        'required' => true,
                        'filters' => array(
                            array('name' => 'StripTags'),
                            array('name' => 'StringTrim'),
                        ),
                        'validators' => array(
                            array(
                                'name' => 'StringLength',
                                'options' => array(
                                    'encoding' => 'UTF-8',
                                    'min' => 1,
                                    'max' => 100,
                                ),
                            ),
                        ),
            )));

            $inputFilter->add($factory->createInput(array(
                        'name' => 'password',
                        'required' => true,
                        'filters' => array(
                            array('name' => 'StripTags'),
                            array('name' => 'StringTrim'),
                        ),
                        'validators' => array(
                            array(
                                'name' => 'StringLength',
                                'options' => array(
                                    'encoding' => 'UTF-8',
                                    'min' => 1,
                                    'max' => 15,
                                ),
                            ),
                        ),
            )));
     
            $this->inputFilter = $inputFilter;
        }

        return $this->inputFilter;
    }

}

 7. Make UserView/Model/UserSignUpTable.php 

<?php

namespace UserView\Model;

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Adapter\Adapter;

class UserSignUpTable {

    protected $tableGateway;

    public function __construct(TableGateway $tableGateway) {
       
        $params = array('host'      =>'localhost',
                      'username'  =>'root',
                      'password'  =>'',
                      'dbname'    =>'mdc'
                     );
          
        $this->tableGateway = new Zend_Db_Adapter_Pdo_Mysql($params);
    }


    public function saveUserView(UserView $userview) {
        $data = array(
            'email' => $userview->email,
            'password' => $userview->password
        );
       
       
            $this->tableGateway->insert('tbl_teambeachbody',$data);
       
    }


}

8. Make UserView/Controller/UserViewController.php 

<?php

namespace UserView\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel      

use UserView\Form\UserSignupForm;   // <-- Add this importuse UserView\Model\UserSignUp;   // <-- Add this importuse Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;

class UserViewController extends AbstractActionController {

    public function signupAction() {

        $form = new UserSignupForm();

        $form->get('signup')->setValue('Sign Up');

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

            $signup = new UserSignUp();
            $form->setInputFilter($signup->getInputFilter());

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


            if ($request->getPost('email') != "" && $request->getPost('password') != "" && $request->getPost('password') == $request->getPost('cpassword')) {


                $sm = $this->getServiceLocator();
                $dbAdapter1 = $sm->get('Zend\Db\Adapter\Adapter');
                $sql = new Sql($dbAdapter1);
                $insert = $sql->insert('tbl_teambeachbody');
                       
                $data = array(
                    'email' => $request->getPost('email'),
                    'password' => $request->getPost('password')
                );
                $insert->values($data);
                $statement = $sql->prepareStatementForSqlObject($insert);
                $result = $statement->execute();
                //print_R($result);
                return $this->redirect()->toRoute('userview', array(
                            'action' => 'edit'
                ));
            }
        }

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

}