(11-04-2019, 11:03 AM)seunex Wrote: Hello guys am stuck tying to learn this ci4 validations
this is my view code
PHP Code:
<?= form_open('main/get_post') ?>
<h5>Username</h5>
<input type="text" name="username" value="<?= old('username') ?>" size="50" />
<div><input type="submit" value="Submit" /></div>
<?= form_close(); ?>
here my controller
PHP Code:
public function get_post()
{
$validation = \Config\Services::validation();
$validation->setRules([
'username' => ['label' => 'Username', 'rules' => 'required'],
'password' => ['label' => 'Password', 'rules' => 'required|min_length[10]']
]);
$errors = $validation->getErrors();
foreach($errors as $error)
{
echo $error;
}
}
i really do no know how to check errors i follow d docs i can get it
onlike ci4 we use if($this->form_validation == false;
here i am deeply confuse.
I dont see a password field in your form. That may be something to resolve.
Also the getErrors() method returns key=> value pairs. Try echo $errors['username'];
Also, normally you display error messages on the form itself , so.....
if(!$validation->withRequest($this->request)->run()){
echo view('myformviewpage');
}else{
$model->save(...);
}
then in the formview try
<?= \Config\Services::validation()->listErrors(); ?>