gpt4 book ai didi

cakephp - cakephp 模型中的条件验证

转载 作者:行者123 更新时间:2023-12-02 17:53:59 45 4
gpt4 key购买 nike

我想要条件验证,即在更新密码时隐藏字段,并且密码验证为“6 到 15”个字符!因此密码以哈希格式存储在数据库中,因此不允许我更新。我的模型代码如下,

<?php
class User extends AppModel {

public $belongsTo = array(
'Group',
'City',
'Area',
);

public $validate = array(
'name' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Only alphabets and numbers are allowed!'
),
'email' => array(
//~ 'rule1' => array(
//~ 'rule' => 'isUnique',
//~ 'message' => 'Email address already exists!',
//~ 'last' => true
//~ ),
'rule2' => array(
'rule' => 'email',
'message' => 'Invalid Email!',
'last' => true
)
),
//~ 'password' => array(
//~ 'rule' => array('between', 6, 15),
//~ 'required' => true,
//~ 'message' => 'Password must be of 6 to 15 characters'
//~ ),
//~ 'confirm_password' => array(
//~ 'rule' => 'confirmPassword',
//~ 'message' => 'Confirm password do not match!'
//~ ),
//~ 'address' => array(
//~ 'allowEmpty' => false,
//~ 'required' => true,
//~ 'message' => 'Description is required'
//~ ),
//~ 'phone_number' => array(
//~ 'rule' => 'phone',
//~ 'allowEmpty' => false,
//~ 'required' => true,
//~ 'message' => 'Phone Number is required'
//~ )
);

public function beforeSave() {
if (isset($this->data[$this->alias]['password'])) {
$this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
}
return true;
}

public function confirmPassword() {
debug('model');
if ($this->data[$this->alias]['confirm_password'] != '') {
debug('model in');
if(strcmp($this->data[$this->alias]['password'], $this->data[$this->alias]['confirm_password']) == 0) {
debug('model in in');
return true;
}
}
return false;
}

}

我还确认了密码验证。所以请告诉我条件验证的任何解决方案!感谢您的帮助!

最佳答案

您可以拥有多个验证数组,并在保存之前选择要使用的数组:

在模型中:

public $validate = array(
// default validation rules
);

public $validateWithPassword = array(
// validation rules including password validation
);

现在,在您的 Controller 操作中,您可以选择使用密码字段进行验证:

$this->User->validate = $this->User->validateWithPassword;

关于cakephp - cakephp 模型中的条件验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13270911/

45 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com