CakePhp isUniquer Apparently Wrong - cakephp

I am trying to use this validation set for username field in my User model:
public $validate = [
'password' => [
'rule-1'=>array(
'rule' => array('minLength','6'),'message'=> 'At least 6 letters',
'allowEmpty' => true),
'rule-3'=>array(
'rule'=> '/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d#!#$%_-]{7,}$/',
'message'=>'Wrong password')
],
'username' => [
'unique' => array(
'rule' => ['isUnique',['username'],false],
'required' => 'create',
'message' => 'Username present!'
)
]
];
But whenever I try to add a new record to my User model the validation fails and it says the username is already present, while it is not.

My fault.
I had a beforeFind() function in AppModel.php which changed the query!
Changing that beforeFind() solve the problem.

Related

Cake php modal validation error

I have a simple registration form and complete model validation.
public function add() {
// Has any form data been POSTed?
if ($this->request->is('post')) {
$this->User->set($this->request->data); //echo '<pre>'; print_r($this->data);exit;
if($this->User->validates()){
if ($this->User->save($this->request->data)) {
// Set a session flash message and redirect.
$this->Session->setFlash('User Saved!');
return $this->redirect('/users');
}
}
}
}
Modal validation code is below
class User extends AppModel {
public $validate = array(
'username' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Alphabets and numbers only'
),
'between' => array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Minimum 8 characters long'
),
'email' => 'email',
'born' => array(
'rule' => 'date',
'message' => 'Enter a valid date',
'allowEmpty' => true
)
);
}
The problem is that when i delete html fields using firebug and submit form it save data with blank entries . i think its a big problem i am facing because it not good practice to save blank entry and i want to stop it by the hacker or any one.
please help.
Add the validation rule 'notEmpty' for all fields which shouldn't be blank entries in the database.
Reference: cookbook
I am agree with semmelbroesel13
please use notEmpty rules as:
'rule' => array('notempty')
Updated:
Please try below code and check whats the query exactly
public function add() {
// Has any form data been POSTed?
if ($this->request->is('post')) {
$this->User->set($this->request->data); //echo '<pre>'; print_r($this->data);exit;
if($this->User->validates()){
if ($this->User->save($this->request->data)) {
$log=$this->User->getDataSource()->getLog(false, false);
echo "<pre>";print_r($log);exit;
// Set a session flash message and redirect.
$this->Session->setFlash('User Saved!');
return $this->redirect('/users');
}
}
}
}

MultivalidatableBehavior with cakephp 2.3.0. Not Working

I am using 'MultivalidatableBehavior' in cakephp. Though it worked fine with previous version. But its not working with Cakephp 2.4.0 version. Is their any changes with this behaviour. Because When I use this code.
var $validationSets = array( 'ruleset mentioned in it. ');
Though my request go through the Behaviour class but its not putting validation on the desired field. I have also checked $this->request->data Its also valid and for same Model where ruleset has been written.
I tried to debug using die in my MultivalidatableBehavior class. Though during valiation my request falls in the function of setValidation(&$model, $rules = array()) {. Please suggest if it is compatible with greater version of cakephp2.3.
My tried code..
Model Code :
var $actsAs = array('Multivalidatable');
var $validationSets = array(
'login' => array(
'username' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Username is required !!')),
'password' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Password is required !!')),
),
);
Controller Call.
$this->User->setValidation('login'); Fields are also with same name.
Its not validating If I put if($this->User->setValidation('login')) it returns false.
Thanks to Piotr Beschel for helping me out. I shouldn't use any sort of behaviour. It can also be achieved without behaviour. My Model code would be
public $validationAdmin = array(
'adminLogin' => array(
'username' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your username !!')),
'password' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your password !!')),
),
'adminForgotPassword' => array(
'email' => array('required' => array('rule' => array('notEmpty'), 'message' => 'Please enter your email address !!'),
'email' => array('rule' => array('email'), 'message' => 'Please enter valid mail address')),
),
);
That is simply creating variable named $valdiationAdmin I have named it as admin to know following rule set is for admin.
My sample controller code would be.
$this->Admin->validate = $this->Admin->validationAdmin['adminLogin'];
Just this way. If form has data to save it would move like butter. In case data is just validating I have to force form to validate it(In case login where data is not saved).
if ($this->Admin->validates()) {
} else {
$errors = $this->Admin->validationErrors;
$this->set('validationErrors', $errors);
}
It would validate my form. Even add error message to form field in ctp file. I need not to write $this->form->error('fieldname');
Thanks again to all the viewer. Suggest me something if more better. I am thinking to validate data of other form if associated with one form, with same implementation. Help if you can.
The method
$this->User->setValidation('login')
only chooses which validation Set will be used.
Use:
if ($this->User->validates()) {
...
to get the result of the validation.

CakePHP one of the validation rules is not applied during edit/update

Here are the 3 validation rule that are applied for "initials" field:
'initials' => array(
'Not empty' => array(
'rule' => 'notEmpty',
'message'=>'Please enter a customer\'s initials'
),
'Unique' => array(
'rule' => array('isUniqueForCompany'),
'message' => 'Customer with these initials already exists'
),
'Long' => array(
'rule' => array('between', 2, 12),
'message' => 'Initials should be between 2 and 12 characters long'
)
)
When creating a new record, all 3 rules are applied, but when updating/editting the 'Unique' rule is skipped out.
What can cause such a problem?
If needed, I can provide the controller actions ar view forms.
I would suggest that you have a problem with your custom isUniqueForCompany validation function in the model.
It's probably returning true when it shouldn't.

CakePHP Required Fields - Not Making Any Sense

I have a form that it seems to me Cake is making fields required or not required so randomly that I can't even control it. Please help me to understand how to get required fields working.
public $validate = array(
'fname' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your first name'
),
'rule2' => array(
'rule' => array('maxLength', '50'),
'message' => 'First name cannot exceed 50 characters'
)
),
'sname' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your last name'
),
'rule2' => array(
'rule' => array('maxLength', '50'),
'message' => 'Last name cannot exceed 50 characters'
),
),
'email' => array(
'rule1' => array(
'rule' => 'notEmpty',
'message' => 'Please enter your email address'
),
'rule2' => array(
'rule' => array('maxLength', 100),
'message' => array('Email cannot exceed 100 characters')
),
'rule3' => array(
'rule' => 'email',
'message' => 'Please enter a valid email address'
),
'rule4' => array(
'rule' => 'isUnique',
'message' => 'An account already exists with that email address',
'on' => 'update'
)
)
);
All these 3 fields cannot be empty, but Cake is deciding to make fname and sname show as required on the form, but email is not required. I'm talking about the class on the inputs making it look required. This is so random. In the database the fields are all identical VARCHAR, NOT NULL.
I tried adding allowEmpty => false to the email but it doesn't do anything. This works on other fields that only have one rule but doesn't work on fields that have multiple rules. Still, I want to know why it is making fname and sname required but not email.
Screenshot: http://i.imgur.com/3JMTR.gif
Hi in the table for email field make it as could not be
NULL
(I think email column can accept null values like that u created the table) then it Required * symbol will come on view. Surely it will work, I face the same issue like this
Try adding 'required'=true on the rules of the fields you want to be required.
You can switch off all of the email validation rules by:
unset($this->ModelName->validate['email']);
But after watching the screenshoot,i can see that there is no red star above email field.The cause of this maybe you didn't properly named the email input
echo $this->Form->input('email');
Normally if you have this line(if it is generated by bake than must be right),it must have the validation rules for email field

Cakephp: Form Vailidation

I'm trying to create a validator for my models:
But taking the example from http://book.cakephp.org/:
var $validate = array(
'country' => array(
'rule' => 'notEmpty'
)
);
gives the following error: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash [CORE/cake/libs/model/app_model.php, line 166]
By googling this error I found a mailinglist entry that recommended using: ( http://cakephp.1045679.n5.nabble.com/validation-notEmpty-td1320629.html)
'country' => array(
'rule' => VALID_NOT_EMPTY,
'message' => 'This field cannot be left blank.'
)
which didn't work. so I tried:
'country' => array(
'rule' => 'VALID_NOT_EMPTY',
'message' => 'This field cannot be left blank.'
)
Which marked the field as a required, but didn't stop me from leaving it blank.
My question is: how to do this correctly? I'm using CakePHP 1.3.6
Most probably you need to put the required key; something like this:
'country' => array(
'rule' => 'notEmpty',
required => true,
'message' => 'This field cannot be left blank.'
)
Hope this helps.
Why donĀ“t you use the commandline for baking your models (cmd: cake bake)? If you bake the models you can specify the validation rules there.
It is fast and easy... and you can see how validation works.
It helped me a lot...
Here an example code.
'username' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Username required',
'allowEmpty' => false,
'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
You must use an array for the rule definition...
in your view file just add 'class'='required'
eg:
<?php echo $this->Form->input('new_email',array('class'=>'email required yellow', 'div'=>false, 'label'=>false));?>

Resources