georgik
(Yurij Stehura)
April 20, 2015, 9:55pm
1
I’m new to Yii and trying to make registration form using CPortlet. I faced a problem that ajax validation is not triggered, because it does not pass this standart condition:
if(isset($_POST['ajax']) && $_POST['ajax'] == 'registration-form')
I’ve searched a lot, but couldn’t find any solution. Is that possible that pressing submit button in form doesn’t create POST ajax variable? enableAjaxValidation is set to ‘true’ in form.
1 Like
alirz23
(Alirz23)
April 20, 2015, 11:13pm
2
do you have ajax validation enabled on the form?
paste your form code
georgik
(Yurij Stehura)
April 21, 2015, 1:06am
3
I am using this form as widget, so Controller (in my case - CPortlet) and it’s view are in ‘Components’ folder.
This is view
<?php $form1=$this->beginWidget('CActiveForm', array(
'id'=>'registration-form',
'enableAjaxValidation'=>true,
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
)
));?>
<div class="row">
<?php echo $form1->textField($form,'username'); ?>
<?php echo $form1->error($form,'username');?>
</div>
......
<div class="row submit">
<?php echo CHtml::submitButton(Yum::t('Registration')); ?>
</div>
<?php $this->endWidget(); ?>
and Controller
class RegistrationWidget extends CPortlet
{
public function init()
{
parent::init();
}
protected function renderContent()
{
Yii::import('application.modules.profile.models.*');
$form = new YumRegistrationForm;
$profile = new YumProfile;
$this->performAjaxValidation(array($form,$profile));
if (isset($_POST['YumRegistrationForm'])&&isset($_POST['YumProfile'])) {
$form->attributes = $_POST['YumRegistrationForm'];
$profile->attributes = $_POST['YumProfile'];
if(!$form->hasErrors() && !$profile->hasErrors()) {
$user = new YumUser;
$user->register($form->username, $form->password, $profile);
$user->profile = $profile;
}
}
$this->render($this->view, array('form' => $form,'profile' => $profile));
}
protected function performAjaxValidation($models) {
if(isset($_POST['ajax']) && $_POST['ajax'] == 'registration-form') {
echo CActiveForm::validate($models);
Yii::app()->end();
}
}
}