name field validation is not working in cakephp - cakephp

Below is my model function which is already working...
i need to validate name field.
when i submit blank form.....the form display Enter portfolio name.
and when i enter numeric value in name field the form display Enter valid portfolio name.
but when i submit the blank form the form does not display any message for name field.
and when i enter the numeric value in name textbox and submit then it display message properly.
please help me to make both the rule working for name field.
so plz suggest me how to implement this.
<?php
class Portfolio extends AppModel{
var $name = 'Portfolio';
var $validate = array(
'name' => array(
'rule' => 'notEmpty',
'message' => "Enter portfolio name."
),
'name' => array(
'rule' => '/^[a-zA-Z]*$/',
'message' => "Enter valid portfolio name."
),
'job_title' => array(
'rule' => 'notEmpty',
'message' => "Enter your quote request."
),
'freight_mode'=> array(
'rule'=>'notEmpty',
'message'=>"Enter your frieght mode."
),
'expected_transport_growth' => array(
'rule' => 'notEmpty',
'message' => "Select expected transport growth."
),
'current_annual_spend' => array(
'rule' => 'notEmpty',
'message' => "Select current annual spend."
),
'expected_annual_spend' => array(
'rule' => 'notEmpty',
'message' => "Select expected annual spend."
),
'quotes_expiry' => array(
'rule' => 'notEmpty',
'message' => "Enter deadline on quote request."
),
'quotes_required' => array(
'rule' => 'notEmpty',
'message' => "Select quote requrest required."
),
'contract_start_date' => array(
'rule' => 'notEmpty',
'message' => "Enter contract start date."
),
);
/*var $belongsTo = array(
'SupplierUquotes' => array(
'className' => 'SupplierUquotes',
'foreignKey' => 'id'
),);*/
}
?>

Try this
<?php
public $validate = array(
'name' => array(
'notEmpty' => array(
'rule' => 'notEmpty',
'message' => "Enter portfolio name."
),
'valid' => array(
'rule' => '/^[a-zA-Z]*$/',
'message' => "Enter valid portfolio name."
)
)
);
?>

Try this code:
<?php
class Portfolio extends AppModel{
var $name = 'Portfolio';
var $validate = array(
'name' => array( // here 'name' is the field name to be validated
'notEmpty'=>array( // here 'notEmpty' is user defined rule name, it should not be "rule" which is a cakephp reserved word, I think
'rule'=>'notEmpty', // here 'notEmpty' is the actual rule applied on the field 'name'
'message'=>'Enter portfolio name.'
),
'validName'=>array( // here 'validName' is another user-defined rule name. It should be different for a particular field.
'rule'=>'/^[a-zA-Z]*$/',
'message'=>'Enter valid portfolio name.'
)
),
'job_title' => array(
'rule' => 'notEmpty',
'message' => "Enter your quote request."
),
'freight_mode'=> array(
'rule'=>'notEmpty',
'message'=>"Enter your frieght mode."
),
'expected_transport_growth' => array(
'rule' => 'notEmpty',
'message' => "Select expected transport growth."
),
'current_annual_spend' => array(
'rule' => 'notEmpty',
'message' => "Select current annual spend."
),
'expected_annual_spend' => array(
'rule' => 'notEmpty',
'message' => "Select expected annual spend."
),
'quotes_expiry' => array(
'rule' => 'notEmpty',
'message' => "Enter deadline on quote request."
),
'quotes_required' => array(
'rule' => 'notEmpty',
'message' => "Select quote requrest required."
),
'contract_start_date' => array(
'rule' => 'notEmpty',
'message' => "Enter contract start date."
),
);

Related

Cakephp isUnique is not working properly

isUnique validation doesn't work if there there is validation error only of uniqueness in user creating form and gives error of 'The page isn't redirecting properly'.
Model code is:
class User extends AppModel {
public $validate = array(
'username' => array(
'notEmpty' => array(
'rule' => array('email'),
'message' => 'Please supply a valid email address.'
),
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This email has already been taken.'
)
),
'password' => array(
'min' => array(
'rule' => array('minLength', 3),
'message' => 'Password must be at least 3 characters.'
),
'required' => array(
'rule' => 'notEmpty',
'message' => 'Please enter a password.'
),
),
'cpassword' => array(
'required' => 'notEmpty',
'match' => array(
'rule' => 'validatePasswdConfirm',
'message' => 'Passwords do not match'
)
)
);
}
When I enter mismatch passwords then it gives error of 'Passwords do not match' and also gives error 'This email has already been taken.' if a username is already there in users table but the problem is when I Only enter the username which already exists and enter a match password then it goes to a page with error "The page isn't redirecting properly".
How can I fix this issue?
Thanks in advance.
if you have used $this->Auth->login() in beforeFilter for any condition, remove it and use !empty($this->Auth->user('id')) instead of $this->Auth->login() for you conditions in beforeFilter.

