Hi,
I am getting mad on something I am sure is simple.
In my form I have a starting datetime field (name it $valid_from) whose default value is today @ 00:00:00 but the user can change it to whatever he likes.
[size="2"]When the user choose a date from the associated datepicker I need to update four other fields in the same form:[/size]
-
the end datetime (name it $valid_to equals to $valid_from plus 12 months)
-
another datetime (name it $expiration_from equals to $valid_from)
-
another endtime (name it $expiration_to equals $start1 plus 45 days)
-
the number of working days between $expiration_from and $[size="2"]expiration_to[/size]
Now, I have solved all the stuff of the calculation and thought I knew how to update another form field but it doesn’t work
Somebody can spread some light on this?
The form:
<div class="form">
<?php
/** @var OnePassengerCardController $this */
/** @var OnePassengerCard $modelPassengerCard */
/** @var AweActiveForm $form */
$form = $this->beginWidget('ext.AweCrud.components.AweActiveForm', array(
'id' => 'one-passenger-card-form',
'enableAjaxValidation' => true,
'enableClientValidation'=> true,
'focus'=>array($modelPassengerCard,'card_id'),
));
?>
<p class="note">
<?php echo Yii::t('AweCrud.app', 'Fields with') ?> <span class="required">*</span>
<?php echo Yii::t('AweCrud.app', 'are required') ?>.
</p>
<?php echo $form->errorSummary(array($modelPassengerCard, $modelCardCredit)); ?>
<div class="well left">
<?php echo $form->textFieldRow($modelPassengerCard, 'card_id', array('class' => 'span3')) ?>
<?php
// echo $form->datetimePickerRow(
echo $form->datepickerRow(
$modelPassengerCard,
'valid_from',
array(
'prepend'=>'<i class="icon-calendar"></i>',
'class' => 'span2',
'options'=>array(
// 'format'=>'dd/mm/yyyy hh:ii:ss',
'format'=>'dd/mm/yyyy 00:00:00',
'autoclose'=>true
),
'ajax' => array(
'type'=>'POST',
'url'=>CController::createUrl('onePassengerCard/dynamic'),
'update'=>'#OnePassengerCard_valid_to',
)
)
)
?>
<?php echo $form->textFieldRow($modelPassengerCard, 'valid_to', array('class' => 'span2', 'readonly' => true)) ?>
</div>
<div class="well left">
<?php if($modelPassengerCard->passenger->passenger_type_id >= 1 && $modelPassengerCard->passenger->passenger_type_id <= 2):?>
<?php echo $form->textFieldRow($modelCardCredit, 'available_trips', array('class' => 'span1', 'readonly' => true)) ?>
<?php endif?>
<?php if($modelPassengerCard->passenger->passenger_type_id == 3):?>
<?php echo $form->textFieldRow($modelCardCredit, 'available_trips', array('class' => 'span1')) ?>
<?php endif?>
<?php echo $form->textFieldRow($modelCardCredit, 'expiration_from', array('class' => 'span2', 'readonly' => true)) ?>
<?php echo $form->textFieldRow($modelCardCredit, 'expiration_to', array('class' => 'span2', 'readonly' => true)) ?>
</div>
<div class="form-actions clear">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>$modelPassengerCard->isNewRecord ? Yii::t('AweCrud.app', 'Create') : Yii::t('AweCrud.app', 'Save'),
)); ?>
<?php $this->widget('bootstrap.widgets.TbButton', array(
'label'=> Yii::t('AweCrud.app', 'Cancel'),
'htmlOptions' => array('onclick' => 'javascript:history.go(-1)')
)); ?>
</div>
<?php $this->endWidget(); ?>
</div>
The Controller:
<?php
class OnePassengerCardController extends AweController
{
/**
* @var string the default layout for the views. Defaults to '//layouts/column2', meaning
* using two-column layout. See 'protected/views/layouts/column2.php'.
*/
public $layout = '//layouts/column1';
public $defaultAction = 'admin';
/**
* Creates a new model.
* If creation is successful, the browser will be redirected to the 'view' page.
*/
public function actionCreate($id)
{
$modelPassengerCard = new OnePassengerCard;
$modelPassengerCard->passenger_id = $id;
$modelPassengerCard->valid_from = date("d/m/Y 00:00:00");
$startingDate = new DateTime(date("Y-m-d"));
$endingDate = $startingDate->modify('+1 year');
$modelPassengerCard->valid_to = $endingDate->format('d/m/Y 23:59:59');
$modelCardCredit = new OneCardCredit;
switch ($modelPassengerCard->passenger->passenger_type_id)
{
case (1):
$modelCardCredit->available_trips = OneHoliday::WorkingDaysTillEndOfMonth()*2;
$modelCardCredit->expiration_from = $modelPassengerCard->valid_from;
$modelCardCredit->expiration_to = date("d/m/Y H:i:s");;
break;
case (2):
$modelCardCredit->available_trips = OneHoliday::WorkingDaysTillEndOfMonth();
$modelCardCredit->expiration_from = $modelPassengerCard->valid_from;
$modelCardCredit->expiration_to = date("d/m/Y H:i:s");;
break;
case (3):
$modelCardCredit->expiration_from = $modelPassengerCard->valid_from;
$modelCardCredit->expiration_to = date("d/m/Y H:i:s");;
break;
}
$this->performAjaxValidation(array($modelPassengerCard, $modelCardCredit), 'one-passenger-card-form');
if(isset($_POST['OnePassengerCard'], $_POST['OneCardCredit']))
{
$modelPassengerCard->attributes = $_POST['OnePassengerCard'];
$modelCardCredit->attributes = $_POST['OneCardCredit'];
$modelCardCredit->card_id = $modelPassengerCard->card_id;
$valid=$modelPassengerCard->validate();
$valid=$modelCardCredit->validate() && $valid;
if($valid)
{
$modelPassengerCard->save(false);
$modelCardCredit->save(false);
$this->redirect(array('admin', 'id' => $modelPassengerCard->passenger_id));
}
}
$this->render(
'create',array(
'modelPassengerCard' => $modelPassengerCard,
'modelCardCredit' => $modelCardCredit,
)
);
}
/**
* Deletes a particular model.
* If deletion is successful, the browser will be redirected to the 'admin' page.
* @param integer $id the ID of the model to be deleted
*/
public function actionDelete($id)
{
if(Yii::app()->request->isPostRequest)
{
// we only allow deletion via POST request
$this->loadModel($id)->delete();
// if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
if(!isset($_GET['ajax']))
$this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
}
else
throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
}
/**
* Lists all models.
*/
public function actionIndex()
{
$dataProvider=new CActiveDataProvider('OnePassengerCard');
$this->render('index', array(
'dataProvider' => $dataProvider,
));
}
/**
* Manages all models.
*/
public function actionAdmin($id)
{
$model = new OnePassengerCard('search');
$model->passenger_id = $id;
if(isset($_GET['OnePassengerCard']))
$model->attributes = $_GET['OnePassengerCard'];
$this->render('admin', array(
'model' => $model,
));
}
/**
* Returns the data model based on the primary key given in the GET variable.
* If the data model is not found, an HTTP exception will be raised.
* @param integer the ID of the model to be loaded
*/
public function loadModel($id, $modelClass=__CLASS__)
{
$model = OnePassengerCard::model()->findByPk($id);
if($model === null)
throw new CHttpException(404,'The requested page does not exist.');
return $model;
}
/**
* Performs the AJAX validation.
* @param CModel the model to be validated
*/
protected function performAjaxValidation($model, $form=null)
{
if(isset($_POST['ajax']) && $_POST['ajax'] === 'one-passenger-card-form')
{
echo CActiveForm::validate($model);
Yii::app()->end();
}
}
[size=2] [/size][size=2]public function actionDynamic()[/size]
{
$dummyDate = DateTime::createFromFormat('d/m/Y H:i:s', $_POST['OnePassengerCard']['valid_from']);
$dummyDate = $dummyDate->modify('+'.OneCompany::findCardValidityMonths().' months');
$dummyDate = $dummyDate->modify('-1 seconds');
$_POST['OnePassengerCard']['valid_to'] = $dummyDate->format('d/m/Y H:i:s');
echo $_POST['OnePassengerCard']['valid_to'];
}
}
[size=2]
[/size]