My updateHunter()
in HunterController.php
file should make an update of the registration method information and redirect to the listing page if everything is ok, if not, it should return to the update form which will be shown message(s) to correct the problem of a certain field(s).
When returning to the form page form_update.php
to make the user correct the failed fields I come across the error: Update Undefined variable $user_hunter.
Image here: screen fully white
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 >
<div >
<div >
<div >
<div >
<h4>Update <?= $user_hunter['name_hunter'] ?>
<a href="<?= site_url('/home');?>" ><i ></i> Return list</a>
</h4>
</div>
</div>
<br>
<div >
<?php $validation = \Config\Services::validation(); ?>
<form action="<?= site_url('update/'.$user_hunter['id_hunter'])?>" method="POST">
<div >
<div >
<div for="name_hunter">ID:
<input type="number" name="id_hunter" value="<?= $user_hunter['id_hunter'] ?>" readonly>
</div>
</div>
<br>
<div >
<div for="name_hunter">Name:
<input type="text" 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" ><i ></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());
}
}
CodePudding user response:
they is no user_hunter var passed to form_update view, nor in updateHunter()
CodePudding user response:
Look at the following tutorial:
https://www.guru99.com/codeigniter-form-validation.html#6
Specifically:
Populating Submitted Form Data: Sticky Forms