i am developing a form where a user can be filled either mobile no. OR telephone no. or both. so i want to add validation to do this.
cakephp version : 2.5
'mobile' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message'=> 'Please enter mobile'
)
'telephone' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message'=> 'Please enter telephone'
)
i want to combination of both with OR condition in Model.
First Get the Conditional Behaviour from here:
Conditional Validation Behavior
Add Behavior to your model:
class Something extends AppModel
{
public $actsAs = array('ConditionalValidation');
}
Defining your validation rule by Model:
class Something extends AppModel
{
public $actsAs = array('ConditionalValidation');
public $validate = array(
'telephone' => array('isActive' => array(
'rule' => 'notEmpty',
'if' => array('mobile',''),//activate when you don't have mobile
),
'mobile' => array('isActive' => array(
'rule' => 'notEmpty',
'if' => array('telephone',''),//activate when you don't have mobile
),
);
);
}
Have to find out whats the rule for both fields exist
Related
I have a User model with a lot of attributes and validation rules:
class User extends AppModel {
public $hasMany = 'Serviceorder';
public $validate = array(
'name' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A name is required'
)
),
...
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required'
)
),
...
);
}
This makes sense to me because when creating a user a password is always required.
In some cases, like when editing a user, I don't want the password to be required. When there's no new password, the password is not changed. How can I do that?
change the password validation as below :-
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'A password is required',
'on' => 'create'
)
),
<?php
App::uses('AppModel', 'Model');
class Announcement extends AppModel {
public $validate = array(
'id' => array(
'notempty' => array(
'rule' => array('notempty'),
),
'numeric' => array(
'rule' => array('numeric'),
),
),
'enabled' => array(
'numeric' => array(
'rule' => array('numeric'),
),
'boolean' => array(
'rule' => array('boolean'),
),
),
'firstPageEnterDate' => array(
'datetime' => array(
'rule' => 'compareDates',
'message' => 'attention to data interval',
),
),
'firstPageLeaveDate' => array(
'datetime' => array(
'rule' => array('datetime'),
),
'notempty' => array(
'rule' => array('notempty'),
),
)
);
public function compareDates() {
if ($this->data[$this->alias]['enabled'] == 1) {
return $this->data[$this->alias]['firstPageEnterDate'] < $this->data[$this->alias]['firstPageLeaveDate'];
}
}
}
The problem is:
It displays the validation message set on firstPageEnterDate, regardless, if
$this->data[$this->alias]['enabled'] == 1 or not.
Please note that this condition:
$this->data[$this->alias]['enabled'] == 1 is not always true. But even when it's false, so the contents inside don't run, still the message appears.
So, it seems that, if Cake sees a rule enabled and a message, regardless what is inside rule, it triggers the validation message associated!
Any clue why?
'firstPageEnterDate' => array(
'datetime' => array(
'rule' => 'compareDates',
'message' => 'attention to data interval',
),
Return true in compareDates() if it is valid -- or if you don't want to check it because that counts as valid.
public function compareDates() {
if ($this->data[$this->alias]['enabled'] != 1) return true; // we don't want to check
return $this->data[$this->alias]['firstPageEnterDate'] < $this->data[$this->alias]['firstPageLeaveDate'];
}
You can also check in beforeValidate() callback the value of enabled and unset that validation rule from there.
I have a model that has a first name, last name, and organization/company name field. The user must enter either a first name and a last name OR an organization name.
The issue is that my custom validation method ("validateNames") is never called. For debugging purposes, I have a "die" statement there, rather than real validation logic -- but the die statement is never reached.
My model looks like:
class Contact extends AppModel {
public $validate = array(
'first_name' => array(
'rule' => 'validateNames',
'allowEmpty' => true,
'required' => false
),
'last_name' => array(
'rule' => 'validateNames',
'allowEmpty' => true,
'required' => false
),
'organization' => array(
'rule' => 'validateNames',
'allowEmpty' => true,
'required' => false
)
);
public function validateNames($check) {
die('here');
}
}
The problem is that as long as I have 'allowEmpty' in the validation rules, my custom validation method is never called (and the 'die' statement is never reached). But if I remove 'allowEmpty', then an HTML "required" attribute is added to each INPUT field (even though I have 'required' => false) -- this prevents the form from being submitted unless all three fields are filled in, when only one (organization) or two (first and last names) are actually required.
You must have to pass in array if you want to call 2 or more validation with same fields
like
class Contact extends AppModel {
public $validate = array(
'first_name' => array(
'rule1' => array(
'rule' => 'validateNames',
'message' => 'Must be a valid first name',
'allowEmpty' => true
),
),
'last_name' => array(
'rule1' => array(
'rule' => 'validateNames',
'message' => 'Must be a valid names',
'allowEmpty' => true
),
'organization' => array(
'rule' => 'validateNames',
'allowEmpty' => true,
'required' => false
)
);
public function validateNames($check) {
die('here');
}
}
let me know if i can help you more.
Remove allowEmpty option from the validation rules and disable required option when outputting the field in your view. Try this:
Model:
class Contact extends AppModel {
public $validate = array(
'first_name' => array(
'rule' => 'validateNames'
),
'last_name' => array(
'rule' => 'validateNames'
),
'organization' => array(
'rule' => 'validateNames'
)
);
public function validateNames($check) {
die('here');
}
}
View:
echo $this->Form->input('first_name', array('required' => false));
echo $this->Form->input('last_name', array('required' => false));
echo $this->Form->input('organization', array('required' => false));
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;
}
}
?>
I am using cakePHP 2.0.6 and I am trying to add validation for fields like name, title shoudl not be blank. I placed logic inside model class.
<?php
class Post extends AppModel {
public $name = 'Post';
public $validate = array(
'name' => array(
'required' => true
),
'title' => array(
'required' => true
)
);
}
but somehow it is not working, Can I have some help please?
Thanks
you need to add some more attributes like
<?php
class Post extends AppModel {
public $name = 'Post';
public $validate = array(
'name' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Name Should not be blank'
),
'title' => array(
'rule' => 'notEmpty',
'required' => true,
'message' => 'Title Should not be blank'
)
);
}
Please let me know if this isn't work