CakePHP error message

EDITED

CakePHP Version: 2.2.4

When validating the falis input, the error message that CakePHP generates appears 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, for example:

<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 out how I can do this. 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>

      

+3


source to share


3 answers


I actually solved it. I add 'error' => false

and post the error 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>

      

+7


source


The input type 'checkbox' should already post an error after the input.

But to specify the exact order, use the key format

in the array $options

:



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

      

Just change the order of these array elements according to what you need.

+2


source


  • 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);
     }
    
          

  • in your ctp file placed below code where you want to show your error message

    if(isset($errors) && !empty($errors['fieldname']))
    { 
        echo $errors['fieldname'][0];
    }
    
          

0


source







All Articles