$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
Related
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.
we have tried several solution for validation of password,but none is working but user get login, all validations get working except alphanumeric validation in password.
Here is code:
'password' => array ('required' => array (
'rule' => array ('notEmpty'),
'rule' => array ('between',1,15 ),
//'rule' => array('custom', '[a-zA-Z0-9, ]+'),
'message' => 'A password is required,must be between 8 to 15 characters' )
),
using custom function it doesn't work so we tried
'alphaNumeric' => array(
'rule' => array('alphaNumericDashUnderscore'),
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'password must contain Alphabets and numbers only'
)),
custom function in model
public function alphaNumericDashUnderscore($check) {
$value = array_values($check);
$value = $value[0];
return preg_match('|^[0-9a-zA-Z_-]*$|', $value);
}
we are working on cakephp version 2.4.3
That is because you are defining two times the same key rule in an array. The second one will always overwrite the first one.
As per documentation, you should do it as follow:
public $validate = array(
'password' => array(
'password-1' => array(
'rule' => 'alphaNumeric',
'message' => 'password must contain Alphabets and numbers only',
),
'password-2' => array(
'rule' => 'alphaNumericDashUnderscore',
'message' => 'password must contain Alphabets and numbers only'
)
)
);
I have a cakephp 2.2 app I'm working on that allows users to submit an expenses claim. The expenese claim is made up of many expenese which have an expense code. The relationships are as follows:
ExpenseClaim hasMany Expense
Expense belongsTo ExpenseClaim
Expense hasAndBelongsToMany ExpenseCode
I want to add validation to ensure when the form is completed the expense code must be complete for each expense but for some reason adding a normal validation rule to the expense model doesnt work, obviously I'm missing something.
Heres the validation in the expense model:
class Expense extends AppModel {
/**
* Display field
*
* #var string
*/
public $displayField = 'id';
/**
* Validation rules
*
* #var array
*/
public $validate = array(
'date' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Date cannot be blank',
),
),
'sitename' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Sitename cannot be blank',
),
),
'detail' => array(
'notempty' => array(
'rule' => array('notempty'),
'message' => 'Details cannot be blank',
),
),
'ExpenseCode' => array(
'multiple' => array(
'rule' => array('multiple', array('min' => 1)),
'message' => 'You need to select at least one tag',
),
),
When $this->request->data is submitted via the expenseClaim/add method it looks like this:
/app/Controller/ExpenseClaimsController.php (line 179)
array(
'submit' => 'Save',
'Expense' => array(
(int) 0 => array(
'date' => array(
'day' => '25',
'month' => '03',
'year' => '2013'
),
'sitename' => '123',
'detail' => 'test',
'ExpenseCode' => array(
'ExpenseCode' => ''
),
'billable' => '1',
'amount' => '100',
'miles' => '',
'total' => '100',
'billable_mileage' => '',
'recorded_global_mileage_rate' => '0.4'
)
),
'CashFloat' => array(
'amount' => ''
),
'ExpenseClaim' => array(
'user_id' => '16',
'claim_status_id' => '1',
'date_submitted' => '2013-03-25 18:20:53'
)
)
As you can see the ExpenseCode is empty... How can I ensure that this is not the case?
I managed to solve this by adding the following to my Expense model:
function beforeValidate() {
if (!isset($this->data['ExpenseCode']['ExpenseCode']) || empty($this->data['ExpenseCode']['ExpenseCode'])) {
$this->invalidate('ExpenseCode', 'Type cannot be blank');
}
return true;
}
Hope that helps someone.
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.
Hie guys i need help with my code. I have a form where a student selects subjects, marks and grades they obtained. Subjects and grades are a dropdown menu and they are in a loop. I want a student to enter at least five subjects including english language. each subject is referenced by a subject code. Can you help me do this?
My Controller function is
subject_code' => $this->data['ApplicantOlevelQualification']['subject_code'][$i],
'grade' => $this->data['ApplicantOlevelQualification']['grade'][$i],
My model is as follows
public $validate = array(
'grade' => array(
'notempty' => array(
'rule' => array('notempty'),
// extra keys like on, required, etc. go here...
),
'ruleName2' => array(
'rule' => array('inList', array('A', 'B','C','D','E')),
'message' => 'Not in range.',
),
),
'subject_code' => array(
'numeric' => array(
'rule' => array('numeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
You need to create a custom validation.
Have a look here:
http://book.cakephp.org/1.3/view/1179/Custom-Validation-Rules
An example:
<?php
class User extends AppModel {
var $name = 'User';
var $validate = array(
'promotion_code' => array(
'rule' => array('limitDuplicates', 25),
'message' => 'This code has been used too many times.'
)
);
function limitDuplicates($check, $limit){
//$check will have value: array('promomotion_code' => 'some-value')
//$limit will have value: 25
$existing_promo_count = $this->find( 'count', array('conditions' => $check, 'recursive' => -1) );
return $existing_promo_count < $limit;
}
}
?>