• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use ajax/json with validation ?

#1
Hello,

I'm actually working on a new project with CI4.
It's greats :-)

But I have some problems wit ajax request and validation.

My view :
Code:
axios.post({
    method: 'post',
    url: '/my_url',
    data: {
        email: '[email protected]',
        id: 12
    }
})
My Controller :
PHP Code:
$this->validation->setRules([
    
'email' => ['label' => 'Email''rules' => 'required|valid_email',],
    
'id' => ['label' => 'Lien URL''rules' => 'required|integer',],
];
        
if (! 
$this->validation->withRequest($this->request)->run()) {
     return 
$this->response->setStatusCode(400$this->validation->listErrors());
}

return 
$this->response->setJSON([
     
'success'    => true,
]); 

What's wrong ?
Reply

#2
(11-23-2018, 11:48 AM)bvrignaud Wrote: Hello,

I'm actually working on a new project with CI4.
It's greats :-)

But I have some problems wit ajax request and validation.

My view :
Code:
axios.post({
    method: 'post',
    url: '/my_url',
    data: {
        email: '[email protected]',
        id: 12
    }
})
My Controller :
PHP Code:
$this->validation->setRules([
 
   'email' => ['label' => 'Email''rules' => 'required|valid_email',],
 
   'id' => ['label' => 'Lien URL''rules' => 'required|integer',],
];
 
       
if (! $this->validation->withRequest($this->request)->run()) {
 
    return $this->response->setStatusCode(400$this->validation->listErrors());
}

$this->return $this->response->setJSON([
 
    'success'    => true,
]); 

What's wrong ?

Code:
$this->return $this->response->setJSON([ // <<<<-------- $this->return $this->response... ?? or return $this->... ??
     'success'    => true,
]);
Reply

#3
(11-23-2018, 12:13 PM)Piotr Wrote:
Code:
$this->return $this->response->setJSON([ // <<<<-------- $this->return $this->response... ?? or return $this->... ??
     'success'    => true,
]);
Sorry, I changed for return $this->...
Reply

#4
(11-23-2018, 12:54 PM)bvrignaud Wrote:
(11-23-2018, 12:13 PM)Piotr Wrote:
Code:
$this->return $this->response->setJSON([ // <<<<-------- $this->return $this->response... ?? or return $this->... ??
     'success'    => true,
]);
Sorry, I changed for return $this->...
I'm using responseTrait gor json response

PHP Code:
....
use 
ResponseTrait;
....
...
return 
$this->respond([
...
...
]); 
Reply

#5
Thanks.

Sorry for the late, I haven't seen you have post a reply.

A had the same solution.

Thanks
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.