문제

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>
도움이 되었습니까?

해결책

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>

다른 팁

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.

  1. in your controller use below syntex

     if($this->{$this->pageModel}->validates())
     {
        // your code....
     }
     else{
           // your errors 
        $errors = $this->{$this->pageModel}->validationErrors;
        $this->set('errors',$errors);
     }
    
  2. in your ctp file put below code where you want to show your error message

    if(isset($errors) && !empty($errors['fieldname']))
    { 
        echo $errors['fieldname'][0];
    }
    
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top