Two Forms (1 Modal) On Same Page

Hi all:

I’ve been struggling with this for a couple of days and still can’t make it work.

I have a medical referral form that first looks up the patient in the database. When the patient is new and therefore not found, I have a button that activates a modal dialog on top of the referral form, instead of loading the regular patient creation form. This dialog contains another form to define the patient. The problem is that the newly entered patient is not saved to the database, and no warnings nor errors are thrown.

ReferralController.php:




public function actionCreate()

{

    $model = new Referral;

    $modelPatient = new Patient;


    if(isset($_POST['Referral'])) {

        $model->attributes = $_POST['Referral'];

        $model->ref_status = Referral::REFERRALSTATUS_PENDING;

            

        if($model->save()) {

            $this->redirect(array('view','id'=>$model->id));

        }

    }


    $this->render('create',array(

        'model'=>$model,

        'modelPatient'=>$modelPatient,

    ));

}



PatientController.php:




public function actionCreatemodal()

{

    $model = new Patient;


    if(!empty($_POST['Patient'])) {

        $model->attributes = $_POST['Patient'];

        $model->is_active = Patient::PATIENTSTATUS_ACTIVE;

        if($model->save()) {

            Yii::app()->end();	// return to previous page is handled in javascript in form

        }

    }

    $this->renderPartial('_modalform',array(

        'model'=>$model,

    ),false,false);

}



View referral/create.php:




<?php $this->widget('bootstrap.widgets.TbBox', array(

    'title' => Yii::t('app','CREATE NEW REFERRAL').'&nbsp;<i>'.Yii::t('app','Fields with <span style="color:red;">*</span> are required.').'</i>',

    'headerIcon' => 'fa fa-file-text',

    'headerButtons'=>array(

        array(

            'class'=>'bootstrap.widgets.TbButton',

            'buttonType'=>'link',

            'type'=>'info',

            'label'=> Yii::t('app','New Patient'),

            'icon'=> 'fa fa-female',

            'htmlOptions' => array(

                'data-toggle' => 'modal',

                'data-target' => '#newPatientModal',

            ),

        ),

    ),

    'content' => $this->renderPartial('_form', array(

        'model'=>$model,

        'modelPatient'=>$modelPatient,

        ),

        true)

    ));

?>



View referral/_form.php (notice the modal dialog code at the end):




<?php


// javascript scripts for form processing

Yii::app()->clientScript->registerScript('resetform',

    "$('#reset-btn').click(function()

        {

            $('#patientdata').html('');

            $('#patientdata').hide();

        });

    

    ",

    CClientScript::POS_READY

);

Yii::app()->clientScript->registerScript('isarray',

    "function isArray(ob)

        {

            return ob.constructor === Array;

            //return Object.prototype.toString.call(ob) === '[object Array]';

        }

    ",

    CClientScript::POS_READY

);

Yii::app()->clientScript->registerScript('loadselectedpatient',

    "$('.patientbox').on('click', '.hrlink', function(event){

        $.post('fetchpatient',{'patient2lookup' : $(this).attr('id')},function(data)

        {

            $('#Referral_patient_id').val(data.id);

            $('#pidinput').val(data.last_name + ', ' + data.first_name);

            $('#patientdata').html(data.healthrecord_number+'<br>'+data.patientage+'<br>'+data.sex+'<br>'+data.telephone);

        },

        'json'

        );

    });

    $('#patientdata').html('');

    $('#patientdata').hide();

    ",

    CClientScript::POS_READY

);

?>


<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(

    'id'=>'referral-form',

    'type'=>'vertical',

    'enableAjaxValidation'=>false,

    'enableClientValidation'=>false,

    'focus'=>array($modelPatient,'patient2lookup'),

)); ?>


<?php

if(!($model->isNewRecord))	// if existing, load parent data

{

    $modelPatient->patient2lookup = $modelPatient->last_name.', '.$modelPatient->first_name;

    $listreferees = Provider::model()->getListRefereesByRefType($model->ref_type);

}

else

{

    $listreferees = array();

}

?>

<div class="well well-small">

<h3><?php echo Yii::t('app','Referral Information'); ?></h3>

<fieldset>

<?php echo $form->errorSummary($model); ?>

