validating form which is rendered using renderPartial in yii - ajax

I have more then one forms on one page and I am rendering one form using renderPartial, now if I want to validate it using ajax validation it don't work.
view code
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'bill-shipp-form',
'action'=>CController::createUrl('cart/index'),
'enableAjaxValidation'=>true,
'focus'=>array($billingShippingInfo,'first_name_b'),
//'enableClientValidation'=>true,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange'=>false,
'afterValidate'=>'js:postBillShipp'
),
)); ?>
and in cart/index I have
if(isset($_POST['ajax']) && $_POST['ajax']==='bill-shipp-form')
{
echo CActiveForm::validate($billingShippingInfo);
Yii::app()->end();
}
Thanks in advance

You should use 4th parameter for render partial:
$this->render('view',$data,false,TRUE);
The 4th paramater is processOutput and you should set it to true
API

Related

Yii2 ActiveForm Ajax Validation and submit

I have made an Active form with validation and ajax submit.
The form is created in view:
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'layout' => 'horizontal',
'method' => 'post',
'enableClientValidation' => false,
'enableAjaxValidation' => true,
'validationUrl' => 'panel/validate',
'fieldConfig' => [
'options' => [
'tag' => false,
],
'template' =>'{input}<p class="help-block help-block-error ">{error}</p>'
],
]); ?>
The validation action:
public function actionValidate()
{
$model = new LoginForm();
$request = \Yii::$app->getRequest();
if ($request->isAjax && $request->isPost && $model->load(Yii::$app->request->post())) {
\Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}
return $this->renderAjax('login', [
'model' => $model,
]);
}
When i leave the fields blank for example, or do not specify the captcha i can see the ajax response:
{"loginform-username":["Username cannot be blank."],"loginform-password":["Password cannot be blank."],"loginform-captcha":["Captcha cannot be blank."]}
However, those errors not getting shown under my form fields. The form fields are created like this:
<?= $form->field($model, 'username')->textInput()
If i turn off ajax validation, the erros are displayed. What can be wrong here?
I'm afraid there is no possible way to display error while turning off 'tag' = false in fieldConfig.
Even though it works for server-side validation, the main problem is how yii.activeForm.js updateInput() function for fields works. When ajax request is completed, the .js tries to find an outer tag (of field) with .field-<model>-<attribute> class selector and fetch error-div children. As long as there is no outer .field tag, no error message is append to form.
I'm not 100% sure about it, but this is my understanding from debugging yii.activeForm.js functionality.
Actually, here is the similar question in yii2/issues, where SilverFire explains that there is no way to achieve this.
ActiveForm fieldConfig options tag=>false will render class attribute without any tag

Yii ajax loads whole page instead of my output

I have a problem in YII with ajax.
I used this link as exaple to test ajax in my project with depended drop-down lists.
Form code (protected/views/game/_form.php):
<?php
echo $form->dropDownList($model, 'season_id', Season::getSeasonsList(), array(
'ajax' => array(
'type'=>'POST',
CController::createUrl('Game/selectGameStages'),
'update'=>'#Game_season_game_stage'
)
)
);
?>
Controller code (protected/Controller/GameController.php):
public function actionSelectGameStages()
{
echo CHtml::tag('option', array('value'=>'1'), 'Some output 1', true);
echo CHtml::tag('option', array('value'=>'2'), 'Some output 2', true);
echo CHtml::tag('option', array('value'=>'3'), 'Some output 3', true);
}
Ajax is working, but with debugger if founded that code, putted in my Game_season_game_stage select is the html code of the whole page of my site, like as
<select name="Game[season_game_stage] id="Game_season_game_stage">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
...
<div class="container" id="page">...</div>
Please help to understand why whole code of page loaded instead of code from my selectGameStages function?
Yii version is 1.14.
Sorry for my English. Thx.
Simply exit after the output.
public function actionSelectGameStages()
{
// Do stuff
...
exit;
}
why noy use dropDownList and directly in the view?
es.
<?php echo CHtml::dropDownList('listname', $select,
array('M' => 'Male', 'F' => 'Female'),
array('empty' => '(Select a gender)'));
http://www.yiiframework.com/wiki/48/by-example-chtml/#hh5
Thank you for all answers! I found solution.
in protected/views/game/_form.php I missed the 'url' key and renamed game/selectGameStages
<?php
echo $form->dropDownList($model, 'season_id', Season::getSeasonsList(), array(
'ajax' => array(
'type'=>'POST',
'url' => CController::createUrl('game/selectGameStages'),
'update'=>'#Game_season_game_stage'
)
)
);
?>
after that I updated controller code (protected/Controller/GameController.php) with this code
public function accessRules()
{
return array(
array('allow', // allow all users to perform 'index' and 'view' actions
'actions'=>array('index','view', 'selectGameStages'),
'users'=>array('*'),
Thanks for the help!
Your whole page is loading because of your layout,
in your controller check if this call, is an ajax call, change your layout to some layout without those tags, or better create a layout for your ajax calls,
if($this->getIsAjaxRequest())
$this->layout = '//ajax'; // ajax layout

Yii - Using Ajax Validation

I'm trying to enable ajax validation in a form, I've added the right parameters to the form, but it's not working, here's my code:
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'user-form',
'enableAjaxValidation'=>true,
'htmlOptions' => array(
'enctype' => 'multipart/form-data',
'enableClientValidation'=>true
),
)); ?>
Please advice.
In the top of your action, add the following lines:
if(isset($_POST['ajax']) && $_POST['ajax']==='user-form'){
echo CActiveForm::validate($model);
Yii::app()->end();
}

2 AJAX calls in the same view (CakePHP 2.3)

I have problem with AJAX in CakePHP. I have 2 different AJAX forms in one view. The first AJAX form works well, but the other one doesn't work properly. When I call the second AJAX, it doesn't set data from this form to $this->request->data, but the AJAX will run properly. After that, it will update <div id="about"></div>. Curious is fact, that after the div is updated and I try send data through this form again, it works and it will update my data in database. Also when I have only the second AJAX in the view, it will work properly and send the data first time I send the data through it.
Here's my code:
This is a view file:
<div id="price-list">
<?php echo $user['User']['price_list']; ?>
</div>
<?php
echo $this->Form->create('User');
echo $this->Form->input('User.price_list', array('label' => false));
echo $this->Js->submit('Save', array(
'url' => array('controller' => 'users', 'action' => 'ajax_edit_price_list'),
'update' => '#price-list',
'buffer' => false,
));
?>
<div id="about">
<?php echo $user['User']['about']; ?>
</div>
<?php
echo $this->Form->create('User');
echo $this->Form->input('User.about', array('label' => false));
echo $this->Js->submit('Save', array(
'url' => array('controller' => 'users', 'action' => 'ajax_edit_about'),
'update' => '#about',
'buffer' => false,
));
?>
I have also append <?php echo $this->Js->writeBuffer(); ?> in my default.ctp before </body>.
Do you have any idea, where can be a problem? Thanks
I wasn't able to reproduce your exact issue, but I think I know the problem. Your second form is being created inside of the first form. You need to add echo $this->Form->end(); to the end of both forms.
Also, the forms are getting created with the same ID. Although this isn't what's causing the problem, it's still not good. I suggest you take thaJeztah's advice and specify an ID manually inside $this->Form->create().

Partial Ajax Pagination in cakePHP

I'm trying to add ajax pagination into a view of my application.
I followed this tutorial: http://book.cakephp.org/view/1600/Ajax-Pagination
So, in this tutorial you always reload the whole content of the view by clicking the pagination links.
But what I want is, that it only reloads the part of the site which includes the paginated data.
I found a solution for myself:
I made a view which only contains the ajax pagination and included it via ajax request:
<?php $this->Html->scriptStart(array('inline' => false));?>
$(document).ready(function() {
<?php
echo $this->Js->request(array(
'controller' => 'topics',
'action' => 'index',
$location['Location']['id']
), array(
'async' => true,
'update' => '#ajax_topics',
));
?>
});
<?php $this->Html->scriptEnd();?>

Resources