2012-06-20 62 views
1
public function actionEquipmentLoanRequestCreate() 
{ 



    if(isset($_POST['EquipmentRequest'])) 
    { 
     $ids = $_POST['EquipmentRequest']['equipID']; 
     $equipment = Equipment::model()->findAllByPk($ids); 
     $requests = self::instantiateEquipmentRequests($equipment); 
     //echo "<pre>"; 
     //die(count($requests)." ".CActiveForm::validate($requests)); 


     $booking = new EquipmentBooking; 



     if(isset($_POST['EquipmentRequest']) && $_POST['EquipmentBooking']) 
     { 
      $booking->attributes = $_POST['EquipmentBooking']; 
      $requestPostedVars = $_POST['EquipmentRequest']; 
      //have posted collection of equipment 
      //need to take count 
      //request below not really useful??? 
      $equipmentRequests = array(); 
      foreach($requestPostedVars as $request) 
      { 
       if(isset($request)) 
       { 
        $equipmentRequest = new EquipmentRequest('loanRequest'); 
        $equipmentRequest->attributes = $request; 
        array_push($equipmentRequests,$equipmentRequest); 


       } 

      } 
      $models = $equipmentRequests; 
      $models[]=$booking; 
      $this->performAjaxValidation($models); 
      $booking->equipment = $equipmentRequests; 
      if ($booking->save()){ 
       self::assignBookingIds($equipmentRequests,$booking); 
       $this->redirect('index'); 
      } 


     } 
     //displays view to create loan request 
     $this->render('equipment-loan-request',array('requests' => $requests,'booking' => $booking)); 


    } else { 
      Yii::app()->user->setFlash('error', "You need to select equipment first!"); 
      $this->redirect(array('simulation/equipment'), true); 
    } 


} 

public function instantiateEquipmentRequests($equipment) 
{ 
    $equipmentRequests = array(); 
    foreach($equipment as $item) 
    { 
     $request = new EquipmentRequest('loanRequest'); 
     $request->equipment = $item; 
     $request->qty = 1; 

     array_push($equipmentRequests,$request); 
    } 
    return $equipmentRequests; 
}  

形式视图忽略多余的按钮后更换 - 香港专业教育学院直接试图调用请求,而不是仅仅在预订或两个以下,以及没有用。Yii有通过AJAX验证许多

<?php $form=$this->beginWidget('CActiveFormExtended', array(
     'id' => 'equipment-booking-form', 
     'enableAjaxValidation' => true, 
     'clientOptions' => array(
      'validateOnSubmit' => true, 
      'validateOnChange'=>true, 

     ), 

)); ?> 
    <div class="content-bg">   

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



    if(isset($requests) && count($requests) > 0) { 
      foreach($requests as $i => $request) { 
      $this->renderPartial('_requestedEquipment', array("index" => $i, "request" => $request, "form"=> $form)); 
      } 
     }?> 
    </div> 
    <br /> 


    <hr class="brown"/> 



    <h2>Request Details</h2> 
    <div class="content-bg"> 
     <h3>Step 1</h3> 
     <?php echo $form->label($booking,'organisation'); ?> 
     <?php echo $form->dropDownList($booking, 'organisation', $booking::$organisations,array('id' => 'organisation', 'class' => "selectfield",'empty' => 'Select')); ?> 
    </div> 
    <br /> 
    <div class="content-bg"> 
     <h3>Step 2</h3> 
     <?php echo $form->label($booking,'name'); ?> 
     <?php echo $form->textField($booking,'name',array('size'=>60,'maxlength'=>255)); ?> 
        <?php echo $form->error($booking,'name'); ?> 

     <?php echo $form->label($booking,'email'); ?> 
     <?php echo $form->textField($booking,'email',array('size'=>60,'maxlength'=>255)); ?> 
        <?php echo $form->error($booking,'email'); ?> 

     <?php echo $form->label($booking,'phone'); ?> 
     <?php echo $form->textField($booking,'phone',array('size'=>60,'maxlength'=>255)); ?> 
     <?php echo $form->error($booking,'phone'); ?> 

    </div> 
    <br /> 
    <div class="content-bg"> 
     <h3>Step 3</h3> 
     <?php echo $form->label($booking,'address'); ?> 
     <?php echo $form->textField($booking,'address',array('size'=>60,'maxlength'=>255)); ?> 
     <?php echo $form->error($booking,'address'); ?> 

     <?php echo $form->label($booking,'suburb'); ?> 
     <?php echo $form->textField($booking,'suburb',array('size'=>60,'maxlength'=>255)); ?> 
     <?php echo $form->error($booking,'suburb'); ?> 

     <?php echo $form->label($booking,'postcode'); ?> 
     <?php echo $form->textField($booking,'postcode',array('size'=>60,'maxlength'=>255)); ?> 
     <?php echo $form->error($booking,'postcode'); ?> 

    </div> 

    <br /> 
    <div class="content-bg"> 
     <h3>Step 4</h3> 
     <div class="five columns alpha"> 
     <?php echo $form->label($booking,'pickupDate'); ?> 
     <?php echo $form->dropDownDayList($booking, 'pickupDate',array('class' => 'date-day pickup_select','id' => 'pickup')); ?> 
     <?php echo $form->dropDownYearList($booking,1, 'pickupDate',array('class' => 'date-monthyear pickup_select','id' => 'pickup-month')); ?> 
     <?php echo $form->hiddenField($booking, 'pickupDate',array('id' => 'pickupData')); ?> 
     <?php echo $form->error($booking,'pickupDate'); ?> 

     </div> 
     <div class="five columns alpha"> 
     <?php echo $form->label($booking,'returnDate'); ?> 
     <?php echo $form->dropDownDayList($booking, 'returnDate',array('class' => 'date-day return_select','id' => 'return')); ?> 
     <?php echo $form->dropDownYearList($booking,1, 'returnDate',array('class' => 'date-monthyear return_select','id' => 'return-month')); ?> 
     <?php echo $form->hiddenField($booking, 'returnDate',array('id' => 'returnData')); ?> 
     <?php echo $form->error($booking,'returnDate'); ?> 

     </div> 
     <br class="clear"/> 
    </div> 

    <br /> 
    <div class="content-bg"> 
     <h2>Terms &amp; Conditions</h2> 
     <p>By submitting this loan enquiry you agree to and acknowledge our <a href="equiploanterms">Equipment Loan Terms and Conditions</a>.</p> 
    </div> 
    <?php echo CHtml::submitButton('Submit'); ?> 
    <br /> 
