Welcome Guest, Not a member yet? Register   Sign In
Simple validation
#1

(This post was last modified: 04-28-2020, 10:03 AM by jreklund.)

Hello, I have one noobie question. I just started to learn CI4. I would like to validate data in the array. For example...

$data = [
'id' = 10,
'name' = 'whatever'
]

$validation = \Config\Services::validation();

$validation->setRules([
            'id' => 'required',
            'name' => 'required',
        ]);

So how can now validate $data with rules listed above?
Reply
#2

You use it like this, you can read more about it in the manual: https://codeigniter.com/user_guide/libra...ation.html

PHP Code:
use CodeIgniter\Controller;

class 
Form extends Controller
{
    public function 
index()
    {
            
$data = [
                
'id' 10,
                
'name' 'whatever'
            
];
            
            
$validation = \Config\Services::validation();
            
            
$validation->setRules([
                
'id' => 'required',
                
'name' => 'required',
            ]);

            if (! 
$validation->run($data))
            {
                    echo 
view('Signup', [
                            
'validation' => $validation
                    
]);
            }
            else
            {
                    echo 
view('Success');
            }
    }

Reply
#3

Thank you Smile
Reply




Theme © iAndrew 2016 - Forum software by © MyBB