CakePHP Form添加类的错误

时间:2015-08-10 07:51:09

标签: cakephp cakephp-2.5

有没有办法测试特定文本字段是否有错误,以便我可以添加一个类来突出显示who文本框

<div class="form-group">
    <?php echo $this->Form->label('name', 'Name:', array('class' => 'col-sm-2 control-label')); ?>
    <div class="col-sm-10">
        <?php echo $this->Form->text('name', array('required' => false, 'class' => 'form-control input-lg')); ?>
        <?php echo $this->Form->error('name', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
    </div>
</div>

如果has-error有任何错误,我想将<div class="form-group">添加到name div

1 个答案:

答案 0 :(得分:1)

您可以使用

$this->Form->isFieldError('fieldname');
<div class="form-group <?= ($this->Form->isFieldError('name'))? 'has-error': '' ; ?>">
    <?php echo $this->Form->label('name', 'Name:', array('class' => 'col-sm-2 control-label')); ?>
    <div class="col-sm-10">
        <?php echo $this->Form->text('name', array('required' => false, 'class' => 'form-control input-lg')); ?>
        <?php echo $this->Form->error('name', null, array('class' => 'label label-block label-danger text-left', 'wrap' => 'label')); ?>
    </div>
</div>