№15 Dars - Yiida dinamik tekshiruv qoidalar biriktirish.

№15 Dars - Yiida dinamik tekshiruv qoidalar biriktirish.

Yiida o`zi ishlab chiqqan tekshiruv qoidalaridan tashqari o`zingiz uchun ham mos tekshiruv funksiyalari yaratsangiz bo`ladi. Bunday yondashuv o`zimiz uchun shaxsiy tekshiruvlarni dinamik yaratish imkonini beradi. Keling misol bilan bu mavzuni oydinlashtirib olaylik.

Masalani qo`yilishi: startDateTime endDateTime dan kichkina bo`lishi kerak. Buni qoida tarzida kiritishimiz kerak.

Masalani yechilishi: Bir jadval berilgan unda startDateTime va endDateTime ustunlari mavjud.

Shunday qoida birktirishimiz kerakki endDateTime uchun natijada jadvalga qo`shilgan xar bir ma`lumot startDateTime endDateTime dan kichkina bo`lishi kerak.


1) Jadval yaratamiz:

CREATE TABLE `dates` (
`ID` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
`startDateTime` DATETIME NOT NULL,
`endDateTime` DATETIME NOT NULL,
PRIMARY KEY (`ID`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;

2) Jadvalga mos model hosil qilamiz.

3) Modelga quyidagicha kod kiritamiz:

public function rules()
{
 return array(
  array('startDateTime, endDateTime', 'required'),
  array('ID, startDateTime, endDateTime', 'safe', 'on'=>'search'),
  array('endDateTime', 'compareWithStartDateTime'),
 );
}

"'endDateTime'" uchun "'compareWithStartDateTime'" funksiya biriktirmoqdamiz.

4) pastdan shu funksiyani yozamiz:

public function compareWithStartDateTime($attribute, $params)
{
     if ($this->endDateTime < $this->startDateTime)
     $this->addError('endDateTime', 'endDateTime kichkina startDateTime dan.');
}

5) Controllerimizga quyidagicha kod kiritamiz:

$model = new Dates;
            if(isset($_POST['Dates']))
            {
                   $model->attributes=$_POST['Dates'];
                   if($model->validate())
                   {
                           $model->save();
                           $this->redirect(Yii::app()->user->returnUrl);
                   }
            }
            $this->render('contact',array('model'=>$model));

6) Viewimiz quiydagicha kod kiritamiz:

<h1>Dates</h1>
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
 'id'=>'contact-form',
 'enableClientValidation'=>true,
 'clientOptions'=>array(
  'validateOnSubmit'=>true,
 ),
)); ?>
<div class="row">
 <?php echo $form->labelEx($model,'startDateTime'); ?>
<?php
            $this->widget('zii.widgets.jui.CJuiDatePicker',
                array(
                    'model' => $model,
                    'attribute' => 'startDateTime',
                    'language' => Yii::app()->language,
                    'options' => array(
                        // 'buttonImage' => '/backend/i/calendar-view-month.png',
                        // 'buttonImageOnly' => 'true',
                        'showButtonPanel' => false,
                        'showOn' => 'both',
                        'changeMonth' => 'true',
                        'changeYear' => 'true',
                        'showOtherMonths' => 'true',
                        'selectOtherMonths' => 'true',
                        'yearRange' => 'c-100:c',
                    ),
                )
            );
            ?>
<?php echo $form->error($model,'startDateTime'); ?>
</div>
<div class="row">
 <?php echo $form->labelEx($model,'endDateTime'); ?>
<?php
            $this->widget('zii.widgets.jui.CJuiDatePicker',
                array(
                    'model' => $model,
                    'attribute' => 'endDateTime',
                    'language' => Yii::app()->language,
                    'options' => array(
                        // 'buttonImage' => '/backend/i/calendar-view-month.png',
                        // 'buttonImageOnly' => 'true',
                        'showButtonPanel' => false,
                        'showOn' => 'both',
                        'changeMonth' => 'true',
                        'changeYear' => 'true',
                        'showOtherMonths' => 'true',
                        'selectOtherMonths' => 'true',
                        'yearRange' => 'c-100:c',
                    ),
                )
            );
            ?>
 <?php echo $form->error($model,'endDateTime'); ?>
</div>
<div class="row buttons">
 <?php echo CHtml::submitButton('Submit'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->

7) Viewimiz ko`rinishi:

8) Rulesdan o`ta olmagandagi holat:

Ko`rib turganingizdek "Strat date time" katta "End date time" dan bizning qoidamiz bo`yicha "End date time" katta bo`lishi kerak edi. Shuning uchun bu yerda biz keltirgan xatolik xabari chiqmoqda.

Rulesdan muvofaqiyatli o`tsa demak jadvalga qoida bo`yicha qoshildi. Ishlarimiz to`g`ri amalga oshmoqda. Muvofaqiyatli o`tsa web ilovamizni asosiy ilovaga yo`naltirib yuboradi.

Manba:


JONNY - Texnoman foydalanuvchisi

Muallif haqida

JONNY Arduino, Java, C#, Android, Windows, Linux, Debian, Javascript. O'zbekistonni rivojlantiramiz! Dasturlash orqali vatanimizni yangi marralarga olib chiqamiz.


Blogdagi so‘nggi maqolalar:


Birinchi bo‘ling!

Iltimos, fikr bildirish uchun saytga kiring yoki ro‘yxatdan o‘ting!