1

Is there a way to test if a particular text field has an error so that i can add a Class to highlight the who text box

<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>

i want to add has-error to <div class="form-group"> div if name has any errors

Holt
  • 36,600
  • 7
  • 92
  • 139
Harsha M V
  • 54,075
  • 125
  • 354
  • 529
  • 1
    You should think about using a CakePHP plugin for Bootstrap to avoid these problem, most of them would automatically add the `has-error` class to the `form-group` div if there is an error in `name`. – Holt Aug 10 '15 at 09:05
  • thanks will check it out :) – Harsha M V Aug 10 '15 at 11:31

1 Answers1

1

You can use

$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>
Holt
  • 36,600
  • 7
  • 92
  • 139