CodeIgniter Forums
Ajax and ci4 validation - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Ajax and ci4 validation (/showthread.php?tid=81896)



Ajax and ci4 validation - spreaderman - 05-16-2022

Just wondering if it is possible to use ci4’s from validation instead using say jquery’s validate()? If so, is there an example anywhere? I assume not possible as cannot see an example but thought I would still ask. Thank you.


RE: Ajax and ci4 validation - iRedds - 05-16-2022

Can you explain more precisely. For example, I do not understand what you are talking about.


RE: Ajax and ci4 validation - spreaderman - 05-16-2022

(05-16-2022, 06:28 PM)iRedds Wrote: Can you explain more precisely. For example, I do not understand what you are talking about.

Normally, I place my validation in the Model (sometimes place in controller) something like this;

PHP Code:
    $category $this->model->find($category_id);
        if (
$this->request->getMethod() === 'post') {
            
$category->fill($this->request->getPost());
            if (
$this->model->save($category)) {
                return 
redirect()->to('/admin/post/categories')
                                 ->
with('info''Success - category was updated');
            } else {
                return 
redirect()->back()
                                 ->
with('errors'$this->model->errors())
                                 ->
with('warning''Invalid Data')
                                 ->
withInput();
            }
        } else {
            
$this->data['category'] = $this->model->find($category_id);
            return 
view('/Admin/Category/category_update'$this->data    );
        } 

My rules are somethin like;


PHP Code:
protected $validationRules    = [
        
        
'category_name' => 'required|is_unique[category.category_name]|min_length[5]|max_length[50]',
        
'category_user_id' => 'required',
         
'category_description' => 'required|min_length[5]|max_length[250]',
        ];
    protected 
$validationMessages = [
        
'category_name' => [
            
'required' => 'The category name is required',
            
'is_unique' => 'The category already exists',
            
'min_length' => 'The minimum length is 5 characters',
            
'max_length' => 'The maximum length is 50 characters'
            
],
        
'category_description' => [
            
'required' => 'The category description is required',
            
'min_length' => 'The minimum length is 5 characters',
            
'max_length' => 'The maximum length is 50 characters'
            
],   
        ]; 

How would like use the above in case of using ajax. Is is possible?


RE: Ajax and ci4 validation - manager - 05-16-2022

(05-16-2022, 05:58 PM)spreaderman Wrote: Just wondering if it is possible to use ci4’s from validation instead using say jquery’s validate()?  If so, is there an example anywhere?  I assume not possible as cannot see an example but thought I would still ask.  Thank you.
Jquery's validate() method is a client-side validation. CodeIgniters form validation is server-side validation. There is a BIG difference. 
You should always use server-side validation for security reasons, even if you use client-side validation. Never trust user input.

Client-side validations in general used to reduce the number of useless calls to the server.
For example, in your site you have a signup to newsletter form with one input field to enter email address. If there is no client-side validation user may send you empty form, or just text or number instead of email, or just send you invalid email address. All this calls is useless and gives extra load to the webserver.


RE: Ajax and ci4 validation - iRedds - 05-16-2022

Don't use redirect.
PHP Code:
return $this->response->setJson(['errors' => $this->model->errors()]); 



RE: Ajax and ci4 validation - manager - 05-16-2022

(05-16-2022, 10:05 PM)spreaderman Wrote: How would like use the above in case of using ajax.  Is is possible?
In your controller you should handle ajax call, something like
PHP Code:
if ($request->isAJAX()) {
            
// ...
        




RE: Ajax and ci4 validation - spreaderman - 05-17-2022

Thank you very much for all the tips! Super helpful. Have read more on the topic. I am happy to have learnt the advice about keeping the validation on the server. I would have hated to have invested weeks into client side validation. Thanks.