phparraysvalidationyii2yii2-advanced-app

Array validation in yii2?


I am using dynamic form in yii2. My posting data is an array like this

[ModelAddress] => Array
    (
        [0] => Array
            (
                [fullname] => xxxx
                [lastname] => xxxx
                [date] => 15 Apr 1985
                [DOB] => 09 Jan 1985
                [percentage] => 20 %
            )

        [1] => Array
            (
                [fullname] => xxx
                [lastname] => xxxx
                [date] => 15 Apr 1985
                [DOB] => 09 Jan 1985
                [percentage] => 20 %
            )

        [2] => Array
            (
                [fullname] =>xxxx
                [lastname] => xxx
                [date] => 15 Apr 1985
                [DOB] => 09 Jan 1985
                [percentage] => 20 %
            )

I want to check total percentage must be 100. But i am using compare validation it is validate each percentage.But i want to total percentage. My form like this

 <?php foreach ($modelsAddress as $i => $modelAddress): ?>
            <div class="item panel">


                    <div class="pull-right">
                        <button type="button" class="remove-item btn btn-danger btn-xs"><i class="glyphicon glyphicon-minus"></i></button>
                    </div>
                    <div class="clearfix"></div>

                <div class="panel-body">
                         <div class="row">
                            <div class="col-sm-2">
                            <?= $form->field($modelAddress, "[{$i}]fullname")->textInput(['maxlength' => true])->label(false) ?>
                            </div>
                            <div class="col-sm-2">
                            <?= $form->field($modelAddress, "[{$i}]lastname")->textInput(['maxlength' => true])->label(false) ?>
                            </div>
                            <div class="col-sm-2">
                            <?= $form->field($modelAddress, "[{$i}]date")->textInput(['maxlength' => true])->label(false) ?>
                            </div>
                            <div class="col-sm-2">
                            <?= $form->field($modelAddress, "[{$i}]DOB")->textInput(['maxlength' => true])->label(false) ?>
                             </div>
                            <div class="col-sm-2">
                            <?= $form->field($modelAddress, "[{$i}]percentage")->textInput(['maxlength' => true])->label(false) ?>
                           </div>
                       </div>

                </div>
            </div>
        <?php endforeach; ?>

Please help any one.Thanks in advance


Solution

  • I finally write my own validation in controller.

     $postvalue = Yii::$app->request->post();                         
              foreach ($postvalue['ModelAddress'] as $value) {
                    $total = $total +$value['percentage'];
              } 
    
    
              if($total != 100)
              {
                Yii::$app->session->setFlash('error', 'Percentage Must be 100');
              }else{
                return $this->redirect('/Home');
              }