Yii 有很多通过 ajax 验证


Yii has many through ajax validation

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;
}    

表单视图稍后忽略多余的按钮替换 - 我直接尝试调用请求,而不仅仅是预订或下面的两者,但无济于事。

<?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>

设备请求部分

<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(),请使用:

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