HunterController.php
文件中的我的updateHunter()
应该更新注册方法信息,如果一切正常,则重定向到列表页面,如果不正常,则返回到更新表单,更新表单将显示消息以更正某个字段的问题。
当返回表单页面form_update.php
以使用户更正失败的字段时,我遇到错误:更新未定义变量$user_hunter。
此处的图像:屏幕全白色
form_page.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="<?= base_url('css/bootstrap.css'); ?>">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="https://kit.fontawesome.com/41b4cd8ba8.js" crossorigin="anonymous"></script>
<script type="text/javascript" href="<?= base_url('js/bootstrap.js')?>"></script>
<title>Update <?= $user_hunter['name_hunter'] ?></title>
</head>
<body>
<div class="contained">
<div class="row">
<div class="col-md-12 mt-2">
<div class="card">
<div class="card-header">
<h4>Update <?= $user_hunter['name_hunter'] ?>
<a href="<?= site_url('/home');?>" class="btn btn-secondary float-end"><i class="fa fa-arrow-left"></i> Return list</a>
</h4>
</div>
</div>
<br>
<div class="card-body">
<?php $validation = ConfigServices::validation(); ?>
<form action="<?= site_url('update/'.$user_hunter['id_hunter'])?>" method="POST">
<div class="form_group">
<div class="form_group">
<div for="name_hunter">ID:
<input type="number" class="form-control" name="id_hunter" value="<?= $user_hunter['id_hunter'] ?>" readonly>
</div>
</div>
<br>
<div class="form_group">
<div for="name_hunter">Name:
<input type="text" class="form-control" name="name_hunter" maxlength="30" value="<?= $user_hunter['name_hunter'] ?>">
<?php if($validation->getError('name_hunter')) {?>
<div class='text-danger mt-1'>
<?= $error = $validation->getError('name_hunter'); ?>
</div>
<?php }?>
</div>
</div>
<br>
... (More fiels below)
<button type="submit" class="btn btn-primary"><i class="fa fa-edit"></i> Update</button>
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
HunterController.php
public function updateHunter()
{
try {
helper(['form', 'url', 'html']);
$validations = $this->validate([
'name_hunter' => 'required|max_length[30]',
'year_hunter' => 'required|integer',
'height_hunter' => 'required|decimal',
'weight_hunter' => 'required|decimal',
'type_hunter' => 'required|max_length[30]',
'type_nen' => 'required|max_length[30]',
'type_blood' => 'required|max_length[3]'
],
[
'name_hunter' => [
'required' => 'Hunter name can't be empty.',
'max_length' => 'Hunter name must be a maximum of 30 characters.'
],
'year_hunter' => [
'required' => 'Hunter year can't be empty.',
'integer' => 'Hunter age must be a whole number.'
],
'height_hunter' => [
'required' => 'Hunter weight can't be empty.',
'decimal' => 'Hunter weight must be a decimal number.'
],
'weight_hunter' => [
'required' => 'Hunter weight can't be empty.',
'decimal' => 'Hunter weight must be a decimal number.'
],
'type_hunter' => [
'required' => 'It is necessary to define the type of Hunter.',
'max_length' => 'Hunter type must be a maximum of 30 characters.'
],
'type_nen' => [
'required' => 'It is necessary to define the type Nen.',
'max_length' => 'Nen type must be a maximum of 30 characters.'
],
'typo_blood' => [
'required' => 'It is necessary to define the type Blood.',
'max_length' => 'Blood type must be a maximum of 30 characters..'
]
]);
$hunter = new HunterModel();
$id_hunter = $this->request->getPost('id_hunter');
if ($validations) {
$data = [
'name_hunter' => $this->request->getPost('name_hunter'),
'year_hunter' => $this->request->getPost('year_hunter'),
'height_hunter' => $this->request->getPost('height_hunter'),
'weight_hunter' => $this->request->getPost('weight_hunter'),
'type_hunter' => $this->request->getPost('type_hunter'),
'type_nen' => $this->request->getPost('type_nen'),
'type_blood' => $this->request->getPost('type_blood')
];
$hunter->update($id_hunter, $data);
return $this->response->redirect(site_url('/home'));
} else {
echo view('form_update', ['validation' => $this->validator]);
}
} catch (Exception $e) {
exit($e->getMessage());
}
}
查看以下教程:
https://www.guru99.com/codeigniter-form-validation.html#6
具体而言:
填充提交的表单数据:粘贴表单
它们不是传递给form_update视图的user_hunter var,也不是updateHunter((中的