<input class="orange-btn right" type="submit" value="Submit Loan Request"> 

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

EquipmentRequest部分

<div class="selected-equipment" id="request_<?php echo $request->equipment->equipName; ?>"> 
<div class="equipment-selected"><a href="#"><?php echo $request->equipment->equipName; ?></a></div> 
    <div class="equipment-qty"><?php echo $form->textField($request, "[$index]qty", array('value' => 1,'class' => 'requestQuantity')); ?></div> 
    <?php echo $form->error($request,"[$index]qty"); ?> 

    <div class="equipment-update"><a href="#" class="brown-btn small"><span>Remove</span></a></div> 
    <?php echo $form->hiddenField($request, "[$index]equipID",array('value' => $request->equipment->equipID)); ?> 
       <?php echo $form->error($request,"[$index]equipID"); ?> 

    <br class="clear"> 
</div> 

编辑 请注意:我已经纠正了上述问题。主要是由于performAjaxValidate的错误用法导致的,我现在已经设置了这个用于调用预订和设备请求集合。

这现在让我得到2验证错误消息对象回来,1为两个模型。但是只有预订模型被传递给错误摘要div。

@ bool.dev感谢您的帮助,到目前为止,任何想法与上面,试图在errorSummary

合并设备的请求对象的收集和登记对象的一个​​参数调用我已经编辑了以上显示更新控制器动作和视图

+0

它如何不承认equipId?确切的错误是什么? –

+0

你可以在这里发表评论,并让我知道 –

+0

我已经把它带到了现在我正在接受对这两种模型的正确验证错误的阶段。但到目前为止,实际表格中显示的唯一错误是预订,而不是设备请求。 将更新以上问题的详细信息 – ridget

回答

1

当你想errorSummary()超过一个型号,用途:

$form->errorSummary(array($model1,$model2,$model3)); 
+0

你以前试过这个吗? –

+0

直接使用@ bool.dev与我的对象集合进行了尝试:array_push($ equipmentRequests,$ equipmentRequest); 所以当我调用errorSummary通过 errorSummary($ equipmentRequests)直接调用数组; 以及errorSummary(array($ equipmentRequests)) 使用数组方法获得空对象错误。 Wierd位是我肯定从ajax验证调用中获得两个单独的对象,它们都是正确的。 – ridget

+0

欣赏所有帮助顺便说一句。 – ridget