How can I control where the error message div is displayed in CakePHP forms?
cakephp, cakephp-2.0
Solution
In the VIEW:
1) Disable automatic error display:
echo $this->Form->create('Mymodel', array(
'inputDefaults' => array(
'error' => false
)
));
2) Get the current form errors from `$this->validationErrors`
3) Display them where ever you prefer with your own markup or Cake's default markup via $this->Form->error()
UPDATE: I misunderstood your original question. The order of the elements can be specified via either the create() call (to apply to all fields) or an input() call (for an individual field):
echo $this->Form->create('Mymodel', array(
'inputDefaults' => array(
'format' => array('before', 'error', 'label', 'between', 'input', 'after')
)
));
The above would place the `error` div before the `label` div. You can re-arrange that array in any order you prefer.
Problem
So I understand that CakePHP automatically loads the error message into the view if you validate it against the Model's validators. I am trying to setup a date dropbox validation, and I want to control where the error message shows up. Before submitting to validate form: ``` <div class="input text required"> <label for="UserEmail">Email</label> <input name="data[User][email]" maxlength="50" type="text" id="UserEmail"> </div> ``` After validating, error shows up here: ``` <div class="input text required error"> <label for="UserEmail">Email</label> <input name="data[User][email]" maxlength="50" type="text" value="" id="UserEmail" class="form-error"> <div class="error-message">An email is required</div> </div> ``` Is it possible to control it where I can have it show up here? ``` <div class="input text required error"> <div class="error-message">An email is required</div> <label for="UserEmail">Email</label> <input name="data[User][email]" maxlength="50" type="text" value="" id="UserEmail" class="form-error"> </div> ``` Or here: ``` <div class="error-message">An email is required</div> <div class="input text required error"> <label for="UserEmail">Email</label> <input name="data[User][email]" maxlength="50" type="text" value="" id="UserEmail" class="form-error"> </div> ```