Welcome Guest, Not a member yet? Register   Sign In
Ajax and ci4 validation
#1

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.
Reply
#2

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

(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?
Reply
#4

(This post was last modified: 05-16-2022, 11:19 PM by manager.)

(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.
Reply
#5

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

(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()) {
            
// ...
        

Reply
#7

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.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB