Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP error message position

EDITED

CakePHP Version: 2.2.4

When an input validation falis the error message that CakePHP generates is positioned after my input element

<div class="control-group">
    <label class="control-label">Name <span class="required-field">*</span></label>
        <div class="controls">
            <input name="data[User][name]" class="input-xlarge form-error" type="text" value="">
            <div class="error-message">This field cannot be left blank.</div>
            <input type="hidden" name="data[User][public_name]" id="UserPublicName_" value="0">
           <input type="checkbox" name="data[User][public_name]" class="span1" value="1">
        </div>
 </div>

But I want to place it after my checkbox, like this:

<div class="control-group">
    <label class="control-label">Name <span class="required-field">*</span></label>
        <div class="controls">
           <input name="data[User][name]" class="input-xlarge form-error" type="text" value="">
           <input type="hidden" name="data[User][public_name]" id="UserPublicName_" value="0">
           <input type="checkbox" name="data[User][public_name]" class="span1" value="1">
           <div class="error-message">This field cannot be left blank.</div>
        </div>
 </div>

I've read FormHelper::input() but I can't figure how I can do it. I would like to use inputDefaults.

EDITED

My .ctp

<div class="control-group">
    <label class="control-label">Nanme <span class="required-field">*</span></label>
        <div class="controls">
            <?php
               echo $this->Form->input('name', array(
                   'type' => 'text', 'class' => 'input-xlarge'));
               echo $this->Form->checkbox('public_name', array('class' => 'span1'));
            ?>
        </div>
</div>
like image 398
Marcos Avatar asked Apr 20 '26 10:04

Marcos


2 Answers

Actually I've resolved it. I've add 'error' => false and positioned the error message where I wanted with $this->Form->error()

My .ctp

<div class="control-group">
  <label class="control-label">Nanme <span class="required-field">*</span></label>
    <div class="controls">
        <?php
           echo $this->Form->input('name', array(
               'type' => 'text', 'class' => 'input-xlarge', 'error' => false));
           echo $this->Form->checkbox('public_name', array('class' => 'span1'));
           echo $this->Form->error('User.name', null, array('class' => 'error-message'));
        ?>
    </div>
</div>
like image 91
Marcos Avatar answered Apr 24 '26 01:04

Marcos


The 'checkbox' input type should already be putting the error after the input.

But to specify the exact order, use the format key in input's $options array:

$this->Form->input('fieldname', 
    array('type'=>'checkbox',
          'format'=>array('before', 'input', 'between', 'label', 'after', 'error')
));

Just change the order of those array elements to match what you need.

like image 42
Costa Avatar answered Apr 24 '26 02:04

Costa