<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/**
* Class Issue
*/
class Issue extends CI_Controller
{
public function index()
{
$this->load->library('form_validation');
// Test: If you add one character in username or email the validation will run and show the errors...
//$userdata = ['username' => '', 'email' => ''];
$userdata = $this->input->get();
$this->form_validation->set_data($userdata);
$this->form_validation->set_rules([
[
'field' => 'username',
'label' => 'Username',
'rules' => 'min_length[5]|max_length[32]'
. (isset($userdata['username']) && $userdata['username'] === '' ? '|isset' : ''),
],
[
'field' => 'email',
'label' => 'Email',
'rules' => 'valid_email'
. (isset($userdata['email']) && $userdata['email'] === '' ? '|isset' : ''),
],
]);
if ( ! $this->form_validation->run())
{
var_dump($this->form_validation->error_array());
exit;
}
foreach ($userdata as $key => $val)
{
if ( ! in_array($key, ['username', 'email']))
{
unset($userdata[$key]);
}
}
if (empty($userdata))
{
var_dump('Nothing to update. Stop here.');
exit;
}
echo 'Validated! Data to be updated: ';
var_dump($userdata);
}
}