Hello,
I wrote this validator to check if a attribute of my CFormModel is empty when another attribute starts with a string.
public function rules()
{
return array(
// name, email, subject and body are required
array('escolha', 'required'),
array('torneio, clan, servidor', 'numerical', 'integerOnly'=>true),
array('torneio', 'requiredIfStartsWith', 'attribute'=>'escolha', 'value'=>'T'),
array('clan', 'requiredIfStartsWith', 'attribute'=>'escolha', 'value'=>'C'),
array('servidor', 'requiredIfStartsWith', 'attribute'=>'escolha', 'value'=>'S'),
);
}
public function requiredIfStartsWith($attribute,$params)
{
$match = $params['attribute'];
$value = $params['value'];
if(substr($this->$match,0,strlen($value)) == $value)
{
if(empty($this->$attribute))
{
$this->addError($attribute, Yii::t('yii','{attribute} cannot be blank.'));
}
}
}
The validator is working fine, but no error message appears.
View code for the attribute servidor:
<?php echo $form->labelEx($model,'servidor'); ?>
<?php echo $form->dropDownList($model,'servidor', CHtml::listData($servidores, 'id', 'nome'),array('prompt'=>''));?>
<?php echo CHtml::link(CHtml::image(Yii::app()->request->baseUrl."/images/plus_small.png",Yii::t('app','Add Server'),array("title"=>Yii::t('app','Add Server'))), $this->createUrl("servidor/create",array("retorno"=>"loja/index")));?>
<?php echo $form->error($model,'servidor'); ?>
That’s my first time with validators. Anyone see what is the problem?