Bước 4: Tạo form input (ok) tạo thêm class AppModel && User extends AppModel (ok)

<?php  
	class Form {
		private $model = 'Model';
		protected $rules = null;
		public $inputs = array();
		public function input($name) {
    	$type = 'text';
	    foreach ($this->inputs as $field => $input) {
	      if ($field == $name) {
	        $type  = $input['type'];
	        $style = isset($input['style']) ? ' style="' . $input['style'] . '"' : '';
	        switch ($type) {
	          case 'text':
	            $inputField = '<input type="text"' . $style . ' name="data[' . $this->model . '][' . $name . ']"' . ' value="' . (isset($this->data[$this->model][$name]) ? $this->data[$this->model][$name] : '') . '" />';
	            break;
	          case 'password':
	            $inputField = '<input type="password"' . $style . ' name="data[' . $this->model . '][' . $name . ']"' . ' value="' . (isset($this->data[$this->model][$name]) ? $this->data[$this->model][$name] : '') . '" />';
	            break;
	        }
	      }
	    }
	    echo $inputField;
	  }
	  public function setRules($rules) {
	    if (!empty($rules)) {
	      $this->rules = $rules;
	      foreach ($rules as $field => $rule) {
	        if (isset($rule['form'])) {
	          $this->inputs[$field] = $rule['form'];
	        }
	      }
	    }
	  }
	}
?>
<?php  
	if(!class_exists('Form')) require_once 'Form.php';
	class AppModel {
		public $form = null;
		protected $rules = null;
		public function __construct() {
			$this->form = new Form();
			$this->form->setRules($this->rules);
		}
	};
?>
<?php  
require_once "AppModel.php";
class  User extends AppModel {
	protected $rules = array(
		"email" => array(
			"form" => array(
				"type" => "text"
			), 
			"notEmpty" => array(
				"rule" => "notEmpty", 
				"message" => 'MSG_ERR_NOTEMPTY'
			), 
			"isEmail" => array(
				"rule" => "email", 
				"message" => 'MSG_ERR_EMAIL'
			)
		), 
		"password" => array(
			"form" => array(
				"type" => "password"
			), 
			"notEmpty" => array(
				"rule" => "notEmpty", 
				"message" => 'MSG_ERR_NOTEMPTY'
			)
		), 
		"fullname" => array(
			"form" => array(
				"type" => "text"
			), 
			"notEmpty" => array(
				"rule" => "notEmpty", 
				"message" => 'MSG_ERR_NOTEMPTY'
			)
		), 
		"address" => array(
			"form" => array(
				"type" => "textarea"
			), 
			"notEmpty" => array(
				"rule" => "notEmpty", 
				"message" => 'MSG_ERR_NOTEMPTY'
			)
		)
	);
	public function __construct() {
		parent::__construct();
	}
}
$user = new User();
$user->form->input('email');

Last updated