douyanxing6054 2015-03-14 11:38 采纳率: 0%
浏览 30

formbuilder OOP验证并返回错误的值

I made a formbuilder, and now im thinking of validation and showing a form with the wrong values.

What is the right way to do this? I was thinking of making a new validation class, and a setter in the field class to decide wich validation you want, and then create the object.

And how can i return the wrong inputs, so the user has the chance to modify them? All i could think of, is making a setter for the value, so i can easily modify it. cause how my class is designed now, its not possible

EDIT: i know its not a finished class, and i have not commented the class. That's because its a WIP, and i need some tips before i continue.

<?php

/**
* form.class.php
**/

include 'form/field.class.php';
include 'form/input.class.php';

class Form {

    private $_action;
    private $_method;

    protected $_formHtml;
    protected $_atributes;
        protected $_fields;


    public function __construct($action,$method) {
        $this->_action = $action;
        $this->_method = $method;
    }

    protected function openForm() {
        $this->_formOpen = '<form action="'.$this->_action.'" method="'.$this->_method.'" ';
        if(isset($this->_atributes)) {
            foreach($this->_atributes as $key => $value) {
                $this->_formOpen .= ' '.$key.'="'.$value.'"';
            }
        }
        $this->_formOpen .= '>'."
";
                return $this->_formOpen;
    }

    protected function closeForm() {
        $this->_formClose = '</form>'."
";
                return $this->_formClose;
    }

    public function setAtribute($array) {
        foreach($array as $key => $value) {
            $this->_atributes[$key] = $value; 
        }
    }

    public function generateForm($array) {
                echo $this->openForm();
                foreach($array as $key) {
                    $key->generateField();
                    $key->display();
                }
        echo $this->closeForm();
        echo $this->_formHtml;
    }

        public function showValues($submit) {
            if(isset($_POST[$submit])) {
                foreach($_POST as $key => $value) {
                    if($key !== $submit)
                    echo ''.$key.' : '.$value.'<br>';
                }
            }
        }

}
?>

<?php

/**
* field.class.php abstract class
**/

abstract class Field {

    protected $_error;  

    abstract protected function generateField();
    abstract public function display(); 
        abstract public function returnHtml();

    public function setError($error) {
    $this->_error = $error;
    }

    public function checkErrors() {

    }

    public function returnErrors() {
    return $this->_error;
    }

}
?>

<?php

/**
* input.class.php
**/

class Input extends Field {

    protected $_elements;
    var $_html;

    public function __construct($array) {
        foreach($array as $key => $value) {
            $this->_elements[$key] = $value;
        }       
    }


    public function generateField(){
        $this->_html = '<input';
        foreach($this->_elements as $key => $value) {
            $this->_html .= ' '.$key.'="'.$value.'"';
        }
        $this->_html .= '>'."
";
    }

    public function returnHtml() {
        return $this->_html;
    }

    public function display() {
        echo $this->_html;      
    }



}
?>


<?php

/**
* index.php Voorbeeld
**/

require 'classes/form.class.php';

$username = new Input(array(
    'type' => 'text',
    'name' => 'username',
    'placeholder' => 'Username'
));
$email = new Input (array(
        'type' => 'email',
        'name' => 'email',
        'placeholder' => 'Email',    
));
$password = new Input(array(
        'type' => 'password',
        'name' => 'password',
        'placeholder' => 'Password'
));
$submit = new Input(array(
    'type' => 'submit',
    'name' => 'submit_login',
    'value' => 'Login'
));

$form = new Form('','post');
$form->setAtribute(array('id' => 'test','class' => 'bla'));
$form->generateForm(array($username,$email,$password,$submit));
$form->showValues('submit_login');
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算