formbuilder OOP验证并返回错误的值

时间:2015-03-14 11:38:06

标签: php oop

我做了一个formbuilder,现在我正在考虑验证并显示一个错误值的表单。

这样做的正确方法是什么?我正在考虑创建一个新的验证类,并在字段类中设置一个setter来决定你想要的验证,然后创建对象。

我怎样才能返回错误的输入,以便用户有机会修改它们?我能想到的就是为这个值制作一个setter,所以我可以很容易地修改它。导致我的班级现在如何设计,不可能

编辑:我知道它不是一个完成的课程,我没有对课程进行评论。那是因为它是一个WIP,我需要一些提示才能继续。

<?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 .= '>'."\n";
                return $this->_formOpen;
    }

    protected function closeForm() {
        $this->_formClose = '</form>'."\n";
                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 .= '>'."\n";
    }

    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 个答案:

没有答案