I'm having problems checking the functioning of the validations of a form that updates the information of a certain record, apparently the validations work, but they return an error that I just had to print so you can better understand what is happening:
Apparently the validations work, but the page address is not the form's, that's why the page is completely empty, just containing the validation messages.
form_update.php
<?php if (!empty($errors)): ?>
<div >
<?php foreach ($errors as $error): ?>
<li><?= $error ?></li>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="<?= site_url('update/'.$hxh['id_hunter'])?>" method="POST">
<div >
<div >
<div for="name_hunter">Nome:
<input type="text" name="name_hunter" maxlength="30" value="<?= $hxh['aome_hunter'] ?>">
</div>
</div>
<br>
<div >
<div for="age_hunter">Idade:
<input type="text" name="age_hunter" onkeypress="$(this).mask('00', {reverse: true});" value="<?= $hxh['age_hunter'] ?>">
</div>
</div>
<br>
...
<button type="submit" ><i ></i> Update</button>
</form>
HunterController.php
public function formUpdateHunter($id_hunter)
{
try {
$hunter = new HunterModel();
$dados['hxh'] = $hunter->where('id_hunter', $id_hunter)->first();
return view('form_update', $dados);
} catch (\Exception $e) {
exit($e->getMessage());
}
}
public function updateHunter()
{
try {
helper(['form','url','html']);
$hunter = new HunterModel();
$id_hunter = $this->request->getPost('id_hunter');
$dados = [
'name_hunter' => $this->request->getPost('name_hunter'),
'age_hunter' => $this->request->getPost('age_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')
];
if ($hunter->update($id_hunter, $dados)){
return $this->response->redirect(site_url('listing'));
} else {
return view('form_update', ['errors' => $hunter->errors()]);
}
} catch (\Exception $e) {
exit($e->getMessage());
}
}
CodePudding user response:
I suspect it stops rendering the html, because in updateHunter
the template is missing the $hxh['id_hunter']
variable for the form's action
attribute.
Try passing the hunter data to the template again in updateHunter
(I only changed the else
):
public function updateHunter()
{
try {
helper(['form','url','html']);
$hunter = new HunterModel();
$id_hunter = $this->request->getPost('id_hunter');
$dados = [
'name_hunter' => $this->request->getPost('name_hunter'),
'age_hunter' => $this->request->getPost('age_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')
];
if ($hunter->update($id_hunter, $dados)){
return $this->response->redirect(site_url('listing'));
} else {
$dados['id_hunter'] = $id_hunter;
return view('form_update', ['errors' => $hunter->errors(), 'hxh' => $dados]);
}
} catch (\Exception $e) {
exit($e->getMessage());
}
}