简体   繁体   English

仅在使用php-activerecord创建时执行验证

[英]Perform validation only on create using php-activerecord

I am creating a User Model using Codeigniter and php-activerecord and the wiki says I can use 'on' => 'create' to have a validation only run when a new record is created, like this, 我正在使用Codeigniter和php-activerecord创建用户模型,并且维基说我可以使用'on'=>'create'来使验证仅在创建新记录时才运行,像这样,

static $validates_presence_of = array(
    array('title', 'message' => 'cannot be blank on a book!', 'on' => 'create')
);

It also states that we have access to "save", "update" and "delete"... 它还指出,我们可以访问“保存”,“更新”和“删除” ...

None of these are working for me though and I can figure out why, here is my code, 这些都不对我有用,我可以弄清楚为什么,这是我的代码,

// Validations
static $validates_presence_of = array(
array('email', 'message' => 'Please enter a valid email address.'),
array('password', 'message' => 'Password must be provided.', 'on' => 'create')
);

I want to set it up like this so that when a user updates their profile, they can leave their password blank to keep their current one. 我想要这样设置,以便用户更新其个人资料时,可以将其密码保留为空白以保持当前身份。

I would appreciate any help or guidance! 我将不胜感激! Thanks! 谢谢!

The reason for this is most likely because it's not been implemented. 其原因很可能是因为尚未实施。

Relevant classes are lib/Model.php and lib/Validations.php 相关的类是lib / Model.php和lib / Validations.php

From a purely abstract standpoint, you would need to track the mode of operation between save and create. 从纯抽象的角度来看,您将需要跟踪保存和创建之间的操作模式。 To do this, I created a public property (public $validation_mode) within lib/Model.php and set that property to 'create' or 'save' in private methods Model::insert() and Model::update() respectively. 为此,我在lib / Model.php中创建了一个公共属性(public $ validation_mode),并分别在私有方法Model :: insert()和Model :: update()中将该属性设置为'create'或'save'。 These values match the 'on' property you are trying to use. 这些值与您尝试使用的“ on”属性匹配。

Then within lib/Validations.php, I modified the following methods: 然后在lib / Validations.php中,我修改了以下方法:

Validations::validates_presence_of() 验证:: validates_presence_of()

public function validates_presence_of($attrs)
{
    $configuration = array_merge(self::$DEFAULT_VALIDATION_OPTIONS, array('message' => Errors::$DEFAULT_ERROR_MESSAGES['blank'], 'on' => 'save'));

    foreach ($attrs as $attr)
    {
        $options = array_merge($configuration, $attr);
        $this->record->add_on_blank($options[0], $options['message'], $options);
    }
}

Errors::add_on_blank() 错误:: add_on_blank()

public function add_on_blank($attribute, $msg, $options = array())
{
    if (!$msg)
        $msg = self::$DEFAULT_ERROR_MESSAGES['blank'];

    if (($value = $this->model->$attribute) === '' || $value === null)
            {
                    if(array_key_exists('on', $options))
                    {
                        if($options['on'] == $this->model->validation_mode)
                        {
                            $this->add($attribute, $msg);
                        }
                    } else {                            
                        $this->add($attribute, $msg);
                    }
            }
}

What this does basically is passes ALL the $options specified in your model (including the 'on' property) down to the Errors::add_on_blank() method where it now has enough information to differentiate between 'on' => 'create' and the default ('on' => 'save'). 这基本上是将模型中指定的所有$ options(包括'on'属性)传递到Errors :: add_on_blank()方法,该方法现在具有足够的信息来区分'on'=>'create'和默认值('on'=>'save')。 Using the public $validation_mode property from the Model class ($this->model->validation_mode), we can determine what the current mode of operation is and whether or not we wish to continue adding the error message or skip it this time around. 使用Model类的公共$ validation_mode属性($ this-> model-> validation_mode),我们可以确定当前的操作模式以及我们是否希望继续添加错误消息或这次跳过该错误消息。

Obviously you would want to document any changes you make and test thoroughly. 显然,您希望记录所做的任何更改并进行彻底的测试。 According to the documentation, all validation methods supposedly support this "common option" along side allow_null, allow_blank but again, if it's not implemented, you will have to make it happen yourself by making these necessary changes. 根据文档,所有验证方法都应该在allow_null,allow_blank旁边支持此“通用选项”,但是同样,如果未实现,则必须通过进行这些必要的更改使它自己实现。

I'm find a answer for your question without edit library. 我在没有编辑库的情况下找到了您问题的答案。

Add the before_validation callback and add in this callback a validation rule. 添加before_validation回调,并在此回调中添加验证规则。 It works for me. 这个对我有用。

static $before_validation_on_create = array('before_validation_on_create');

static $validates_presence_of = array(
    array('login'),
);

public function before_validation_on_create() {
   self::$validates_presence_of[] = array('password');
}

should be call the validation method like this: 应该这样调用验证方法:

     #example
$your_obj = new Instace();

if($your_obj->is_valid()) {
  // if all is correct your logical code
}
else {
 // your logical to show the error messages
}
//doesnt work if you write 

if(!$your_obj->is_valid())

//the other way you must be use the next method 
if($your_obj->is_invalid())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM