How to change CakePHP validation error message - cakephp

I have read the CakePHP documentation about validation rules, but I'm still stuck with changing the error message on an email field.
I currently have this Validation rule in my model:
public $validate = array(
'emailadres' => array(
'rule' => 'email',
'required' => true,
'allowEmpty' => false,
'message' => 'My custom error message'
)
);
The field shows up as being mandatory, but a standard error message appears instead of my custom message.
Does anybody see what I'm doing wrong?
My CakePHP version is 2.3.7

You might want to double check the documentation: http://book.cakephp.org/2.0/en/models/data-validation.html#one-rule-per-field
Its not 'ruleName' => 'email',, but 'rule' => 'email',.
You can also try the verbose representation:
public $validate = array(
'emailadres' => array(
'email' => array(
'rule' => 'email',
'required' => true,
'allowEmpty' => false,
'message' => 'My custom error message'
)
)
);

Related

debug Auth Login returning true but login failed

I am working on a simple log in application. Whenever I log in it prints the error message: Invalid Username or password.
I tried debug($this->Auth->login()) but it always returns true and despite this log in gets failed.
Another problem is if I put $this->request->data inside login(),it logs in even with wrong password.
Following is the code.
AppController.php
App::uses('Controller', 'Controller');
class AppController extends Controller
{
public $components = array(
'Session',
'Auth' => array('loginAction'=>array('controller'=>'students','action'=>'login'),
'loginRedirect' => array('controller' => 'students','action' => 'add'),
'logoutRedirect' => array('controller' => 'students','action' => 'login'),
'authError'=> 'You must be logged in to view this page!',
'loginError'=> 'Galat Username or Password !',
'authenticate' => array('Form' => array('userModel' => 'Student','fields' => array('username' => 'email','password'=>'password')),
'authorize'=>'Controller'
)
);
public function beforeFilter() {
$this->Auth->userModel='Student';
$this->Auth->allow('login','logout');
}
}
StudentsController.php is:
<?php
App::uses('AppController', 'Controller');
class StudentsController extends AppController {
public $helpers = array('Session', 'Number', 'Text', 'Time');
public $components = array('Paginator', 'Auth', 'Flash', 'Security', 'Session');
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('login','add','index');
}
public function login()
{
//if already logged-in, redirect
/*if($this->Session->check('Auth.Student'))
{
$this->redirect(array('action'=>'index'));
}*/
//if we get the post information,try to authenticate
if($this->request->is('post'))
{
if(debug($this->Auth->login()))
{
$this->Session->setFlash(__('Welcome, '.$this->Auth->user('name')));
/*$this->redirect($this->Auth->redirectUrl());*/
}
else
{
$this->Session->setFlash(__('Invalid username or password from student controller!'));
}
}
}
Login view is:
<div class="users form" >
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Student'); ?>
<fieldset >
<legend>
<?php echo __('Please enter your email and password.'); ?>
</legend>
<?php echo $this->Form->input('email');
echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
<?php
echo $this->Html->link("Register",array('action'=>'add'));
echo"\r\n";
echo $this->Html->link("Forgot Password",array('action'=>'add'));
?>
Student.php
<?php
App::uses('AppModel', 'Model');
/**
* Student Model
*
*/
class Student extends AppModel {
/**
* Primary key field
*
* #var string
*/
public $primaryKey = 'enroll_no';
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'enroll_no' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter your enrollment number',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter your full name.',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'course' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Please enter your course e.g. B.Tech MCA',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'majors' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'year_of_admission' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'email' => array(
'email' => array(
'rule' => array('email'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'password' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'phone_no' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, // Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'guardian_name' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, //Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
'guardian_phone_no' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
//'message' => 'Your custom message here',
'allowEmpty' => false,
'required' => true,
'last' => false, //Stop validation after this rule
'on' => 'create', // Limit validation to 'create' or 'update' operations
),
)
);
public function beforeSave($options = array()) {
/* password hashing */
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}
}
I tried debug($this->Auth->login()) but it always returns true and
despite this log in gets failed. Another problem is if I put
$this->request->data inside login(),it logs in even with wrong
password. Following is the code. AppController.php
Guys, start using the documentation and API documentation... On that page is a big red box telling you that...
In 2.x $this->Auth->login($this->request->data) will log the user in
with whatever data is posted, whereas in 1.3
$this->Auth->login($this->data) would try to identify the user first
and only log in when successful.

Cakephp image -Can not determine the mimetype