How users can sign up with null values in cakephp?

some users can resgister with null values in my cakephp website !!
when i try to register with null value im getting the error messages of my model, but someone do that, i found 8 users with no data (no usernam, no email, no names... !!!!! )
this is my signup (add) action:
if($this->request->is('Post')){
$this->User->create();
$this->request->data['User']['user_id'] = $this->Auth->User('id');
if($this->User->save($this->request->data)){
$iduser=$id=$this->User->getLastInsertId();
$this->Session->setFlash(_('your accoutn has been created, check you inbox.'));
$this->redirect(array('action'=>'login',$iduser));
}
else
$this->Session->setFlash(_('Erreur !'));
}
$this->set('title_for_layout','users sign up');
and this is a part of my users model :
public $validate =array(
'username' => array(
'length' => array(
'rule' => array('minLength', 5),
'message' => 'error !',
'required' => true,
),
'alphanum' => array(
'rule' => 'alphanumeric',
'message' => 'error !',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'error !)',
),
),
'password' => array(
'length' => array(
'rule' => array('minLength', 6),
'message' => 'error !',
'required' => true,
'on' => 'create'
),
'alphanum' => array(
'rule' => 'alphanumeric',
'message' => 'error !',
'on' => 'create'
),
),
'email' => array(
'email' => array(
'rule' => 'email',
'message' => 'error !',
),
'unique' => array(
'rule' => 'isUnique',
'message' => 'error !',
),
),
(other fields are checked too)
so, how someone can signup with no data ??????!
please help !
Are the creating the accounts and then removing the data thereafter?
Is there the facility for the user to edit these details out later on?
You need to specify which fields are empty and post the full validation from the controller.

cakephp form validation displaying default message instead of custom

Instead of my custom validation message being show upon submitting a form, the default "This field cannot be left blank" is being shown. Any ideas?
Here is my validation:
public $validate = array(
'id' => array(
'rule' => 'notEmpty'
),
'first_name' => array(
'rule' => 'notEmpty'
),
'last_name' => array(
'rule' => 'notEmpty'
),
'accused_of' => array(
'rule' => 'notEmpty'
),
'last_4_of_ssn' => array(
'rule' => '/^[0-9]{4}$/', // NEED TO DOUBLE CHECK THIS
'messsage' => 'Exactly 4 digits'
),
'date_of_accusation' => array(
'rule' => array('date', 'ymd'), // NEED TO DOUBLE CHECK THIS
'message' => 'Date must be in YYYY-MM-DD format.'
),
'monitoring' => array(
'rule' => 'notEmpty'
)
);
And here is my form:
echo $this->Form->create('Offender');
echo $this->Form->input('first_name', array('label' => 'First name:'));
echo $this->Form->input('last_name', array('label' => 'Last name:'));
echo $this->Form->input('accused_of', array('label' => 'Accused of:'));
echo $this->Form->input('monitoring', array('label' => 'Monitoring (enter as comma seperated list. ex: BAC, location):'));
echo $this->Form->input('last_4_of_ssn', array('label' => 'Last 4 digits of SSN:') );
echo $this->Form->input('date_of_accusation', array('label' => 'Date of accusation:'));
echo $this->Form->end('Save New Offender');
It appears for some fields you have not set any custom messages please go to this URL http://book.cakephp.org/2.0/en/models/data-validation.html
You can also create a custom validation for this purpose like in validation array do like this
'last_4_of_ssn' => array(
'rule' => array('digitValidate'),
'messsage' => 'Exactly 4 digits'
)
and in Model you can create a public function with the same name.
public function digitValidate() {
$pattern = '/^[0-9]{4}$/';
if (!preg_match($pattern,$this->data['last_4_of_ssn'])) {
return false;
}
return true;
}
I hope this will help you.

Attribute required = false in special cases

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'
)
),

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
}

Resources