Dynamic Hiddenfield Validation


(Jimuel Dedicatoria) #1

Good day guys, please help me I have here a hidden field that will be posted to the controller for checking, if the entered quantity is greater that the hiddenfield remaining quantity then the controller will add a new dynamic validation. This is my codes.

for my view




<div class="row">

                <?php echo $form->labelEx($model,'cons_dosage'); ?>

                <?php echo $form->dropdownlist($model,'cons_dosage', array(),

					array(

						'ajax' => array(

						'type'=>'POST', //request type

						'url'=>CController::createUrl('quantity'),

						'update'=>'#remaining, #hidden',

						))); 

				?>

                <?php echo $form->error($model,'cons_dosage', array('style'=>"color:red")); ?>

            </div>

        

            <div class="row">

                <?php //echo $form->labelEx($model,'cons_quantity'); ?>

                <?php //echo $form->textField($model,'cons_quantity'); ?>

                <?php //echo $form->error($model,'cons_quantity', array('style'=>"color:red")); ?>

                

                <table border="0" width="200">

                	<tr>

                    	<td><?php echo $form->labelEx($model,'cons_quantity'); ?></td>

                        <td align="center">Remaining</td>

                    </tr>

                    <tr>

                    	<td>

							<?php echo $form->textField($model,'cons_quantity', array('style'=>'width:80px')); ?>

                            <?php echo $form->error($model,'cons_quantity', array('style'=>"color:red")); ?>

                        </td>

                        <td align="center">

                        	<div id="remaining"></div>

                            <?php echo $form->hiddenField($model, 'hidden', array('id'=>'hidden', 'name'=>'hidden'))?>

                            <?php echo $form->error($model,'hidden', array('style'=>"color:red")); ?>

                        </td>

                    </tr>

                </table> 

            </div>



for my controller




if(isset($_POST['ShisConsultation']))

				{

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

					$model->cons_user_id = Yii::app()->user->getState('userid');

					$model->cons_date_time = $date;

					$model->cons_cm_id = $_POST['ShisConsultation']['cons_cm_id'];

					$model->cons_medname = $_POST['cons_medname'];

					$model->validatorList->add(

						CValidator::createValidator('required', $model, 'cons_cm_id')

					);					

					if($_POST['cons_medname'] == null)

					{	

						$model->cons_medname = null;

						$model->cons_quantity = null;

						$model->cons_dosage = null;

					}

					else

					{

						$model->validatorList->add(

							CValidator::createValidator('required', $model, 'cons_quantity')

						);							

					}

					

					if(isset($_POST['hidden']))

					{

						if($_POST['ShisConsultation']['cons_quantity'] > $_POST['hidden'])

						{

							$model->validatorList->add(

								CValidator::createValidator('isGreater', $model, 'cons_quantity')

							);	

						}

					}

					

					$model->cons_comment = $_POST['ShisConsultation']['cons_comment'];

					$model->cons_type = "Cast Member";

					$model->cons_flag = 0;

					if($dr_re == null)

					{

						$model->fk_duty = $dr;

					}

					else

					{

						$model->fk_duty = $dr_re;

					}

					if($model->save())

					{

						//if($_POST['cons_medname'] != null)

						//{	

							//$this->actionReduce();

						//}

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

					}

				}

			}

		}

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

			'model'=>$model,

		));



and for the dynamic validation




public function isGreater($attribute, $params)

	{

		if($this->cons_quantity > $this->hidden)

			$this->addError('cons_quantity', 'Quantity must not exceed remaining item.');	

	}



tnx in advance