Yii通过ajax验证有很多

时间:2012-06-20 23:18:08

标签: php yii

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>

EquipmentRequest partial

<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中的一个参数调用

我已编辑上述内容以显示更新的控制器操作和视图

1 个答案:

答案 0 :(得分:1)

如果您希望errorSummary()用于多个模型,请使用:

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