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

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

If all elements are wrapped in form, you can use like this :

if(!$('#form-id')[0].checkValidity()){
document.querySelector('#form-id').reportValidity();
return false;
}
Reply
#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




Theme © iAndrew 2016 - Forum software by © MyBB