cakePHP 2.0.6 - validations - cakephp-2.0

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

Related

CakePHP validation with OR condition in Model

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

Relationship between model in cakephp

I have two table user(id,email,username,password) and post(id,user_id,title,body),
i have has many relation in both model,
now when i add post, it shows user id in the add action,it works ok, but i want to see username instead of user_id in the post add action.
how to do it?
this is my post model:
class Post extends AppModel {
public $useTable = 'post';
public $displayField = 'title';
public $validate = array(
'title' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
'body' => array(
'notEmpty' => array(
'rule' => array('notEmpty'),
),
),
);
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
this is my postcontroller add function:
public function add() {
if ($this->request->is('post')) {
$this->Post->create();
if ($this->Post->save($this->request->data)) {
$this->Session->setFlash(__('The post has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The post could not be saved. Please, try again.'));
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}
Try this one:
$users = $this->Post->User->find('list', array('fields' => array('User.username')));
try to update your query, specify your fields:
$users = $this->Post->User->find('list',
array("fields" =>array(
"User.id",
"User.email"
)
)
);

CakePHP Custom Validation Method Not Called With allowEmpty

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));

Cakephp How to validate an array to make sure that it has at least five items or more

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;
}
}
?>

How create belonge assocation in models in cakephp

I use Cakephp framework, and I have problem with my association.
How create belong to association in models in cake php.
When I use belongto and hasMany in my model.
Can I find sample model to view this example?
Simple belongsTo association:
<?php
class Profile extends AppModel {
var $name = 'Profile';
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
?>
Simple hasMany association:
<?php
class User extends AppModel {
var $name = 'User';
var $hasMany = array(
'Comment' => array(
'className' => 'Comment',
'foreignKey' => 'user_id',
'conditions' => array('Comment.status' => '1'),
'order' => 'Comment.created DESC',
'limit' => '5',
'dependent'=> true
)
);
}
?>
More specific information about associations is in the CakePHP Book.
User has Many Photos
Photos belongs to User
In User Model :
var $hasMany = array(
'Photo' => array(
'className' => 'Photo',
'foreignKey' => 'user_id'
);
In Photo Model :
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id',
'conditions' => '',
'fields' => '',
'order' => ''
),
'PhotoAlbum' => array(
'className' => 'PhotoAlbum',
'foreignKey' => 'photo_album_id',
'conditions' => '',
'fields' => '',
'order' => '',
))

Resources