CodeIgniter Forums
How to use method setRule? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forum-31.html)
+--- Thread: How to use method setRule? (/thread-75298.html)



How to use method setRule? - 5flex - 01-23-2020

Sorry for my bad English.

Today I read Official User Guide about Validation Library. From the very beginning, the tutorial on this library has been going sequentially, but after the setRule() methods it breaks off and there is no logical conclusion just like a tutorial. I don't understand how to work the next code:

PHP Code:
$validation->setRule('username''Username''required'); 
  
Ok! I set up one rule for my form field. In tutorial wrote about it, but how to use and run the next checking? If in the condition I leave the code as in the example below, then the method setRule() has no effect. My form do not show any errors after send. What should I write in the condition if()? Another words - how to activate validation mechanism?

In my Controller I have:

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

$validation->setRule('username''Username''required');

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


and further... about 
Code:
withRequest()
In what cases can this method be applied? Can I have a simple example?

Thanks in advance.