<div class="row-fluid">

    <div class="span7">

    <?php echo $form->textFieldRow($modelPatient,'patient2lookup',array(

        'id'=>'pidinput',

        'placeholder'=>Yii::t('app','Record # or Last,First Name'),

        'prepend'=>'<i class="fa fa-search"></i>',

    ));

    echo '&nbsp;&nbsp;';

    $this->widget('bootstrap.widgets.TbButton', array(

	'id' => 'lookup-btn',

        'type'=>'info',

	'buttonType' => 'ajaxButton',

	'icon'=>'icon-search',

	'label'=>Yii::t('app','Lookup'),

        'size'=>'small',

        'url'=>$this->createUrl('fetchpatient'),

        'ajaxOptions'=>array(

            'type'=>'POST',

            'dataType'=>'json',

            'data'=>array('patient2lookup'=>'js:$("#pidinput").val()'),

            'success'=>'js:function(data){

                if (isArray(data) && data.length > 0) {

                    for (var i in data) {

                        var hrecnum = data[i].healthrecord_number;

                        var patage = (data[i].birthdate == "0000-00-00") ? "?" : ((new Date() - new Date(data[i].birthdate)) / 31557600000).toFixed(0);

                        switch (data[i].sex) {case "1":patsex="F";break;case "2":patsex="M";break;default:patsex="O";break;}

                        var patinfo = data[i].last_name + ", " + data[i].first_name + " - " + patage + " " + patsex;

                        $("#patientdata").append("<div class=\"hrlink\" id=" + hrecnum + ">" +

                            hrecnum.link("#") + "  " + patinfo + "</div>"

                        );

                    }

                }

                else if (data.length != 0) {

                    $("#Referral_patient_id").val(data.id);

                    $("#pidinput").val(data.last_name + ", " + data.first_name);

                    $("#patientdata").html(data.healthrecord_number+"<br>"+data.patientage+"<br>"+data.sex+"<br>"+data.telephone);

                }

                else {

                    $("#patientdata").html("'.Yii::t("app","&nbsp;&nbsp;&nbsp;PATIENT NOT FOUND!").'");

                }

                $("#patientdata").show();

            }',

	),

    ));

    echo '&nbsp;&nbsp;';

    $this->widget('bootstrap.widgets.TbButton', array(

	'id' => 'reset-btn',

	'buttonType' => 'reset',

	'label'=>Yii::t('app','Reset'),

        'size'=>'small',

    ));

    ?>

    <?php echo $form->hiddenField($model,'patient_id'); ?>

    <div id="patientdata" class="patientbox" style="display:none"></div>

    <?php echo $form->dropDownListRow($model,'ref_type',$model->getAllRefTypes(),array(

        'prompt'=>Yii::t('app','(Select Type)'),

        'ajax'=>array(

            'type'=>'POST',

            'url'=>$this->createUrl('fetchrefereesbyreftype'),

            'data'=>array('reftype'=>'js:this.value'),

            'success'=>'js:function(data){

                $("#Referral_referee_id").html(data);

            }',

        ),

        )

    ); ?>

    <?php echo $form->datepickerRow($model,'referral_date',array('options'=>array('format'=>'yyyy-mm-dd','autoclose'=>true,'language'=>Yii::app()->language)),array('prepend'=>'<i class="fa fa-calendar"></i>')); ?>

    </div>

    <div class="span5">

    <?php echo $form->dropDownListRow($model,'procdiag_id',$this->FetchActiveProcDiagDescriptions,array('prompt'=>Yii::t('app','(Select Proc/Diag)'))); ?>

    <?php echo $form->dropDownListRow($model,'referrer_id',$this->FetchActiveReferrers,array('prompt'=>Yii::t('app','(Select Referrer)'))); ?>

    <?php echo $form->dropDownListRow($model,'referee_id',$listreferees,array('empty'=>Yii::t('app','(Choose Ref Type First)'))); ?>

    </div>

</div>

</div>

<div class="well well-small">

<h3><?php echo Yii::t('app','Follow Up'); ?></h3>

