[need help] php mvc form validation

Discussion in 'PHP' started by huseinbandi, May 18, 2014.

  1. #1
    Hey guys,for a couple days, i have try to make form validation using php mvc base structure, and i have try to following this tutorial on youtube :

    [youtube]rWon2iC-cQ0[/youtube]

    but, on that tutorial.. in action line.. is just redirect on that page it self.
    bcz the validate checking function is write in sama page with the form.

    bcz i have my own mvc, i try to put in different location and i separate it into 3 files :
    view : index.php
    controller : register.php
    model : register_model.php
    libs : validate.php

    view/index.php
    <form method="post" action="<?php echo URL; ?>register/insert">
    <table>
    <tr>
    <td>username</td>
    <td>&lt;input name="username" type="text" autocomplete="off" value="&lt;?php echo Register_Model::get('username'); ?&gt;" /&gt;&lt;/td>
    <td></td>
    </tr>
    <tr>
    <td>first name</td>
    <td>&lt;input name="first_name" type="text" autocomplete="off" value="&lt;?php echo Register_Model::get('first_name'); ?&gt;" /&gt;&lt;/td>
    <td></td>
    </tr>
    <tr>
    <td>last name</td>
    <td>&lt;input name="last_name" type="text" autocomplete="off" value="&lt;?php echo Register_Model::get('last_name'); ?&gt;" /&gt;&lt;/td>
    <td></td>
    </tr>
    <tr>
    <td>password</td>
    <td>&lt;input name="password" type="text" /&gt;&lt;/td>
    <td></td>
    </tr>
    <tr>
    <td>retype password</td>
    <td>&lt;input name="retype_password" type="text" /&gt;&lt;/td>
    <td></td>
    </tr>
    <tr>
    <td>address</td>
    <td>&lt;textarea name="address" placeholder="your address here"&gt;&lt;?php echo Register_Model::get('address'); ?&gt;&lt;/textarea&gt;&lt;/td>
    <td></td>
    </tr>
    <tr>
    <td>phone</td>
    <td>&lt;input name="phone" type="text" autocomplete="off" value="&lt;?php echo Register_Model::get('phone'); ?&gt;" /&gt;&lt;/td>
    <td></td>
    </tr>
    <tr>
    <td>No KTP</td>
    <td>&lt;input name="nik_numb" type="text" autocomplete="off" value="&lt;?php echo Register_Model::get('nik_numb'); ?&gt;" /&gt;&lt;/td>
    <td></td>
    </tr>
    <tr>
    <td>gender</td>
    <td>
    <select name="gender">
    <option value="male">male</option>
    <option value="female">female</option>
    </select>
    </td>
    <td></td>
    </tr>
    <tr>
    <td>role</td>
    <td>
    <select name="role">
    <option value="member">member</option>
    <option value="operator">operator</option>
    <option value="administrator">administrator</option>
    </select>
    </td>
    <td></td>
    </tr>
    <tr>
    <td>&lt;input type="submit" /&gt;&lt;/td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    &lt;/form&gt;

    controllers/register.php
    public function insert(){

    if(Register_Model::val()){
    $validate = new Validate();
    $validation = $validate->check($_POST, array(
    'username' => array(
    'required' => true,
    'min' => 5,
    'max' => 12,
    'unique' => 'users'
    ),
    'first_name' => array(
    'required' => true,
    'min' => 5,
    'max' => 12
    ),
    'last_name' => array(
    'required' => true,
    'min' => 5,
    'max' => 12
    ),
    'password' => array(
    'required' => true,
    'min' => 6
    ),
    'retype_password' => array(
    'required' => true,
    'matches' => 'password'
    ),
    'address' => array(
    'required' => true
    ),
    'phone' => array(
    'required' => true
    ),
    'nik_numb' => array(
    'required' => true
    ),
    'gender' => array(
    'required' => true
    ),
    'role' => array(
    'required' => true
    )
    ));

    if($validation->passed()){
    //register user
    $c = $this->model->numbering();

    foreach ($c as $key => $value) {
    $data = $value['id'];
    }

    $abc['member_id'] = 0 . date('ymdN') . '00' . $data+1;
    $abc['username'] = $_POST['username'];
    $abc['name'] = $_POST['first_name'] ."_". $_POST['last_name'];
    $abc['last_name'] = $_POST['last_name'];
    $abc['password'] = $_POST['password'];
    $abc['address'] = $_POST['address'];
    $abc['phone'] = $_POST['username'];
    $abc['nik_numb'] = $_POST['nik_numb'];
    $abc['gender'] = $_POST['gender'];
    $abc['role'] = $_POST['role'];

    $this->model->create($abc);


    }else{
    //output errors
    echo 'inserting data failed';
    print_r($validation->errors());
    }

    header('location:'.URL.'register');
    }
    }

    and for the model page, is :
    public function numbering(){
    $sth = $this->db->prepare('SELECT * FROM member_data');
    $sth->execute();
    return $sth->fetchAll();
    }

    public static function val($type = 'post'){
    switch($type){
    case 'post' :
    return (!empty($_POST)) ? true : false;
    break;
    case 'get' :
    return (!empty($_GET)) ? true : false;
    break;
    default :
    return false;
    break;
    }
    }

    public static function get($item){
    if(isset($_POST[$item])){
    return $_POST[$item];
    }else if(isset($_GET[$item])){
    return $_GET[$item];
    }
    return '';
    }

    public function create($abc){
    $insert = $this->db->prepare('INSERT INTO member_data (`member_id`,`username`,`name`,
    `password`,`address`,`phone`,`nik_numb`,`gender`,`role`)
    VALUES:)member_id, :username, :name, :password, :address, :phone, :nik_numb, :gender, :role)
    ');

    $insert->execute(array(
    ':member_id' => $abc['member_id'],
    ':username' => $abc['username'],
    ':name' => $abc['name'],
    ':password' => $abc['password'],
    ':address' => $abc['address'],
    ':phone' => $abc['phone'],
    ':nik_numb' => $abc['nik_numb'],
    ':gender' => $abc['gender'],
    ':role' => $abc['role']
    ));
    }

    and last for libs, the code like this below:
    &lt;?php

    class Validate{

    private $_passed = false,
    $_errors = array(),
    $db = null;


    public function __construct(){
    $this->db = new Database();
    }

    public function check($source, $items = array()){
    foreach($items as $item => $rules){
    foreach($rules as $rule => $rule_value){

    $value = trim($source[$item]);
    $item = escapeshellcmd&#40;$item&#41;;

    if($rule === 'required' && empty($value)){
    $this->addError("{$item} is required");
    }else{
    switch($rule){
    case 'min':
    if(strlen($value) < $rule_value){
    $this->addError("{$item} must be a minimum of {$rule_value}");
    }
    break;
    case 'max':
    if(strlen($value) > $rule_value){
    $this->addError("{$item} must be a maximum of {$rule_value}");
    }
    break;
    case 'matches':
    if($value != $source[$rule_value]){
    $this->addError("{$rule_value} must matches");
    }
    break;
    case 'unique':
    $check = $this->db->prepare($rule_value, array($item, '=' ,$value));
    //if($check->count()){
    // $this->addError("{$item} already exists");
    //}
    break;
    }
    }
    }
    }

    if(empty($this->_errors)){
    $this->_passed = true;
    }

    return $this;
    }

    public function addError($error){
    $this->_errors[] = $error;
    }

    public function errors(){
    $this->_errors = $this->view->err;
    return $this->_errors;
    }

    public function passed(){
    return $this->_passed;
    }
    }

    and the question is, how i called and show validate error message on view index file? ( on red color )
     
    huseinbandi, May 18, 2014 IP