Welcome Guest, Not a member yet? Register   Sign In
Using the form validator with AJAX
#3

(01-25-2021, 09:15 PM)iampuping Wrote: I am submitting a form to my controller with AJAX, not via the action/method of the form itself. How do I use the validator with this method, since the data is coming in as a post request?

In your controller do something like this:
PHP Code:
$validationRules = array(
                'status' => [
                    'rules'  => 'required|alpha|in_list[active,deleted,disabled]',
                    'errors' => [
                        'required' => 'Your request is invalid',
                        'alpha'    => 'Your request is invalid'
                    ]
                ],
                'rec'    => [
                    'rules'  => 'required|numeric',
                    'errors' => [
                        'required' => 'Your request is invalid',
                        'numeric'  => 'Your request is invalid'
                    ]
                ]
            );

            /* if inputs dont validate */
            if ( ! $this->validate$validationRules ) )
            {
                /* return error */
                return $this->commonLib->sendJsonResponse(
                    400,
                    $this->validator->listErrors"error_template_name" // this is template file name, check validation documentation
                );
            }
            else
            {
                
// get values and save
            

Reply


Messages In This Thread
Using the form validator with AJAX - by iampuping - 01-25-2021, 09:15 PM
RE: Using the form validator with AJAX - by harpreetsb - 02-02-2021, 02:00 AM



Theme © iAndrew 2016 - Forum software by © MyBB