在我的 CakePHP 项目中,我想显示错误消息,但它不起作用。
当我提交时,我希望它显示一条错误消息,例如:“此字段是必需的”,但默认情况下它显示“请填写此字段”。我改变了信息,但它并没有从核心改变。
我的视图、模型和控制器代码如下:
add_district.ctp
:
<div class="pg_title txtLeft">Add District</div>
<?php echo $this->Form->create('Admins', array('action' => 'add_district'));?>
<table>
<tbody>
<tr>
<td><label>District Name<span class="red required">*</span></label></td>
<td><?php echo $this->Form->input('District.district_name',array('label'=>false,'div'=>false,'size'=>50,'style'=>'width:330px;')); ?></td>
</tr>
<tr>
<td colspan="2" align="right" style="padding-right: 113px;"><input type="reset" value="Reset"> | <?php echo $this->Form->submit('ADD', array('div' => false));?></td>
</tr>
</tbody>
</table>
<?php print $this->Form->end();?>
<div class="clear"></div>
模型 :District.php
<?php
App::uses('AppModel', 'Model');
/**
* Admin Login Model
*
*/
class District extends AppModel
{
public $name='District';
public $usetables='districts';
public $validate = array(
'district_name' => array(
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'This field is required'));
}
?>
我的控制器代码是(AdminController.php
):
public function add_district()
{
$this->layout='common';
$this->District->create();
$this->District->set($this->data);
if(empty($this->data) == false)
{
if($this->District->save($this->data))
{
$this->Session->setFlash('District Added Successfully.');
$this->redirect('add_district');
}
}
else
{
$this->set('errors', $this->District->invalidFields());
}
}