Skip to content

Instantly share code, notes, and snippets.

@mockiemockiz
Created May 10, 2014 15:20
Show Gist options
  • Save mockiemockiz/c3116f405b7084e53b51 to your computer and use it in GitHub Desktop.
Save mockiemockiz/c3116f405b7084e53b51 to your computer and use it in GitHub Desktop.
test
<?php
/**
* Created by PhpStorm.
* User: mockie
* Date: 5/10/14
* Time: 8:21 PM
*/
namespace User\Controller;
use User\Form\UserForm;
use User\Model\UserAddInputFilter;
use Zend\Debug\Debug;
use Zend\Mvc\Controller\AbstractActionController;
class UserController extends AbstractActionController{
public function signupAction(){
$form = new UserForm();
$inputFilter = new UserAddInputFilter( $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter') );
$request = $this->getRequest();
if($request->isPost()){
$dataPost = $request->getPost();
$form->setInputFilter($inputFilter->getInputFilter());
$form->setData($dataPost);
if( $form->isValid() ){
Debug::dump($form->getData());
}
}
return array(
'form' => $form
);
}
}
<?php
/**
* Created by PhpStorm.
* User: mockie
* Date: 5/10/14
* Time: 9:00 PM
*/
namespace User\Model;
use Zend\Db\Adapter\Adapter;
use Zend\Debug\Debug;
use Zend\InputFilter\InputFilterAwareInterface;
class UserAddInputFilter extends UserBaseInputFilter implements InputFilterAwareInterface{
protected $adapter;
private $password;
function __construct(Adapter $adapter){
parent::__construct();
parent::getInputFilter();
$this->setAdapter($adapter);
}
public function exchangeArray($data)
{
$this->password = (!empty($data['password'])) ? $data['password'] : null;
}
// Add the following method:
public function getArrayCopy()
{
return get_object_vars($this);
}
public function getInputFilter(){
$this->inputFilter->add(array(
'name' => 'password',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'options' => array(
'min' => 8,
),
));
return $this->inputFilter;
}
}
<?php
/**
* Created by PhpStorm.
* User: mockie
* Date: 5/10/14
* Time: 9:00 PM
*/
namespace User\Model;
use Zend\InputFilter\InputFilterAwareInterface;
use Zend\InputFilter\InputFilter;
use Zend\InputFilter\InputFilterInterface;
class UserBaseInputFilter implements InputFilterAwareInterface{
protected $inputFilter;
protected $adapter;
private $id;
private $username;
private $email;
function __construct(){
$this->inputFilter = new InputFilter();
}
/**
* @param mixed $adapter
*/
public function setAdapter($adapter)
{
$this->adapter = $adapter;
}
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->username = (!empty($data['username'])) ? $data['username'] : null;
$this->email = (!empty($data['email'])) ? $data['email'] : null;
}
// Add the following method:
public function getArrayCopy()
{
return get_object_vars($this);
}
public function getInputFilter(){
$this->inputFilter->add(array(
'name' => 'username',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'StripTags'),
),
'validators' => array(
array('name' => '\Zend\Validator\Db\NoRecordExists', 'options' => array(
'table' => 'user_user',
'field' => 'username',
'adapter' => $this->adapter,
)),
),
'options' => array(
'min' => 8,
),
));
$this->inputFilter->add(array(
'name' => 'email',
'required' => true,
'filters' => array(
array('name' => 'StringTrim'),
array('name' => 'HtmlEntities'),
),
'validators' => array(
array('name' => '\Zend\Validator\Db\NoRecordExists', 'options' => array(
'table' => 'user_user',
'field' => 'email',
'adapter' => $this->adapter,
)),
array('name' => 'EmailAddress')
),
'options' => array(
'max' => 40,
),
));
return $this->inputFilter;
}
public function setInputFilter(InputFilterInterface $inputFilter)
{
throw new \Exception("Not used");
}
}
<?php
/**
* Created by PhpStorm.
* User: mockie
* Date: 5/10/14
* Time: 8:36 PM
*/
namespace User\Form;
use Zend\Form\Form;
use Zend\Validator\Csrf;
class UserForm extends Form{
function __construct(){
parent::__construct('UserForm');
$this->add(array(
'name' => 'username',
'type' => 'Text',
'attributes' => array(
'class' => 'username form-control',
),
'options' => array(
'label' => _('Username'),
)
));
$this->add(array(
'name' => 'email',
'type' => 'Text',
'attributes' => array(
'class' => 'email form-control',
),
'options' => array(
'label' => _('Email'),
)
));
$this->add(array(
'name' => 'password',
'type' => 'Text',
'attributes' => array(
'class' => 'password form-control',
),
'options' => array(
'label' => _('Password'),
)
));
$csrfValidator = new Csrf(array('name'=> 'csrf',
'salt' => 'asdsadasdsa'
));
$csrf = new \Zend\Form\Element\Csrf('csrf');
$csrf->setCsrfValidator($csrfValidator);
$this->add($csrf);
//assign!
$this->csrf = $csrf;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment