Hi All
I was try to use documentation and create model, form & controller for submitting form by ajax.
But validation do not work.
Please, help to fix this problem
Form:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'contact-form',
'enableAjaxValidation'=>true,
'enableClientValidation'=>false,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
....
<?php
echo CHtml::ajaxSubmitButton('Отправить сообщение', '', array(
'type' => 'POST',
'update' => '#contact-form-div',
),
array('type' => 'submit'));
?>
...
<?php $this->endWidget();?>
Controller:
public function actionIndex(){
$model=new ContactForm;
$this->performAjaxValidation('contact-form',$model);
$flashMessage = 'Наш консультант свяжется с вами...';
if(Yii::app()->request->isAjaxRequest){
if(isset($_POST['ContactForm'])){
$model->attributes=$_POST['ContactForm'];
$onlineform=$flashMessage;
echo CHtml::encode($onlineform);
//MailContent::send(MailContent::INFO,$model);
}
Yii::app()->end();
}
}
protected function performAjaxValidation($form,$model){
if(isset($_POST['ajax']) && $_POST['ajax']===$form)
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
moidel ContactForm:
<?php
/**
* ContactForm class.
* ContactForm is the data structure for keeping
* contact form data. It is used by the 'contact' action of 'SiteController'.
*/
class ContactForm extends CFormModel
{
public $name;
public $email;
public $phoneNumber;
public $subject;
public $body;
public $verifyCode;
public $readOnly = false;
/**
* Declares the validation rules.
*/
public function rules()
{
return array(
// name, email, subject and body are required
array('name, email, phoneNumber, body, verifyCode', 'required'),
// email has to be a valid email address
array('email', 'email'),
array('subject, phoneNumber','safe'),
array('phoneNumber', 'length', 'max'=>16),
array('phoneNumber', 'length', 'min'=>16),
array('phoneNumber', 'match','pattern' =>'/^((\+?7)\(*(\d{3})\)*)(\d{3})(-?\d{2})(-?\d{2})$/',
'message' => 'Некорректный формат поля "{attribute}"'),
// verifyCode needs to be entered correctly
array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
);
}
/**
* Declares customized attribute labels.
* If not declared here, an attribute would have a label that is
* the same as its name with the first letter in upper case.
*/
public function attributeLabels()
{
return array(
'name' => 'Ваше имя',
'email' => 'Ваш email',
'subject' => 'Тема сообщения',
'phoneNumber' => 'Телефон для связи',
'body' => 'Опишите кратко свою проблему',
'verifyCode'=>'Код верификации',
);
}
}