private function addError(AjaxFormField $field, array $errors) { if (empty($errors)) { return; } $this->errors[$field->getName()] = $errors; }
use AjaxForm\AjaxForm; use AjaxForm\AjaxFormField; $ajaxForm = new AjaxForm(); $ajaxForm->setFrom('*****@*****.**', 'Ajax Form'); $ajaxForm->addAddress('*****@*****.**'); // $ajaxForm->addCC('*****@*****.**'); // $ajaxForm->addBCC('*****@*****.**'); // $ajaxForm->addReplyTo('*****@*****.**'); $ajaxForm->setSubject('Form is filled'); $ajaxForm->setErrorMessage('Incorrectly filled fields form'); $ajaxForm->setSuccessMessage('Form is sended!'); $nameField = new AjaxFormField('name', 'Name'); $nameField->addValidator('required', 'Name is required'); $nameField->addValidator(function ($value) { if (strlen($value) <= 10) { return true; } return false; }, 'Max length 10'); $mailField = new AjaxFormField('email', 'Email'); $mailField->addValidator('required', 'Email is required'); $mailField->addValidator('email', 'Invalid email'); $phoneField = new AjaxFormField('phone', 'Phone'); $fileField = new AjaxFormField('file', 'File'); $fileField->addValidator('required', 'Choose file'); $fields = array($nameField, $mailField, $phoneField, $fileField); $ajaxForm->addFields($fields); $ajaxForm->setMailTemplateFromPath('template.tpl'); $response = $ajaxForm->send(); header('Content-type:application/json;charset=utf-8'); echo $response;