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));?>
Related
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.
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.
Using CakePHP 1.3 I developed engine for blog with posts and comments tables and recently I have noticed that in database I've got records with null values in content column despite of the fact that Comment model has defined proper validation:
<?php
class Comment extends AppModel {
var $name = 'Comment';
var $sequence = 'comments_seq';
var $belongsTo = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'post_id'
)
);
var $validate = array(
'content' => array(
'required' => array (
'rule' => 'notEmpty',
'message' => 'Content can't be empty.'
)
),
'post_id' => array(
'rule' => 'notEmpty'
),
'created' => array(
'rule' => 'notEmpty'
)
);
?>
Is there a bug in CakePHP framework or validation defined above is not correct or insufficient?
In your validation rules, you're not actually requiring the field. Requiring means that the key must exist when it comes time to validate. The notEmpty rule requires only that the key is not empty but not that it exists.
To require that the field exists, use the required option in your validation rules:
var $validate = array(
'content' => array(
'required' => array ( // here, 'required' is the name of the validation rule
'rule' => 'notEmpty',
'message' => 'Content can\'t be empty.',
'required' => true // here, we say that the field 'content' must
// exist when validating
)
),
'post_id' => array(
'rule' => 'notEmpty'
),
'created' => array(
'rule' => 'notEmpty'
)
);
Without the required key, you could potentially save completely empty records by simply not including the 'content' key when saving. Now that it is required, validation will fail if 'content' is not in the data that you're saving.
New to unit testing... testing an articles controller and I am getting a fail on the $this->assertNotEmpty();
Shouldn't this be displaying an array full of validation errors? Instead I am getting an empty array.
It seems my validation rules are not being picked up... as further inspection show that Article::save() is returning true on data that should fail....
/**
* Admin Add
* #see controllers/MastersController::_admin_add()
* #return void
*/
public function admin_add(){
//parent::_admin_add();
if(!empty($this->request->data){
$this->Article->save($this->request->data);
}
}
/**
* Test Admin Add
*
* #return void
*/
public function testAdminAdd() {
#define sample passing data
$sampleDataPass = array(
'Article'=>array(
'title'=>'Test Article Add Will Pass',
'body'=>'Test Article Add Body',
'status_id'=>1,
'category_id'=>1,
)
);
#test action
$this->testAction('admin/articles/add', array('data'=>$sampleDataPass));
$this->assertEmpty($this->Articles->Article->validationErrors); #####PASSES#####
#define sample failing data
$sampleDataFail = array(
'Article'=>array(
'title'=>'Test Article Add Will Fail',
)
);
$this->testAction('admin/articles/add', array('data'=>$sampleDataFail));
$this->assertNotEmpty($this->Articles->Article->validationErrors); #####FAILS#####
}
class Article extends AppModel {
/*
* Name
*/
public $name = 'Article';
/*
* Validation Rules
*/
public $validate = array(
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'You must supply an article title in order to save.',
),
),
'body' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'You must supply an article title in order to save.',
),
),
'status_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'You must choose a status.',
'allowEmpty' => false,
),
),
'category_id' => array(
'numeric' => array(
'rule' => array('numeric'),
'message' => 'You must choose a category.',
'allowEmpty' => false,
),
)
);
}
CakePHP will ignore validation rules if the field is not present in the data.
By setting the option 'required' to true the validation rule will always be checked.
For example:
'title' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'You must supply an article title in order to save.',
'required' => true
),
),
Documention on validation in CakePHP can be found here: http://book.cakephp.org/2.0/en/models/data-validation.html#one-rule-per-field
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