https://framework.zend.com/

Use validator to validate password

The password requirement normally must contain at least ONE digit, ONE uppercase, ONE lowercase and at least few characters long.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<?php
$pattern = '/^(?=.*\d)(?=.*[A-Z])(?=.*[a-z])[0-9A-Za-z!@#$%]{6,}$/';

$input_filter->add($factory->createInput(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StripTags'),
array('name' => 'StringTrim'),
),
'validators' => array(
array(
'name' => 'NotEmpty',
'options' => array(
'messages' => array(
\Zend\Validator\NotEmpty::IS_EMPTY => 'Password is required',
),
),
),
array(
'name' => 'Regex',
'options' => array(
'pattern' => $pattern,
'messages' => array(
'regexInvalid' => 'Regular expression is invalid',
'regexNotMatch' => 'Password must contain uppercase, lowercase & digit and at least 6 characters',
),
),
),
),
)));
  • /^ - start of string
  • (?=.*\d) - search for digit to ensure at least contain 1 digit
  • (?=.*[A-Z]) - search for uppercase character to ensure at least contain 1 uppercase
  • (?=.*[a-z]) - search for lowercase character to ensure at least contain 1 lowercase
  • [0-9A-Za-z!@#$%] - password can consists of any digit, alphabet and symbols ‘!@#$%’
  • {6,} - must be at least 6 characters (if want to limit the max number, then {6,12})
  • $/ - end of string
Reference:

Get query string from URL

In your controller

1
2
<?php
$keyword = $this->getRequest()->getQuery('keyword');
Reference:

Get query string in view

In layout

1
2
3
4
5
6
<?php
$children = $this->viewModel()->getCurrent()->getChildren();
$variables = $children[0]->getVariables();

echo $variables['param1'];
echo $variables['param2'];

In view

1
2
3
4
5
<?php
$variables = $this->viewModel()->getCurrent()->getVariables();

echo $variables['param1'];
echo $variables['param2'];

Add custom error message

1
2
3
4
5
6
7
8
9
10
<?php
$form->get('element')->setMessages(array(
'foo has this error',
'bar has another error',
...
));

if ($form->isValid() && !$form->get('element')->getMessages()) {
// no more error
}
Reference:

Get config in view

First create a view helper (Refer to here)

./module/Application/src/Application/View/Helper/Config.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?php
namespace Application\View\Helper;

use Zend\View\Helper\AbstractHelper;

class Config extends AbstractHelper
{
private $_sm;

public function __construct($sm)
{
$this->_sm = $sm;
}

public function __invoke()
{
return $this->_sm->getServiceLocator()->get('Config');
}
}

In ./module/Application/Module.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
...
public function getViewHelperConfig()
{
return array(
'factories' => array(
'getConfig' => function($sm) {
$helper = new \Application\View\Helper\Config($sm);
return $helper;
},
),
);
}
...

Now you can use it in your view

1
2
3
<div id="foo">
<pre><?php print_r($this->getConfig()); ?></pre>
</div>

Construct URL in controller

1
2
3
4
<?php
public function indexAction() {
$route = $this->url()->fromRoute('product', array('action' => 'edit'));
}

Fixed sql quote value NOTICE

The notice is look like

1
Notice: Attempting to quote a value without specific driver level support can introduce security vulnerabilities in a production environment. in /Users/username/public_html/projectname/vendor/zendframework/zendframework/library/Zend/Db/Adapter/Platform/Sql92.php on line 80

Fix it by edit the file vendor/zendframework/zendframework/library/Zend/Db/Metadata/Source/MysqlMetadata.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
...
protected function loadColumnData($table, $schema)
{
...
$this->prepareDataHierarchy('columns', $schema, $table);
$p = $this->adapter->getPlatform();
// if the platform's resource is not synchronized, try to fetch it
// from connection object
$p->setDriver($this->adapter->getDriver());

$isColumns = array(
array('C','ORDINAL_POSITION'),
...
}
...

alt text

Reference: