所以我理解,如果您根据Model的验证器验证错误消息,CakePHP会自动将错误消息加载到视图中。我正在尝试设置日期保管箱验证,我想控制错误消息显示在哪里。
在提交验证表格之前:
<div class="input text required">
<label for="UserEmail">Email</label>
<input name="data[User][email]" maxlength="50" type="text" id="UserEmail">
</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 class="error-message">An email is required</div>
</div>
是否有可能将它控制在我可以在这里出现的位置?
<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>
或者在这里:
<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>
答案 0 :(得分:5)
在VIEW中:
1)禁用自动错误显示:
echo $this->Form->create('Mymodel', array(
'inputDefaults' => array(
'error' => false
)
));
2)从$this->validationErrors
更新: 我误解了你原来的问题。可以通过create()调用(应用于所有字段)或input()调用(针对单个字段)指定元素的顺序:
echo $this->Form->create('Mymodel', array(
'inputDefaults' => array(
'format' => array('before', 'error', 'label', 'between', 'input', 'after')
)
));
上面会将error
div放在label
div之前。您可以按照自己喜欢的顺序重新排列该数组。
答案 1 :(得分:0)
我能够为 password_confirm 字段获取此信息,也许有帮助
echo $this->Form->input('username');
echo $this->Form->input('password');
echo $this->Form->input('password_confirm', array(
'type' => 'password',
'div'=>'control-group',
'error' => array('attributes' => array('wrap' => 'div','class' => 'help-inline'))
));
echo $this->Form->input('group_id', array(
'options' => array('2' => 'Admin', '3' => 'Publisher')
));
echo $this->Form->input('publisher_id', array(
'options' => $publishers
));
echo $this->Form->input('active');