Bước 1: tạo mảng inputs (ok)

Chúng ta phải xây dụng được mảng email, mảng password như sau

array (
  'email' => 
  array (
    'type' => 'text',
  ),
  'password' => 
  array (
    'type' => 'password',
  )
)

Xây dựng class form

<?php  
	class Form {
		private $model = 'Model';
		protected $rules = null;
		public $inputs = array();
		public function setRules($rules) {
			if (!empty($rules)) {
				$this->rules = $rules;
				foreach ($rules as $field => $rule) {
					if (isset($rule['form'])) {
						$this->inputs[$field] = $rule['form'];
					}
				}
			}
		}
	};
	$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'
				)
			)
		);
	$form = new Form();
	$form->setRules($rules);
	echo '<pre>';
		var_export($form->inputs);
	echo '</pre>';
	// array (
	//   'email' => 
	//   array (
	//     'type' => 'text',
	//   ),
	//   'password' => 
	//   array (
	//     'type' => 'password',
	//   ),
	//   'fullname' => 
	//   array (
	//     'type' => 'text',
	//   ),
	//   'address' => 
	//   array (
	//     'type' => 'textarea',
	//   ),
	// )
?>

Last updated