In my CakePHP project, I want to display error messages but it's not working.
When I'm submitting, I want it to display an error message like: "This field is required", but by default it's showing "Please fill out this field". I changed the message, but it's not changing from the core.
My View, Model, and Controller codes are as following:
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>
Model : 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'));
}
?>
And my controller Code is (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());
}
}