<div class="row-fluid">

    <?php echo $form->datepickerRow($model,'appointment_date',array('options'=>array('format'=>'yyyy-mm-dd','autoclose'=>true,'language'=>Yii::app()->language)),array('prepend'=>'<i class="fa fa-calendar"></i>')); ?>

    <?php echo $form->textAreaRow($model,'comments',array('placeholder'=>Yii::t('app','Comments if any'),'rows'=>6, 'cols'=>50, 'class'=>'span8')); ?>

</div>

</fieldset>

<br><br>

<div>

<?php

    $this->widget('bootstrap.widgets.TbButton', array(

    	'buttonType'=>'submit',

	    'type'=>'primary',

    	'label'=>$model->isNewRecord ? Yii::t('app', 'Create') : Yii::t('app', 'Save'),

	    'icon'=>'fa fa-save',

    ));

    echo '&nbsp;&nbsp;&nbsp;';

    $this->widget('bootstrap.widgets.TbButton', array(

    	'buttonType'=>'link',

	    'label'=>  Yii::t('app', 'Cancel'),

    	'url'=> $this->createUrl('/site/mainview'),

	    'icon'=>'fa fa-times-circle',

    ));

?>

</div>

<!-- ********************************* CREATE NEW PATIENT DIALOG ********************************* -->

<?php $this->beginWidget('bootstrap.widgets.TbModal',array('id' => 'newPatientModal','options'=>array('backdrop'=>false))); ?>


<div class="modal-header well well-small">

    <a class="close" data-dismiss="modal">&times;</a>

    <h4 style="text-align: center;"><?php echo Yii::t('app','New Patient'); ?></h4>

</div>


<div class="modal-body">

    <?php echo $this->renderPartial('//patient/_modalform',array('model'=>new Patient()),true,true); ?>

</div>




<?php $this->endWidget(); ?>

<?php $this->endWidget(); ?>




View patient/_modalform.php (renderPartialed by the modal dialog):




<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(

    'id'=>'patientmodal-form',

    'type'=>'horizontal',

    'enableAjaxValidation'=>true,

    'action'=>$this->createUrl('/patient/createmodal'),

)); ?>


<div class="well well-small" style="background-color: #fffacd;">

<fieldset>

<?php echo $form->errorSummary($model); ?>

<div class="row-fluid">

    <div class="span6">

        <?php echo $form->textFieldRow($model,'first_name',array('maxlength'=>32)); ?>

    </div>

    <div class="span6">

        <?php echo $form->textFieldRow($model,'last_name',array('maxlength'=>32)); ?>

    </div>

</div>

<div class="row-fluid">

    <div class="span6">

        <?php echo $form->textFieldRow($model,'healthrecord_number',array('maxlength'=>32)); ?>

    </div>

    <div class="span6">

        <?php echo $form->textFieldRow($model,'telephone',array('maxlength'=>32)); ?>

    </div>

</div>

<div class="row-fluid">

    <div class="span6">

        <?php echo $form->datepickerRow($model,'birthdate',array('options'=>array('format'=>'yyyy-mm-dd','autoclose'=>true,'language'=>Yii::app()->language)),array('prepend'=>'<i class="fa fa-calendar"></i>')); ?>

    </div>

    <div class="span6">

        <?php echo $form->dropDownListRow($model,'sex',$model->getAllPatientSexes()); ?>

    </div>

</div>

</fieldset>

</div>

<div>

    <?php

    $this->widget('bootstrap.widgets.TbButton', array(

        'buttonType'=>'submit',

        'type'=>'primary',

        'label' => Yii::t('app','Save'),

        'icon'=>'fa fa-save',

        'htmlOptions' => array('data-dismiss' => 'modal'),

    ));

    echo '&nbsp;&nbsp;&nbsp;';

    $this->widget('bootstrap.widgets.TbButton', array(

        'label'=>  Yii::t('app', 'Cancel'),

        'url'=> '#',

        'icon'=>'fa fa-times-circle',

        'htmlOptions' => array('data-dismiss' => 'modal'),

    ));

    ?>

</div>


<?php $this->endWidget(); ?>




As I said, the modal displays fine but when submitted the patient is not saved, nor is there any trace of route ‘patient/createmodal’ being executed. FireBug only shows the original GET create for the referral. Any ideas?

Thanks,