Im very new to CodeIgniter, using 4.5.5. I got a HTML form, a sophisticated one, to manage data in a database. Suddenly the validation form error view does not show errors with a point marker in different rows, but all errors without markers in a single row. I dont know what i have changed causing this. I didn engage a git, so i cannot roll back. When i look into the HTML code, CodeIgniter produces, the only difference to the tutorial first app i see is the ::marker is missing in the li tags.
See the form error view
<?php
namespace App\Controllers;
use App\Models\FlightsModel;
use CodeIgniter\Exceptions\PageNotFoundException;
class Airports extends BaseController
{
public function new(?array $form_data = null)
{
helper('form');
if( is_null($form_data)) {
$form_data['rwystored'] = false;
$form_data['ICAOcode'] = "";
$form_data['runways' ] = null; // indexed array
$form_data['radios' ] = []; // indexed array for radio number with associative array for sign, freq and type
}
$this->session->set($form_data);
return view('templates/header', ['title' => 'Create an airport'])
. view('airports/create', $form_data)
. view('templates/footer');
}
public function create()
{
helper('form');
$form_data = $this->session->get();
$data = $this->request->getPost(['ICAOcode', 'newrunway']);
// Checks whether the submitted data passed the validation rules.
if (! $this->validateData($data, [
'ICAOcode' => 'required|max_length[4]|min_length[4]',
'newrunway' => 'max_length[3]',
])) {
// The validation fails, so returns the form.
$form_data['rwystored'] = false;
return $this->new($form_data);
}
$post = $this->validator->getValidated();
// Store new runway and redisplay the form
$data = $this->request->getPost(['action']);
if($data['action'] === 'runway') {
$rwy = $post['newrunway'];
if($rwy !== "") {
$form_data['runways'][] = $rwy;
$form_data['rwystored'] = true;
}
return $this->new($form_data);
}
return view('templates/header', ['title' => 'Create an airport'])
. view('airports/success')
. view('templates/footer');
}
}
airports/create.php :
<h2><?= esc($title) ?></h2>
<?= session()->getFlashdata('error') ?>
<?= validation_list_errors() ?>
<form action="/airports" method="post">
<?= csrf_field() ?>
<label for="ICAOcode">ICAOcode</label>
<input type="input" name="ICAOcode" value="<?= set_value('ICAOcode') ?>">
<br>
<fieldset>
<legend>Runways</legend>
<?php if(!is_null($runways)): ?>
<ul>
<?php foreach ($runways as $runway): ?>
<li><a href="/airports/new/<?= esc($runway, 'url') ?>"><?= esc($runway) ?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<label for="newrunway">New RWY</label>
<input type="input" name="newrunway" value="<?php if(!$rwystored) echo(set_value('newrunway')) ?>">
<button type="submit" name="action" value="runway">Enter</button>
</fieldset>
<fieldset>
<legend>Radios</legend>
<ul>
<li><a href="#MKK">MKK</a></li>
<li>171.00</li>
<li> VORDME Ha</li>
</ul>
<ul>
<li><a href="#AK">AK</a></li>
<li>341.0</li>
<li> NDB HH</li>
</ul>
<label for="newsign">New sign</label>
<input type="input" name="newsign" value="<?= set_value('newsign') ?>">
<label for="newfreq">Freq</label>
<input type="input" name="newfreq" value="<?= set_value('newfreq') ?>">
<label for="newtype">Type</label>
<input type="input" name="newtype" value="<?= set_value('newtype') ?>">
<input type="submit" name="radio" value="Enter">
</fieldset>
<input type="submit" name="saveindb" value="Save in DB">
<input type="submit" name="abort" value="Abort">
<input type="submit" name="delete" value="Delete">
</form>
Im very new to CodeIgniter, using 4.5.5. I got a HTML form, a sophisticated one, to manage data in a database. Suddenly the validation form error view does not show errors with a point marker in different rows, but all errors without markers in a single row. I dont know what i have changed causing this. I didn engage a git, so i cannot roll back. When i look into the HTML code, CodeIgniter produces, the only difference to the tutorial first app i see is the ::marker is missing in the li tags.
See the form error view
<?php
namespace App\Controllers;
use App\Models\FlightsModel;
use CodeIgniter\Exceptions\PageNotFoundException;
class Airports extends BaseController
{
public function new(?array $form_data = null)
{
helper('form');
if( is_null($form_data)) {
$form_data['rwystored'] = false;
$form_data['ICAOcode'] = "";
$form_data['runways' ] = null; // indexed array
$form_data['radios' ] = []; // indexed array for radio number with associative array for sign, freq and type
}
$this->session->set($form_data);
return view('templates/header', ['title' => 'Create an airport'])
. view('airports/create', $form_data)
. view('templates/footer');
}
public function create()
{
helper('form');
$form_data = $this->session->get();
$data = $this->request->getPost(['ICAOcode', 'newrunway']);
// Checks whether the submitted data passed the validation rules.
if (! $this->validateData($data, [
'ICAOcode' => 'required|max_length[4]|min_length[4]',
'newrunway' => 'max_length[3]',
])) {
// The validation fails, so returns the form.
$form_data['rwystored'] = false;
return $this->new($form_data);
}
$post = $this->validator->getValidated();
// Store new runway and redisplay the form
$data = $this->request->getPost(['action']);
if($data['action'] === 'runway') {
$rwy = $post['newrunway'];
if($rwy !== "") {
$form_data['runways'][] = $rwy;
$form_data['rwystored'] = true;
}
return $this->new($form_data);
}
return view('templates/header', ['title' => 'Create an airport'])
. view('airports/success')
. view('templates/footer');
}
}
airports/create.php :
<h2><?= esc($title) ?></h2>
<?= session()->getFlashdata('error') ?>
<?= validation_list_errors() ?>
<form action="/airports" method="post">
<?= csrf_field() ?>
<label for="ICAOcode">ICAOcode</label>
<input type="input" name="ICAOcode" value="<?= set_value('ICAOcode') ?>">
<br>
<fieldset>
<legend>Runways</legend>
<?php if(!is_null($runways)): ?>
<ul>
<?php foreach ($runways as $runway): ?>
<li><a href="/airports/new/<?= esc($runway, 'url') ?>"><?= esc($runway) ?></a></li>
<?php endforeach ?>
</ul>
<?php endif ?>
<label for="newrunway">New RWY</label>
<input type="input" name="newrunway" value="<?php if(!$rwystored) echo(set_value('newrunway')) ?>">
<button type="submit" name="action" value="runway">Enter</button>
</fieldset>
<fieldset>
<legend>Radios</legend>
<ul>
<li><a href="#MKK">MKK</a></li>
<li>171.00</li>
<li> VORDME Ha</li>
</ul>
<ul>
<li><a href="#AK">AK</a></li>
<li>341.0</li>
<li> NDB HH</li>
</ul>
<label for="newsign">New sign</label>
<input type="input" name="newsign" value="<?= set_value('newsign') ?>">
<label for="newfreq">Freq</label>
<input type="input" name="newfreq" value="<?= set_value('newfreq') ?>">
<label for="newtype">Type</label>
<input type="input" name="newtype" value="<?= set_value('newtype') ?>">
<input type="submit" name="radio" value="Enter">
</fieldset>
<input type="submit" name="saveindb" value="Save in DB">
<input type="submit" name="abort" value="Abort">
<input type="submit" name="delete" value="Delete">
</form>
Share
Improve this question
edited Dec 12, 2024 at 9:45
DarkBee
15.6k8 gold badges72 silver badges117 bronze badges
asked Nov 21, 2024 at 4:34
ralphtoepperralphtoepper
11 silver badge1 bronze badge
1 Answer
Reset to default 0It looks like your not return back to the form or any view with validation errors you can use method $this->validate($array_of_rules)
and return back validation error if found
for example
public function new(){
$rule = [
'ICAOcode' => 'required|max_length[4]|min_length[4]',
'newrunway' => 'max_length[3]',
];
if($this->validate($rule)){
//do some stuff validation passed
}else{
return redirect()->back()->withInput();
}
}
make sure in your veiw you have called helper('form')
and then in your views output validation errors by <?= validation_list_errors() ?>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742313813a4420430.html
评论列表(0条)