This is my code to test my rule, I set it always error but when I click submit it no error?
view.php
<?php
$this->breadcrumbs = array(
'Book',
);
$form = $this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id' => 'book',
'type' => 'horizontal',
'enableClientValidation' => true,
'clientOptions' => array(
'validateOnSubmit' => true,
),
));
echo CHtml::beginForm();
echo CHtml::errorSummary($model);
echo $form->errorSummary($model);
echo $form->textFieldRow($model, 'C_name', '', array());
?>
<div class="control-group">
<label class="control-label" for="Customers_C_time">
เวลาที่จะใช้บริการ
<span class="required"> * </span>
</label>
<div class="controls">
<?php
echo $form->dropDownList($model, 'C_time', $this->HH(), array('empty' => 'เลือก',
'ajax' => array(
'type' => 'POST',
'url' => CController::createUrl('Customers/MM'),
'update' => '#Customers_drpMinute',
'data' => array('hour' => 'js:this.value'),
), 'class' => 'input-small',
));
echo " : ";
echo $form->dropDownList($model, 'drpMinute', array('empty' => 'เลือก'), array('class' => 'input-small',));
echo $form->error($model, 'C_time');
echo $form->error($model, 'drpMinute');
?>
</div>
</div>
<?php
echo $form->dropDownListRow($model, 'C_seats', $this->getSeat(), array('empty' => 'เลือก',
'class' => 'input-small',
));
echo $form->error($model, 'status');
?>
<div class="controls" id="submit">
<?php
$this->widget('bootstrap.widgets.TbButton', array(
'label' => 'Book',
'buttonType' => 'submit',
'htmlOptions' => array(
'name' => 'submit',
),
));
?>
</div>
<?php
echo CHtml::endForm();
$this->endWidget();
?>
Model.php
public function rules() {
return array(
array('C_time', 'isAvailable'),
);
}
public function isAvailable($attribute, $params) {
$r_model = new Restaurant();
$service = $r_model->getTime("HOUR", "R_service") * 60 + $r_model->getTime("MINUTE", "R_service");
$hh = $this->C_time;
$mm = $this->drpMinute;
$time = $hh . $mm . '00';
$endtime = ($hh * 60) + $mm + $service;
$connection = Yii::app()->db;
$sql1 = 'SELECT COUNT(C_seats) as n FROM customers WHERE C_time >=' . $time . 'AND C_time < ' . $endtime . ';';
$sql2 = 'SELECT R_tables as t FROM r_details WHERE R_seats = ' . $this->C_seats . ';';
$command1 = $connection->createCommand($sql1);
$command2 = $connection->createCommand($sql2);
$rs1 = $command1->queryRow();
$rs2 = $command2->queryRoq();
if($rs1 >= $rs2)
$this->addError('status', 'Not Available');
else
$this->addError('status', 'Available');
}