Related
I have searched many topic in here but I can't solve my problem. Please check this for me.
I made the register page and when I made for password field...
I had users_controller.php like:
class UsersController extends AppController
{
var $name = "Users";
var $helpers = array('Paginator','Html');
var $paginate = array();
//Doi tuong component de thuc thi thao tac login
public $components = array
(
'Auth' => array
(
'authorize' => 'controller',
'loginRedirect' => array
(
'admin' => FALSE,
'controller' => 'users',
'action' => 'dashboard'
),
'loginError' => 'Invalid account',
'authError' => 'You don\'t have permission'
),
'Session'
);
//Ham loc cac user truoc khi truy cap trang
public function beforeFilter()
{
parent::beforeFilter();
$this->Auth->allow('add');
$this->Auth->allow('viewuserall');
}
//Ham them moi user
public function add()
{
$this->layout = 'TDCake';
$this->User->set($this->data);
if($this->User->valid_user() == TRUE)
{
if(!empty($this->data))
{
$this->User->create();
if($this->User->save($this->data))
{
$this->Session->setFlash('User has been created!');
$this->redirect(array('action'=>'login'));
}
else
{
$this->Session->setFlash('Please correct the errors');
}
};
}
else
{
$this->Session->setFlash("Your data is NOT available");
}
}
//Ham login cho user
public function login()
{
$this->layout = 'TDCake';
if
(
!empty($this->data) &&
!empty($this->Auth->data['User']['username'])&&
!empty($this->Auth->data['User']['password'])
)
{
$user = $this->User->find
(
'first',array
(
'conditions'=>array
(
'User.email'=>$this->Auth->data['User']['username'],
'User.password'=>$this->Auth->data['User']['password']
),
'recursive' => -1
)
);
if(!empty($user) && $this->Auth->login($user))
{
if($this->Auth->autoRedirect)
{
$this->redirect($this->Auth->redirect());
}
}
else
{
$this->Session->setFlash
(
$this->Auth->loginError,
$this->Auth->flashElement,
array(),'auth'
);
}
}
}
//Ham logout cho user
public function logout()
{
$this->redirect($this->Auth->logout());
}
//Ham gi cha biet, de do tinh sau =))
public function dashboard()
{
$this->layout = 'TDCake';
}
//Ham view cac user khong dieu kien trong table users
function viewuserall()
{
$this->layout = 'TDCake';
$this->paginate=array
(
'limit' => 10,
'order' => array('id' => 'asc'),
);
$data = $this->paginate("User");
$this->set("data",$data);
}
}
User.php in Model is:
class User extends AppModel
{
var $name = "User";
var $validate = array();
function validate_passwords()
{
if($this->data[$this->alias]['pass'] == $this->data[$this->alias]['rpass'])
{
return $this->data[$this->alias]['pass'] = $this->data['User']['password'];
}
else return FALSE;
}
function valid_user()
{
$this->validate = array
(
//Kiem tra username truoc khi add
'username' => array
(
'rule01_notEmpty' => array
(
'rule' => 'notEmpty',
'message' => 'You must enter your Username !'
),
'rule02_max16' => array
(
'rule' => array('maxLength', 20),
'message' => 'Your Username must be less than 20 chars !'
),
'rule03_exists' => array
(
'rule' => 'isUnique',
'message' => 'Your Username have already existed !'
)
),
//Kiem tra email truoc khi add
'email' => array
(
'rule01_notEmpty' => array
(
'rule' => 'notEmpty',
'message' => 'You must enter your Email !'
),
'rule02_exists' => array
(
'rule' => 'isUnique',
'message' => 'Your Email have already existed !'
),
'rule03_emailtype' => array
(
'rule' => 'email',
'message' => 'You didn\'t type a email !'
)
),
//Kiem tra password truoc khi add
'pass' => array
(
'length' => array
(
'rule' => array('between', 6, 20),
'message' => 'Your password must be between 8 and 40 characters.',
),
),
'rpass' => array
(
'length' => array
(
'rule' => array('between', 6, 20),
'message' => 'Your password must be between 8 and 40 characters.',
),
'compare' => array
(
'rule' => 'validate_passwords',
'message' => 'The passwords you entered do not match.',
)
)
);//End this->validate=array
if($this->validates($this->validate==TRUE))
{
return TRUE;
}
else
{
return FALSE;
}
}//End function valid_user
}
add.ctp is
echo $this->Session->flash('auth');
echo $this->Form->create();
echo $this->Form->input('username', array('label' => ('Username')));
echo $this->Form->input('email', array('label' => ('Email')));
echo $this->Form->input('pass', array('label' => ('Password'),'type' => 'password', 'value' => ''));
echo $this->Form->input('rpass', array('label' => ('Repeat Password'), 'type' => 'password', 'value' => ''));
echo $this->Form->input('firstname', array('label' =>('Firstname')));
echo $this->Form->input('lastname', array('label' =>('Lastname')));
echo $this->Form->input('dob', array('label' =>('DOB'),'type' => 'date'));
echo $this->Form->end('Register');
Explanation:
So, in this case, I can validate 2 Password Fields (empty, not equal,...), but it can't insert to the database. That mean it INSERTED current data into the DB but password column in DB is EMPTY. In database, my password column name "password" also.
In another case, I change the name "pass" into "password" for the
echo $this->Form->input('pass', array(
Of course, I have changed any place related to...
and in that case, it can be inserted the password but can not validate anything.
I am too confused about this...I don't know what I am wrong is....can anybody help me.
I am not sure why you are doing an assignment in your validate function:
return $this->data[$this->alias]['pass'] = $this->data['User']['password'];
And even if you were doing an assignment, it should be:
return $this->data['User']['password'] = $this->data[$this->alias]['pass'];
Realize that the field "password" is getting the value from $this->data which has the information, not the other way around.
Also. It would be better (in terms of clarity), to break this code in two lines.
$this->data['User']['password'] = $this->data[$this->alias]['pass'];
return $this->data['User']['password'];
You should name your field the exact name "password" if that's what it is called in the database AND if you are not explicitly assigning it.
Your add function is not doing the above, and further more, as a best practice, you should be hashing the password.
See the CakePHP book on tutorials and examples.
Take some time to go through it with all the snippets and recommendations. And don't forget the standards. :)
I have a site developed in cakephp.
I have a model called User like this:
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(
'not_empty' => array(
'rule'=> 'notEmpty',
'message'=> 'Username not empty'
)
),
'email' => array(
'email_invalid' => array(
'rule' => 'email',
'message' => 'Invalid mail'
),
'email_unique' => array(
'rule' => 'isUnique',
'message' => 'Mail already exist inside database'
)
)
);
public function beforeSave(){
if (isset($this->data['User']['password'])){
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
}
}
Into my validate I have the rules email_unique that check if inside the database is already present another email equal.
When I update a user I make this inside my controller:
$this->User->id = $this->request->data['User']['id'];
if ($this->User->save($this->request->data)) {
$this->redirect (array ('action'=>'index'));
}
else{
$this->Session->write('flash_element','error');
$this->Session->setFlash ('Error');
}
It always fail because email isn't unique but is the same record!
I would like to know what is the best method to escape the validation if the save is an update not a create?
Or something like: check if the page is edit escape validation or I don't know.. maybe there are many system, I would like to know what is the more correct for my problem.
Thanks
You can adjust your validation rules to only apply when a new record is created, not when an existing record is updated. You can do this by setting the on key in your validation rule to create, so it will look like this:
'email_unique' => array(
'rule' => 'isUnique',
'message' => 'Mail already exist inside database',
'on' => 'create' // Only apply this rule upon creation of a new record
)
See the documentation on this for further details.
If you also want to block duplicate e-mails upon updating, create a beforeSave method in your User model, looking for the e-mail address:
public function beforeSave($options = array()) {
// If the email key is set in the data to be saved...
if (isset($this->data[$this->alias]['email'])) {
// Make sure the email is not already in use by another user
if ($this->find('count', array(
'conditions' => array(
$this->alias . '.id !=' => $this->data[$this->alias]['id'],
$this->alias . '.email' => $this->data[$this->alias]['email']
)
)) > 0) {
// The email is found for a user with another id, abort!
return false;
}
}
}
I have a model Interesttype where i want two fields to be validated one should not be more than the other and none should be less than a particular set value. Here is my model.
class Interesttype extends AppModel
{
public $primaryKey = 'int_id';
public $displayField = 'int_name';
public $hasMany= array(
'Loan' => array(
'className' => 'Loan',
'foreignKey' => 'lon_int_id'
)
);
public $validate = array(
'int_name'=> array(
'rule' => 'notEmpty',
'allowEmpty' => false,
'message' => 'The interest type name is required.'
),
'int_active'=>array(
'rule'=>array('boolean'),
'allowEmpty'=>false,
'message'=>'Please select the status of this interest type'
),
'int_max'=> array(
'numeric'=>array(
'rule' => 'numeric',
'allowEmpty' => false,
'message' => 'Please specify a valid maximum interest rate.'
),
'comparison'=>array(
'rule' => array('comparison','>',1000),
'allowEmpty' => false,
'message' => 'The Maximum interest rate cannot be less than the special rate.'
),
'checklimits'=>array(
'rule' => array('checkRateLimits','int_min'),
'allowEmpty' => false,
'message' => 'The Maximum interest rate cannot be less than the minimum rate.'
)
),
'int_min'=> array(
'numeric'=>array(
'rule' => 'numeric',
'allowEmpty' => false,
'message' => 'Please specify a valid minimum interest rate.'
),
'comparison'=>array(
'rule' => array('comparison','>',1000),
'allowEmpty' => false,
'message' => 'The Minimum interest rate cannot be less than the special rate.'
))
);
function checkRateLimits($maxr,$minr){
if($maxr>=$minr){
return true;
}
else{
return false;
}
}
}
the above model validates my forms well except that one check will not be done, it will not check if the maximum interest rate is indeed more than or equal to the minimum inerest rate.
Where am i going wrong on the validation?
You'll have to add your own Validation Method to make this possible. Here's a pretty generic example that makes use of Validation::comparison() and supports all of its operators (>, <, >=, <=, ==, !=, isgreater, isless, greaterorequal, lessorequal, equalto, notequal) as the second argument in the options array and a field name to compare the validated value to as a third parameter.
class MyModel extends AppModel
{
/* Example usage of custom validation function */
public $validate = array(
'myfield' => array(
'lessThanMyOtherField' => array(
'rule' => array('comparisonWithField', '<', 'myotherfield'),
'message' => 'Myfield value has to be lower then Myotherfield value.',
),
),
);
/* Custom validation function */
public function comparisonWithField($validationFields = array(), $operator = null, $compareFieldName = '') {
if (!isset($this->data[$this->name][$compareFieldName])) {
throw new CakeException(sprintf(__('Can\'t compare to the non-existing field "%s" of model %s.'), $compareFieldName, $this->name));
}
$compareTo = $this->data[$this->name][$compareFieldName];
foreach ($validationFields as $key => $value) {
if (!Validation::comparison($value, $operator, $compareTo)) {
return false;
}
}
return true;
}
}
This is generic, so you could throw the function in your AppModel if you want to make use of it in multiple models in your App. You could also make this a Behavior to share it between different models.
I won't help you to do your code however I will suggest you to read the following backery article http://bakery.cakephp.org/articles/aranworld/2008/01/14/using-equalto-validation-to-compare-two-form-fields
<?php
class Contact extends AppModel
{
var $name = 'Contact';
var $validate = array(
'email' => array(
'identicalFieldValues' => array(
'rule' => array('identicalFieldValues', 'confirm_email' ),
'message' => 'Please re-enter your password twice so that the values match'
)
)
);
function identicalFieldValues( $field=array(), $compare_field=null )
{
foreach( $field as $key => $value ){
$v1 = $value;
$v2 = $this->data[$this->name][ $compare_field ];
if($v1 !== $v2) {
return FALSE;
} else {
continue;
}
}
return TRUE;
}
}
?>
modify the code I think it should be easy from here.
I don't think you can achieve this with directly inserting it into the validate array in a model. You can however specify your own validation function. An example is given here (look at the model code):
Example custom validation function
In your Model your validate should be field name with rule pointing to the function you want to run.
And "None should be less than a particular set value" I would use the "range" validation as seen below. The example will accept any value which is larger than 0 (e.g., 1) and less than 10
public $validate = array(
'maxr'=>array(
'Rate Limits'=>array(
'rule'=>'checkRateLimits',
'message'=>'Your Error Message'
),
'Greater Than'=>array(
'rule'=>array('range', -1, 11),
'message'=>'Please enter a number between 0 and 10'
)
),
);
In the function you pass $data which is the fields being passed. If $data doesn't work try $this->data['Model']['field'] ex($this->data['Model']['maxr'] >= $this->data['Model']['minr'])
function checkRateLimits($data){
if ( $data['maxr'] >= $data['minr'] ) {
return true;
} else {
$this->invalidate('minr', 'Your error message here');
return false;
}
}
Your Form would be something like
echo $this->Form->create('Model');
echo $this->Form->input('maxr');
echo $this->Form->input('minr');
echo $this->Form->end('Submit');
An issue with yii validation rule, I have created a signup form with a coupon code for my product. When a user enters coupon code I want to check if the value is present in the coupon table or not.
When the user enter the couponcode I want my validation rule to work, otherwise, if user doesn't enter the code this validation rule should not work, for member signup, I have a member model and for coupon I have a coupon model,
I am using this method in validation rules.
class MemberSignup extends CActiveRecord
{
public $couponcode;
public function rules(){
array('couponcode', 'isCouponCodeExist'),
}//end rules
public function isCouponCodeExist($attribute, $params)
{
$record = Coupon::model()->findByAttributes(array('couponcode' => $this->couponcode));
if($record === null){
$this->addError($attribute, 'Invalid Coupon');
return false;
}
return true;
}
} //class end
any suggesstion will be helpfull for me
<?php
class MemberSignup extends CActiveRecord
{
public $confPassword;
public $couponcode;
/**
* Returns the static model of the specified AR class.
* #param string $className active record class name.
* #return MemberSignup the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* #return string the associated database table name
*/
public function tableName()
{
return 'members';
}
/**
* #return array validation rules for model attributes.
*/
public function rules(){
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('member_login, member_password,gateway_id, confPassword,email, first_name, packageid,agreed,trafficesource', 'required'),
array('couponcode', 'isCouponCodeExist'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('member_id, member_login', 'safe', 'on'=>'search'),
);
}
public function isCouponCodeExist($attribute,$params){
$record=Coupon::model()->findByAttributes(array('couponcode'=>$this->couponcode));
if($record===null){
$this->addError($attribute, 'Invalid Coupon');
}
}
/**
* #return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
/**
* #return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'member_id' => 'Member',
'member_login' => 'Username',
'user_id' => 'User',
'member_password' => 'Password',
'confPassword' =>'Confirm Password',
'member_level' => 'Member Level',
'affiliate_id' => 'Affiliate',
'first_name' => 'First Name',
'last_name' => 'Last Name',
'email' => 'Email',
'address' => 'Address',
'city' => 'City',
'state' => 'State',
'country' => 'Country',
'zip' => 'Zip',
'home_phone' => 'Home Phone',
'work_phone' => 'Work Phone',
'refered_by' => 'Refered By',
'location' => 'Location',
'product_id' => 'Product',
'product_path' => 'Product Path',
'product_description' => 'Product Description',
'confirmation_hash' => 'Confirmation Hash',
'status' => 'Status',
'cancellation_reason' => 'Cancellation Reason',
'cancellation_date' => 'Cancellation Date',
'registration_date' => 'Registration Date',
'next_billingdate' => 'Next Billingdate',
'CC_no' => 'Cc No',
'CC_expiry' => 'Cc Expiry',
'last_login' => 'Last Login',
'total_rebillings' => 'Total Rebillings',
'ufa_list_size' => 'Ufa List Size',
'billing_amount' => 'Billing Amount',
'privilege' => 'Privilege',
'maximportlimit' => 'Maximportlimit',
'mailingcount' => 'Mailingcount',
'mailinglimit' => 'Mailinglimit',
'registration_ip' => 'Registration Ip',
'address2' => 'Address2',
'Reactivation_Note' => 'Reactivation Note',
'call_date' => 'Call Date',
'CC_last_four' => 'Cc Last Four',
'slidenumber' => 'Slidenumber',
'domain' => 'Domain',
'registerdomain' => 'Registerdomain',
'gb1_affilateID' => 'Gb1 Affilate',
'agreed' => 'Agreed',
'packageid' => 'Packageid',
'ppid' => 'Ppid',
'sendmeitemizedbill' => 'Sendmeitemizedbill',
'is_superstarmember' => 'Is Superstarmember',
'activationdate' => 'Activationdate',
'reactivationdate' => 'Reactivationdate',
'suspensiondate' => 'Suspensiondate',
'is_editor' => 'Is Editor',
'mobile_phone' => 'Mobile Phone',
'member_quta' => 'Member Quta',
'notification' => 'Notification',
'cancellationrequest' => 'Cancellationrequest',
'siteiD' => 'Sitei D',
'companyname' => 'Companyname',
'companywebsite' => 'Companywebsite',
's3_quota' => 'S3 Quota',
's3_quota_consume' => 'S3 Quota Consume',
'gateway_id' => 'Gateway',
'invoice_id' => 'Invoice',
'couponid' => 'Couponid',
'coupon_success' => 'Coupon Success',
'dont_cancel' => 'Dont Cancel',
'notes' => 'Notes',
'trafficesource' => 'Traffice Source',
'othersource' => 'Othersource',
'couponcode'=>'Coupon Code',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* #return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('member_id',$this->member_id);
$criteria->compare('member_login',$this->member_login,true);
$criteria->compare('user_id',$this->user_id);
$criteria->compare('member_password',$this->member_password,true);
$criteria->compare('member_level',$this->member_level);
$criteria->compare('affiliate_id',$this->affiliate_id,true);
$criteria->compare('first_name',$this->first_name,true);
$criteria->compare('last_name',$this->last_name,true);
$criteria->compare('email',$this->email,true);
$criteria->compare('address',$this->address,true);
$criteria->compare('city',$this->city,true);
$criteria->compare('state',$this->state,true);
$criteria->compare('country',$this->country,true);
$criteria->compare('zip',$this->zip,true);
$criteria->compare('home_phone',$this->home_phone,true);
$criteria->compare('work_phone',$this->work_phone,true);
$criteria->compare('refered_by',$this->refered_by,true);
$criteria->compare('location',$this->location,true);
$criteria->compare('product_id',$this->product_id);
$criteria->compare('product_path',$this->product_path,true);
$criteria->compare('product_description',$this->product_description,true);
$criteria->compare('confirmation_hash',$this->confirmation_hash,true);
$criteria->compare('status',$this->status,true);
$criteria->compare('cancellation_reason',$this->cancellation_reason,true);
$criteria->compare('cancellation_date',$this->cancellation_date,true);
$criteria->compare('registration_date',$this->registration_date,true);
$criteria->compare('next_billingdate',$this->next_billingdate,true);
$criteria->compare('CC_no',$this->CC_no,true);
$criteria->compare('CC_expiry',$this->CC_expiry,true);
$criteria->compare('last_login',$this->last_login,true);
$criteria->compare('total_rebillings',$this->total_rebillings);
$criteria->compare('ufa_list_size',$this->ufa_list_size);
$criteria->compare('billing_amount',$this->billing_amount);
$criteria->compare('privilege',$this->privilege,true);
$criteria->compare('maximportlimit',$this->maximportlimit);
$criteria->compare('mailingcount',$this->mailingcount,true);
$criteria->compare('mailinglimit',$this->mailinglimit,true);
$criteria->compare('registration_ip',$this->registration_ip,true);
$criteria->compare('address2',$this->address2,true);
$criteria->compare('Reactivation_Note',$this->Reactivation_Note,true);
$criteria->compare('call_date',$this->call_date,true);
$criteria->compare('CC_last_four',$this->CC_last_four,true);
$criteria->compare('slidenumber',$this->slidenumber,true);
$criteria->compare('domain',$this->domain,true);
$criteria->compare('registerdomain',$this->registerdomain,true);
$criteria->compare('gb1_affilateID',$this->gb1_affilateID,true);
$criteria->compare('agreed',$this->agreed,true);
$criteria->compare('packageid',$this->packageid);
$criteria->compare('ppid',$this->ppid);
$criteria->compare('sendmeitemizedbill',$this->sendmeitemizedbill,true);
$criteria->compare('is_superstarmember',$this->is_superstarmember);
$criteria->compare('activationdate',$this->activationdate,true);
$criteria->compare('reactivationdate',$this->reactivationdate,true);
$criteria->compare('suspensiondate',$this->suspensiondate,true);
$criteria->compare('is_editor',$this->is_editor);
$criteria->compare('mobile_phone',$this->mobile_phone,true);
$criteria->compare('member_quta',$this->member_quta,true);
$criteria->compare('notification',$this->notification,true);
$criteria->compare('cancellationrequest',$this->cancellationrequest,true);
$criteria->compare('siteiD',$this->siteiD);
$criteria->compare('companyname',$this->companyname,true);
$criteria->compare('companywebsite',$this->companywebsite,true);
$criteria->compare('s3_quota',$this->s3_quota);
$criteria->compare('s3_quota_consume',$this->s3_quota_consume);
$criteria->compare('gateway_id',$this->gateway_id,true);
$criteria->compare('invoice_id',$this->invoice_id);
$criteria->compare('couponid',$this->couponid);
$criteria->compare('coupon_success',$this->coupon_success);
$criteria->compare('dont_cancel',$this->dont_cancel);
$criteria->compare('notes',$this->notes,true);
$criteria->compare('trafficesource',$this->trafficesource,true);
$criteria->compare('othersource',$this->othersource,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
If I have understood you correctly, your couponcode is not required and you would like to validate the couponcode only if the user has entered one.
public function rules()
{
return array(
array('field1, field2, field3', 'required'),
array('couponcode', 'isCouponCodeExist'),
);
}
EDIT:
In yii, all validation methods in a model will be executed, even if the field that's being validated is not required. Even though your field couponcode is not required, the validation method isCouponCodeExist() will always be executed.
That means we'll have to edit your code in the method isCouponCodeExist() to allow an empty couponcode, a little something like this:
public function isCouponCodeExist($attribute, $params)
{
if(!empty($this->couponcode))
{
$record = Coupon::model()->findByAttributes(array('couponcode' => $this->couponcode));
if($record === null)
{
$this->addError($attribute, 'Invalid Coupon');
}
}
}
Also, you don't have to return true or false in validation methods. All you need to do is add an error if something is wrong.
I'm trying to set up validation for user registration but I'm having troubles. When I only have the email,role and password fields in the $validation array (remove the others) it works and will save a new user. When I try to add the other fields it fails and gives the flash message error "The user could not be saved. Please, try again."
I'm pretty sure it's the re_password check. When I remove the validation for that it works. However, the re_password validation does display an error when the passwords are different, so I'm not sure where to look
Here's my users table
id | email | password | location | website | role | created | modified
Here's the validation requirements. To get it to save a new user I have to remove everything but email, password and role.
public $validate = array(
'email' => 'email'
,
'password' => array(
'required' => array(
'rule' => array('minLength', '8'),
'message' => 'A password with a minimum length of 8 characters is required'
)
),
're_password' => array(
'required' => array(
'rule' => array('equalTo', 'password' ),
'message' => 'Both password fields must be filled out'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'author')),
'message' => 'Please enter a valid role',
'allowEmpty' => false
)
),
'location' => array(
'valid' => array(
'rule' => array('notEmpty'),
'message' => 'Please select a location'
)
)
);
Here's the form (the options array is above, figured it's not necessary to show)
echo $this->Form->input('email');
echo $this->Form->input('password');
echo $this->Form->input('re_password', array('type'=>'password', 'label'=>'Re-Enter Password', 'value'=>'', 'autocomplete'=>'off'));
echo $this->Form->input('location', array('options' => $options, 'label' => 'Select Nearest Location'));
echo $this->Form->input('website',array('label'=>'Enter your website, such as www.example.com. '));
echo $this->Form->input('role', array('type' => 'hidden', 'default' => 'user'));
Here's the re_password checking function in the User model
function check_user_password($userid) {
$salt = Configure::read('Security.salt');
$this->User->id = $userid;
$hashed_password = $this->User->field('password');
// check password
if($hashed_password == md5($data['User']['re_password'].$salt)) {
return true;
} else {
return false;
}
}
And finally, here's the add function in UsersController
public function add() {
if ($this->request->is('post')) {
$this->User->create(); //create initiates a form on User/add.ctp
if ($this->User->save($this->request->data)) { //save the form data
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('controller' => 'demos', 'action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
Please let me know if there's anything else you need to see
I believe that your re_passwords valiadtion rule equalTo compares its value to string password and not the actual field. I like to use custom functions for this.
so try replacing re_passwords rule array
//'rule' => array('equalTo', 'password' ),
'rule' => array('equalToField', 'password'),
and declare equalToField function in that model
function equalToField($array, $field) {
return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
}
** Also in the future when you seem to have a problem with validation rules
try this in your controllers action (its faster than removing every single rule)
if ($this->User->save($this->request->data)) {
...
} else {
debug($this->User->validationErrors);
...
}
I hope this helps.
Hi Please use following code for your requirement :
override equalTo function by putting your own method in user model:
function equalTo( $field=array(), $compare_field=null )
{
foreach( $field as $key => $value ){
$v1 = $value;
$v2 = $this->data[$this->name][ $compare_field ];
if($v1 !== $v2) {
return FALSE;
} else {
continue;
}
}
return TRUE;
}
Attention, in #luboss answer, where he declares:
function equalToField($array, $field) {
return strcmp($this->data[$this->alias][key($array)], $this->data[$this->alias][$field]) == 0;
}
That cannot work as we are comparing inconsistent fields:
the left member of strcmp has already been hashed, but not the right member.
This happens as a CakePHP automation because the field is called password.
The way I got this to work was to reuse the hashing function in the equalToField helper:
public function equalToField($array, $field) {
$valueFirstOccurrence = $this->data[$this->alias][$field];
$valueSecondOccurrence = Security::hash($this->data[$this->alias][key($array)], $type = 'sha1', $salt = true) ;
return !strcmp($valueFirstOccurrence, $valueSecondOccurrence);
}
Other point :
If you are interested in adding a minLength validation field for your password field, you want to read this good post first:
minLength data validation is not working with Auth component for CakePHP