Zend Framework2: Tutorial Part4: Building a CRUD app by using Table Gateway2

 Here you have your form fold, the file stuction should look like this:

crud4

here is the code for userForm.php

<?php
namespace CsnUser\Form;

use Zend\Form\Form;

class UserForm extends Form
{
public function __construct($name = null)
{
parent::__construct(‘user’);
$this->setAttribute(‘method’, ‘post’);

$this->add(array(
‘name’ =>’usr_name’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Username’,
),
));

$this->add(array(
‘name’ =>’usr_password’,
‘attributes’=> array(
‘type’=>’password’,
),
‘options’=>array(
‘label’=>’Password’,
),
));

$this->add(array(
‘name’ =>’usr_email’,
‘attributes’=> array(
‘type’=>’email’,
),
‘options’=>array(
‘label’=>’E-mail’,
),
));

$this->add(array(
‘name’ =>’usrl_id’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Role’,
‘value_options’=>array(
‘1’=>’Public’,
‘2’=>’Member’,
‘3’=>’Admin’,
),
),
));

$this->add(array(
‘name’ =>’lng_id’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Language’,
‘value_options’=>array(
‘1’=>’English’,
‘2’=>’French’,
‘3’=>’German’,
),
),
));

$this->add(array(
‘name’ =>’usr_active’,
‘type’=>’Zend\Form\Element\Select’,
‘options’=>array(
‘label’=>’Active’,
‘value_options’=>array(
‘0’=>’No’,
‘1’=>’Yes’,

),
),
));

$this->add(array(
‘name’ =>’usr_question’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Question’,
),
));

$this->add(array(
‘name’ =>’usr_answer’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Answer’,
),
));

$this->add(array(
‘name’ =>’usr_picture’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Picture URL’,
),
));

$this->add(array(
‘name’ =>’usr_picture’,
‘attributes’=> array(
‘type’=>’text’,
),
‘options’=>array(
‘label’=>’Picture URL’,
),
));

$this->add(array(
‘name’ => ‘usr_password_salt’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Password Salt’,
),
));

$this->add(array(
‘name’ => ‘usr_registration_date’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Registration Date’,
),
));

$this->add(array(
‘name’ => ‘usr_registration_token’,
‘attributes’ => array(
‘type’  => ‘text’,
),
‘options’ => array(
‘label’ => ‘Registration Token’,
),
));

$this->add(array(
‘name’ => ‘usr_email_confirmed’,
‘type’ => ‘Zend\Form\Element\Select’,
‘options’ => array(
‘label’ => ‘E-mail was confirmed?’,
‘value_options’ => array(
‘0’ => ‘No’,
‘1’ => ‘Yes’,
),
),
));

$this->add(array(
‘name’ => ‘submit’,
‘attributes’ => array(
‘type’  => ‘submit’,
‘value’ => ‘Go’,
‘id’ => ‘submitbutton’,
),
));
}
}

here is the code for userFilter.php(you could use javascript like backbone to validate the form instead will more faster)

<?php
namespace CsnUser\Form;

use Zend\InputFilter\Factory as InputFactory;
use Zend\InputFilter\InputFilter;

class UserFilter extends InputFilter
{
public function __construct() {
$this->add(array(
‘name’=>’usr_name’,
‘required’=>true,
‘filters’=>array(
array(‘name’=>’StripTags’),
array(‘name’=>’StringTrim’),
),
‘validators’=>array(
array(
‘name’    => ‘StringLength’,
‘options’ => array(
‘encoding’ => ‘UTF-8’,
‘min’      => 1,
‘max’      => 100,
),
),
),
));

$this->add(array(
‘name’       => ‘usr_email’,
‘required’   => true,
‘validators’ => array(
array(
‘name’ => ‘EmailAddress’
),
),
));

$this->add(array(
‘name’     => ‘usr_password’,
‘required’ => true,
‘filters’  => array(
array(‘name’ => ‘StripTags’),
array(‘name’ => ‘StringTrim’),
),
‘validators’ => array(
array(
‘name’    => ‘StringLength’,
‘options’ => array(
‘encoding’ => ‘UTF-8’,
‘min’      => 6,
‘max’      => 12,
),
),
),
));

$this->add(array(
‘name’     => ‘usr_active’,
‘required’ => true,
‘filters’  => array(
array(‘name’ => ‘Int’),
),
‘validators’ => array(
array(
‘name’    => ‘Digits’,
),
),
));

}

}

the index.phtml code:for retrieve data from your database, here is your presentation page, on you browser type:http://zf2.local/csn-user/user/index

should look like this:

crud3

 

<h1>Index = Retrieve</h1>
<a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘create’)); ?>”>Create</a>
<table>
<tr>
<th>usr_id</th>
<th>usr_name</th>
<th>usr_email</th>
</tr>
<?php
foreach ($rowset as $row) {
?>
<tr>
<td><?php  echo $row->usr_id; // The row is an ArrayObject $row->usr_id and $row[‘usr_id’] with both work?></td>
<td><?php  echo $row[‘usr_name’]; ?></td>
<td><?php  echo $row[‘usr_email’]; ?></td>
<td><a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘update’, ‘id’ => $row[‘usr_id’])); ?>”>Edit</a></td>
<td><a href=”<?php echo $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘delete’, ‘id’ => $row[‘usr_id’]));?>” onclick=”return confirm(‘Do you rely want to delete the record?’)”>Delete</a></td>
</tr>
<?php
}
?>
</table>

your update.phtml code:

<h1>Update</h1>
<?php
$form = $this->form;
$form->prepare();
$form->setAttribute(‘action’,$this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘update’, ‘id’ => $id)));
$form->setAttribute(‘method’,’post’);

echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

your create.phtml code:

<h1>Create an new user</h1>
<?php
$form = $this->form;
$form->prepare();
// Assuming the “contact/process” route exists…
$form->setAttribute(‘action’, $this->url(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘create’))); //’contact/process’));
// Set the method attribute for the form
$form->setAttribute(‘method’, ‘post’);

echo $this->form()->openTag($form);
echo $this->formCollection($form);
echo $this->form()->closeTag();

And you don’t need a delete view

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s