Bước 2: Tạo form input (ok)
<?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'];
}
}
}
}
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="" />';
break;
}
}
}
echo $inputField;
}
};
$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);
$form->input('email');
?>
Last updated