开发者

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,

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 wo开发者_StackOverflow中文版rking 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

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. These values match the 'on' property you are trying to use.

Then within lib/Validations.php, I modified the following methods:

Validations::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()

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

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.


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


I'm find a answer for your question without edit library.

Add the before_validation callback and add in this callback a validation rule. 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');
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