cakephp validation rule for time - cakephp

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.

Related

Cake php validation for percentage,Not accept numeric characters and Spacial characters

I want cake php validation for tax percentage numeric field,In that field accept only numeric character like 5.00.Not greater than 100 and not accept spacial character.I try some pattern but it will not work on spacial character like,(!#$%^).My demo code is
$this->validate['TaxPercent'] = array(
'pattern' => array(
'rule' => '/^[\0-9]+$/',
'allowEmpty' => true,
'message' => __('err__numberfield', array(__('lbl_TaxPercent', true))),
),
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => __('err_required', array(__('lbl_TaxPercent', true))),
),
'decimal' => array(
'rule' => array('decimal', 2),
'allowEmpty' => true,
'message' => __("lbl_TaxPercentValid", array(__('lbl_TaxPercent', true))),
),
'range' => array(
'rule' => array('range', 0, 101),
'allowEmpty' => true,
'message' => __("err__percentage", array(__('lbl_TaxPercent', true))),
)
);
So please suggest me appropriate solution.
UPDATED AND WORKING
$this->validate['TaxPercent'] = array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => __('err_required', array(__('lbl_TaxPercent', true))),
),
'decimal' => array(
'rule' => array("decimal", "2", '/^[0-9]+(\.[0-9]{1,2})?$/') //this is working
'allowEmpty' => true,
'message' => __("lbl_TaxPercentValid", array(__('lbl_TaxPercent', true))),
),
'range' => array(
'rule' => array('range', 0, 100.01), //updated this one to so its only until 100.00
'allowEmpty' => true,
'message' => __("err__percentage", array(__('lbl_TaxPercent', true))),
)
);

How to check Combination validation on field in cakephp

I want to check two field combination validation for duplicate values. I have two fields name and area group.
$this->validate['Name'] = array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => __('err_required', array(__('lbl_Name', true))),
),
'Name' => array(
'rule' => array('uniqueClick', 'GroupID'),
'message' => __(__('lbl_Combination', true)),
)
);
$this->validate['GroupID'] = array(
'notempty' => array(
'rule' => array('notEmpty'),
'allowEmpty' => true,
'message' => __('err_required', array(__('lbl_GroupID', true))),
)
);
public function uniqueClick ($ip)
{
$count = $this->find('count', array(
'conditions' => array(
'Name' => $ip,
'GroupID' => $this->data[$this->alias]['GroupID'])
));
return $count == 0;
}
By this code it check combination in both add and update case ,i want to check combination in both case but by this code it check in edit case always after add. so please give me appropriate solution. reply fast.
Create a custom method use it in either in name or group and pass the value of name/group to custom function and and place the check in function:
$this->validate['Name'] = array(
'notEmpty' => array(
'rule' => array('notEmpty'),
'message' => __('err_required', array(__('lbl_Name', true))),
)
);
$this->validate['GroupID'] = array(
'notempty' => array(
'rule' => array('notEmpty'),
'message' => __('err_required', array(__('lbl_GroupID', true))),
),
'duplicate' => array(
'rule' => array('isDuplicate', $this->data['ModelName']['name']),
'message' => __('err_required', array(__('lbl_GroupID', true))),
)
);
public function isDuplicate($data, $name){
// check here
}

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. 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.

How do I validate the value in a multidimensional array?

$year = $this->Form->input('exp_year', array(
'type' => 'date',
'maxYear' => date('Y', strtotime('+ 7 years')),
'minYear' => date('Y'),
'dateFormat' => 'Y',
'empty' => '----',
'label' => false
)
));
So I am trying to get the expiration year, and this is posting as:
'exp_year' => array(
'year' => '2016'
),
I tried, and didn't see anything in the documentation for such a situation.
'exp_year' => array( 'year' => array(
'required' => array(
'rule' => array('numeric'),
'message' => 'Must select an expiration year'
)
)
),
'exp_year' => array( array(
'required' => array(
'rule' => array('numeric'),
'message' => 'Must select an expiration year'
)
)
),
It doesn't look like cake's built-in data validation rules currently handle the "Y" dateFormat. (list of built-in validation rules)
And even if it did, there's no easy way to specify dynamic values in class properties so you wouldn't be able to use date("Y") in the Model $validate declaration, you'd have use constants which are specified elsewhere in your app... bottom line: inelegant and difficult to maintain.
So easiest way is to use a custom validation function, which are very simple to implement: Adding your own Validation Methods

Resources