Wednesday, February 19, 2014

Validate email in ZF2

Use below code in your Model class:

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

        $inputFilter->add($factory->createInput(array(
            'name' => 'email',
            'required' => true,
            'filters' => array(
                array('name' => 'StripTags'),
                array('name' => 'StringTrim'),
            ),
            'validators' => array(
                array(
                'name' => 'EmailAddress',
                'options' => array(
                    'messages' => array(\Zend\Validator\EmailAddress::INVALID_FORMAT => 'Email address format is invalid',
                    )
                ),
                ),
            ),
        )));
        $this->inputFilter = $inputFilter;
    }
    return $this->inputFilter;
}

Tuesday, February 18, 2014

How to create radio button using ZF2 form

Create a file ModuleNameForm.php file in src/ModuleName/Form direcotry.

<?php

namespace ModuleName\Form;

use Zend\Form\Form;

class ModuleNameForm extends Form
{
    public function __construct($name = null)
    {
    parent::__construct('____');
   
    $this->add(array(
        'name' => 'country',
        'type' => 'Radio',
        'options' => array(
        'label' => 'Country',
        'value_options' => array(
            'USA' => 'USA',
            'CAN' => 'Canada',
        ),
        )
    ));
    } 
}

Monday, February 17, 2014

Store and return zend template as a variable instead of printing it to the browser

Below is the code for your controller in which I am return the phtml in JSON. It can we used for AJAX:

use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use Zend\View\Renderer\PhpRenderer;
use Zend\View\Resolver;

public function searchClientAction()
{ 
     $renderer = new PhpRenderer();
     $map = new Resolver\TemplateMapResolver(array(
         'client_search' => __DIR__ . '/../../../view/Client/Client/prepareHTML.phtml',

     ));

     $resolver = new Resolver\TemplateMapResolver($map);
     $renderer->setResolver($resolver);

     $view = new ViewModel(array(
         'message' => 'Hello world',
     ));

     $view->setTemplate('client_search');

     $result = new JsonModel(array(
         'client_name' => $renderer->render($view),
         'success' => true,
     ));

     return $result;
}
Update module.config.php:
'view_manager' => array(
    .
    .
    .
    'strategies' => array(
            'ViewJsonStrategy',
    ),

Multiple DB Connection in ZF2


1. update global.php
return array(
    'db' => array(
    'adapters' => array(
        'db' => array(
        'driver' => 'Pdo',
        'dsn' => 'mysql:dbname=db1;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
        ),
        'db_securitas' => array(
        'driver' => 'Pdo',
        'dsn' => 'mysql:dbname=db2;host=localhost',
        'driver_options' => array(
            PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'UTF8\''
        ),
        ),
    ),
    ),
    'service_manager' => array( 
        'abstract_factories' => array(
            'Zend\Db\Adapter\AdapterAbstractServiceFactory',
        ),
    ),
);
2. Update local.php
return array(
    'db' => array(
    'adapters' => array(
        'db1' => array(
        'username' => 'user',
        'password' => 'pwd',
        ),
        'db2' => array(
        'username' => 'user',
        'password' => 'pwd',
        ),
    ),
    ),
);
3. Use adapter in Module.php of your module:
     // Add this method:
public function getServiceConfig()
{
return array(
    'factories' => array(
    'ModuleABC\Model\ABCTable' => function($sm)
    {
        $tableGateway = $sm->get('ABCTableGateway');
        $table = new ClientTable($tableGateway);
        return $table;
    },
    'ABCTableGateway' => function ($sm)
    {
        $dbAdapter = $sm->get('db1');
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new ABC());
        return new TableGateway('abc', $dbAdapter, null, $resultSetPrototype);
    },
    'ModuleXYZ\Model\XYZTable' => function($sm)
    {
        $tableGateway = $sm->get('XYZTableGateway');
        $table = new BranchTable($tableGateway);
        return $table;
    },
    'XYZTableGateway' => function ($sm)
    {
        $dbAdapter = $sm->get('db2');
        $resultSetPrototype = new ResultSet();
        $resultSetPrototype->setArrayObjectPrototype(new XYZ());
        return new TableGateway('xyz', $dbAdapter, null, $resultSetPrototype);
    },
    ),
    );
}
4. Done!