cakephp 2.3
I'm uploading an image and I have an error saying that:
Can not determine the mimetype.
Error: An Internal Error Has Occurred.
On my Model this is a part of my $validation
'file_name' => array(
'uploadError' => array(
'rule' =>'uploadError',
'message' => 'Your image upload failed',
'allowEmpty' => FALSE,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
'mimeType' => array(
'rule' => array('mimeType', array('image/gif', 'image/png', 'image/jpg', 'image/jpeg')),
'message' => 'Please only upload images (gif, png, jpg).',
'allowEmpty' => FALSE,
),
'fileSize' => array(
'rule' => array('fileSize', '<=', '2MB'),
'message' => 'Your image must be less than 2MB or(2048ko).',
'allowEmpty' => FALSE,
),
'processCoverUpload' => array(
'rule' => 'processCoverUpload',
'message' => 'Unable to process cover image upload.',
'allowEmpty' => FALSE,
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'This file name is already exist in your folder',
'required' => 'create',
'allowEmpty' => FALSE,
),
),
I'm only allow 3 types of mimetype. any help?
I just ran into exactly the same problem. Thanx to some other comments which pointed me in the right direction, here is my solution:
Edit php.ini (\xampp\php\php.ini on Win7) search for extension=php_fileinfo.dll and uncomment it.
FYI: I'm running xampp 1.7.7 [Apache:2.2.21; PHP:5.3.8; MySQL:5.5.16]. Hopefully on newer xampp versions the extension is enabled by default.

CakePHP Custom Validation Method Not Called With allowEmpty

I have a model that has a first name, last name, and organization/company name field. The user must enter either a first name and a last name OR an organization name.
The issue is that my custom validation method ("validateNames") is never called. For debugging purposes, I have a "die" statement there, rather than real validation logic -- but the die statement is never reached.
My model looks like:
class Contact extends AppModel {
public $validate = array(
'first_name' => array(
'rule' => 'validateNames',
'allowEmpty' => true,
'required' => false
),
'last_name' => array(
'rule' => 'validateNames',
'allowEmpty' => true,
'required' => false
),
'organization' => array(
'rule' => 'validateNames',
'allowEmpty' => true,
'required' => false
)
);
public function validateNames($check) {
die('here');
}
}
The problem is that as long as I have 'allowEmpty' in the validation rules, my custom validation method is never called (and the 'die' statement is never reached). But if I remove 'allowEmpty', then an HTML "required" attribute is added to each INPUT field (even though I have 'required' => false) -- this prevents the form from being submitted unless all three fields are filled in, when only one (organization) or two (first and last names) are actually required.
You must have to pass in array if you want to call 2 or more validation with same fields
like
class Contact extends AppModel {
public $validate = array(
'first_name' => array(
'rule1' => array(
'rule' => 'validateNames',
'message' => 'Must be a valid first name',
'allowEmpty' => true
),
),
'last_name' => array(
'rule1' => array(
'rule' => 'validateNames',
'message' => 'Must be a valid names',
'allowEmpty' => true
),
'organization' => array(
'rule' => 'validateNames',
'allowEmpty' => true,
'required' => false
)
);
public function validateNames($check) {
die('here');
}
}
let me know if i can help you more.
Remove allowEmpty option from the validation rules and disable required option when outputting the field in your view. Try this:
Model:
class Contact extends AppModel {
public $validate = array(
'first_name' => array(
'rule' => 'validateNames'
),
'last_name' => array(
'rule' => 'validateNames'
),
'organization' => array(
'rule' => 'validateNames'
)
);
public function validateNames($check) {
die('here');
}
}
View:
echo $this->Form->input('first_name', array('required' => false));
echo $this->Form->input('last_name', array('required' => false));
echo $this->Form->input('organization', array('required' => false));

CakePHP. Do not works "on => create"

I set up rule 'isUnique' and set 'create' value for 'on' option.
Complete code:
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Empty field',
'required' => true,
),
'between' => array(
'rule' => array('between',3,25),
'message' => 'Too long value'
),
'unique' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'Already taken',
'required' => true,
)
)
But when I'm trying to log in, I get the error message. Why?
CakePHP 2.2.1
The reason is because you have 'required' => true set, which requires the key to be present when validating. This conflicts with the 'on' key, as explained here: http://book.cakephp.org/2.0/en/models/data-validation.html#one-rule-per-field
Instead, make it required only when creating.
'unique' => array(
'rule' => 'isUnique',
'on' => 'create',
'message' => 'Такой никнейм уже занят',
'required' => 'create', // change this to 'create'
)
The problem is, that when you are trying to log in, CakePHP thinks that you are creating a new record (CakePHP identifies it as new record because you don't provide ID of the item like you do when you are editing some item).
The solution could be to create a custom validation function which would check for unique username all the time except when logging in.

Login form validation on CakePHP app

I have added the following validation to my User model:
class User extends AppModel
{
var $useTable = 'users';
var $validate = array(
'username' => array(
'rule'=>array('minLength', 1),
'message'=>'Please enter your username' ),
'password' => array(
'rule'=>array('minLength', 1),
'message'=>'Please enter your password' )
);
}
The idea is that if a user doesn't fill out the fields on the login form then it will show the messages above. However it doesn't work???
What you should actually use is the 'notEmpty', set 'allowEmpty' to false and ensure it is 'required'
var $validate = array(
'username' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a username'
)
'password' => array(
'rule' => 'notEmpty',
'required' => true,
'allowEmpty' => false,
'message' => 'Please enter a password'
)
);
Update:
Bit of a hack, but change the name of your login form to 'UserLogin' - then in your before filter check if $this->data['UserLogin'] is set. If it is - check if $this->data['UserLogin']['username'] is empty, if so just set a flash saying its empty. Same goes for password.

Resources