jeudi 18 septembre 2014

yii active form validation on modal dialog


Vote count:

0




I'm trying to validate CActiveForm on modal dialog. I use bootstrap. The problem is modal dialog. When I read the contents of form during the load of main html page JavaScriopt validation works, but when I load the contents on click of button, validation scrips are gone. Here is the view of calling page



<?php
/* @var $this SiteController */

$this->pageTitle=Yii::app()->name . ' - Все магазины';
$this->breadcrumbs=array(
'Shops',
);
$jsCreate = "$(\".createShop\").click(function(){
var target = $(this).attr('data-target');
var url = '?r=site/editShop';
$(target).find(\".modal-dialog\").load(url);
});";
Yii::app()->getClientScript()->registerScript('create-shop-script',$jsCreate,CClientScript::POS_READY);

$jsEdit = "$(\".editShop\").click(function(){
var target = $(this).attr('data-target');
var url = $(this).attr('href');
$(target).find(\".modal-dialog\").load(url);
});";
Yii::app()->getClientScript()->registerScript('edit-shop-script',$jsEdit,CClientScript::POS_READY);
?>

<h2>Все объекты</h2>
<div id="shopModal" class="modal fade" aria-hidden="true" style="display: none;">
<div class="modal-dialog">

</div>
</div>

<table class="table table-hover">
<thead>
<tr>
<th>Название <span class="hidden-xs">объекта</span></th>
<th><span class="glyphicon glyphicon-ok visible-xs" data-toggle="tooltip" title="Статус активности"></span><span class="hidden-xs">Статус</span></th>
</tr>
</thead>
<?php foreach($shops as $shop) :?>
<?php $disabled = ($shop->active) ?'' :'gray'?>
<tr>
<td><span class="<?php echo $disabled; ?>"><?php echo $shop->name; ?></span></td>
<td>
<?php echo ($shop->active) ?"<span data-toggle='tooltip' title='Активен' class='glyphicon glyphicon-eye-open'></span>" :"<span data-toggle='tooltip' title='Неактивен' class='glyphicon glyphicon-eye-close'></span>" ?>
&nbsp; <a data-toggle='modal' data-target='#shopModal' class="editShop" href="?r=site/editShop&amp;id=<?php echo $shop->id; ?>"><span data-toggle='tooltip' title='Редактировать' class="glyphicon glyphicon-edit"></span></a>
&nbsp; <a><span data-toggle='tooltip' title='Удалить' class="glyphicon glyphicon-remove-circle"></span></a>
</td>
</tr>
<?php endforeach; ?>
</table>
<button style='float:right;' data-toggle='modal' data-target='#shopModal' class="createShop btn btn-primary"><span data-toggle="tooltip" title="Добавить объект"><span class="glyphicon glyphicon-plus"></span> <span class="hidden-xs">Добавить объект</span></span></button>


This is the view of form, that appears inside modal dialog



<?php
/*
@var $this SiteController
@var $model ShopForm
@var $form CActiveForm */
?>
<div class="modal-content">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'shop-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
'htmlOptions'=> array('class'=>'bottom0',),
'action'=>"?r=site/editShop&id={$model->id}",
));?>

<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true"><span data-toggle="tooltip" title="Закрыть">×</span></button>
<h4 class="modal-title">Объект</h4>
</div> <!-- end modal-header -->

<div class="modal-body">
<div class="form-group">
<?php echo $form->textField($model,'name',array('data-toggle'=>'tooltip', 'title'=>'Название объекта', 'class'=>'form-control', 'placeholder'=>'Название', 'maxlength'=>'50')); ?>
<?php echo $form->error($model,'name'); ?>
</div>
<div class="form-group">
<label data-toggle="tooltip" title="Статус активноси">
<?php echo $form->checkBox($model,'active') .' '. $form->label($model,'active') . $form->error($model,'active'); ?>
</label>
</div>
</div> <!-- end modal-body -->


<div class="modal-footer">
<!-- Save button -->
<button type="submit" class="btn btn-primary"><span data-toggle="tooltip" title="Сохранить"><span class="glyphicon glyphicon-ok"></span> <span class="hidden-xs">Сохранить</span></span></button>
<!-- Cancel button -->
<button type="button" class="btn btn-default" data-dismiss="modal"><span data-toggle="tooltip" title="Отменить"><span class="glyphicon glyphicon-remove"></span> <span class="hidden-xs">Отменить</span></span></button>
</div> <!-- end modal-footer -->
<?php $this->endWidget(); ?>
</div>


This is my from model



<?php
class ShopForm extends CFormModel{
public $id;
public $name;
public $active;

public function rules()
{
return array(
// name, email, subject and body are required
array('name', 'required', 'message'=>'Название объекта не может быть пустым'),
array('name', 'length', 'max'=>100),
);
}
public function attributeLabels()
{
return array(
'active'=>'Активен',
);
}
}


This is my site controller



public function actionEditShop($id=null)
{
if (Yii::app()->user->isGuest){
return;
}

// Uncomment the following line if AJAX validation is needed
// $this->performAjaxValidation($model);
$model = $this->loadShop($id);
if(isset($_POST['Shops']))
{

// create/update shop
$fields = $_POST['Shops'];
$fields['id'] = $id;
//$fields['active'] = ($fields['active']) ? '1' :'0';
$model->attributes=$fields;
if($model->save())
$this->redirect(array('shops'));
} else {
// open form to create/update
$this->renderPartial('shop',array('model'=>$model,));
}
}

public function loadShop($id)
{
if ($id){
$model=Shops::model()->findByPk($id);
if($model===null)
throw new CHttpException(404,"Объект под номером $id не существует.");
return $model;
} else {
return new Shops; // creates with active='1'
}
}


asked 30 secs ago







yii active form validation on modal dialog

Aucun commentaire:

Enregistrer un commentaire