CakePHP. Do not works "on => create" - cakephp

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.

Related

How users can sign up with null values in cakephp?

some users can resgister with null values in my cakephp website !!
when i try to register with null value im getting the error messages of my model, but someone do that, i found 8 users with no data (no usernam, no email, no names... !!!!! )
this is my signup (add) action:
if($this->request->is('Post')){
$this->User->create();
$this->request->data['User']['user_id'] = $this->Auth->User('id');
if($this->User->save($this->request->data)){
$iduser=$id=$this->User->getLastInsertId();
$this->Session->setFlash(_('your accoutn has been created, check you inbox.'));
$this->redirect(array('action'=>'login',$iduser));
}
else
$this->Session->setFlash(_('Erreur !'));
}
$this->set('title_for_layout','users sign up');
and this is a part of my users model :
public $validate =array(
'username' => array(
'length' => array(
'rule' => array('minLength', 5),
'message' => 'error !',
'required' => true,
),
'alphanum' => array(
'rule' => 'alphanumeric',
'message' => 'error !',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'error !)',
),
),
'password' => array(
'length' => array(
'rule' => array('minLength', 6),
'message' => 'error !',
'required' => true,
'on' => 'create'
),
'alphanum' => array(
'rule' => 'alphanumeric',
'message' => 'error !',
'on' => 'create'
),
),
'email' => array(
'email' => array(
'rule' => 'email',
'message' => 'error !',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'error !',
),
),
(other fields are checked too)
so, how someone can signup with no data ??????!
please help !
Are the creating the accounts and then removing the data thereafter?
Is there the facility for the user to edit these details out later on?
You need to specify which fields are empty and post the full validation from the controller.

cakephp validation rule for time

I want to add validation rule for time along with notempty rule, my code is
$this->validate['StartTime'] = array(
'notempty' => array(
'rule' => array('notEmpty'),
'allowEmpty' => false,
'message' => __('err_required', array(__('lbl_StartTime', true))),
),
'time' => array(
'rule' => array('???', '???'),
'allowEmpty' => false,
'message' => __('err__invaliddate', array(__('lbl_StartTime', true))),
),
);
Please reply soon if there is possible solution for this.
Use time rule or if You need to validate time with seconds write your own custom rule.

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.

Controller validation in Cakephp

I wish to validate in controller in cakephp. Though my validations are working well in Models but instead of model I wish to validate it in controller as well.
What I did to validate in contrller.
$validates = array('email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A email is required'
),
'isUnique' => array(
'rule' => array('notEmpty'),
'message' => 'This email is already registered'
),
'email' => array(
'rule' => array('email'),
'message' => 'Enter valid mail address'
)
));
if ($this->User->validates($validates)) {
die("Action can be performed as validated !! Fields are correct");
} else {
die("Action can't be performed !! Fields are in-correct");
}
It always end me in correct condition no matters if field is correct or not. Please help
Setting $this->Model->validates = $validates; will work for you as suggested in the previous answer but you risk overwriting all other validation rules which may be set in the Model. It's much better to add, modify and remove validation rules on the fly like such:
$this->Model->validator()
->add('email', 'required', array(
'rule' => array('notEmpty'),
'message' => 'A email is required'
))
->add('email', 'isUnique', array(
'rule' => array('notEmpty'),
'message' => 'This email is already registered'
))
->add('email', 'email', array(
'rule' => array('email'),
'message' => 'Enter valid mail address'
));
I left your array exactly as you presented it, however I assume you have the wrong rule on isUnique
You can read more about binding rules here: http://book.cakephp.org/2.0/en/models/data-validation.html#dynamically-change-validation-rules
Try this -
$data = $this->request->data;
$this->ModelName->set($data);
if ($this->ModelName->validates()) {
// it validated logic
} else {
// didn't validate logic
$errors = $this->ModelName->validationErrors;
}
Suppose you want to validate a particular field in cakephp Controller, then for that below code will be use -
$this->ModelName->validationErrors['html_field_name'][] = 'Your Error Message goes here';
Edit your code:
$this->$Model->validate = array('email' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A email is required'
),
'isUnique' => array(
'rule' => array('notEmpty'),
'message' => 'This email is already registered'
),
'email' => array(
'rule' => array('email'),
'message' => 'Enter valid mail address'
)
));
It work with me :)

cakephp Custom validation rule message

I have a custom validation rule to check if two passwords entered are the same, and if they arent I wish to have a message that says "Passwords do not match".
The rule works, however, when the passwords don't match it simply displays the normal error message, what's going on?
var $validate=array(
'passwd2' => array('rule' => 'alphanumeric',
'rule' => 'confirmPassword',
'required' => true,
'allowEmpty'=>false));
function confirmPassword($data)
{
$valid = false;
if ( Security::hash(Configure::read('Security.salt') .$data['passwd2']) == $this->data['User']['passwd'])
{
$valid = true;
$this->invalidate('passwd2', 'Passwords do not match');
}
return $valid;
}
It says "This field cannot be left blank"
EDIT:
The strange thing is, if I leave one of the password fields blank, both error messages say "This field cannot be left blank"
However, if I put something in both, then it correctly says "Passwords do not match"
I think you made it too complex. Here is how I do it:
// In the model
public $validate = array(
'password' => array(
'minLength' => array(
'rule' => array('minLength', '8')
),
'notEmpty' => array(
'rule' => 'notEmpty',
'required' => true
)
),
'confirm_password' => array(
'minLength' => array(
'rule' => array('minLength', '8'),
'required' => true
),
'notEmpty' => array(
'rule' => 'notEmpty'
),
'comparePasswords' => array(
'rule' => 'comparePasswords' // Protected function below
),
)
);
protected function comparePasswords($field = null){
return (Security::hash($field['confirm_password'], null, true) === $this->data['User']['password']);
}
// In the view
echo $form->input('confirm_password', array(
'label' => __('Password', true),
'type' => 'password',
'error' => array(
'comparePasswords' => __('Typed passwords did not match.', true),
'minLength' => __('The password should be at least 8 characters long.', true),
'notEmpty' => __('The password must not be empty.', true)
)
));
echo $form->input('password', array(
'label' => __('Repeat Password', true)
));
You should use the 'message' key in your $validate array to specify the message:
'message' => 'Your passwords do not match'
Further reading: http://book.cakephp.org/view/1143/Data-Validation
And then you can access the fields and the messages by $this->modelName->invalidFields(), which will return you the fields that didn't pass the validation and the message that you have setted for them...
In the controller I mean...
http://book.cakephp.org/view/1182/Validating-Data-from-the-Controller

Resources