Skip to content

Model's update method fails when validation rules exist #1584

Description

@hwiesmann

name: Bug report
about: Model's update method fails when validation rules exist that refer to record fields that are not going to be updated.


Describe the bug
Model's update method fails when validation rules exist that refer to record fields that are not going to be updated. The problem is that Model's validation method for update uses all rules instead of only those rules that are related to the updated fields.

CodeIgniter 4 version
CodeIgniter 4.0.0 Alpha 2

Affected module(s)
Model

Expected behavior, and steps to reproduce if appropriate
The update method below fails because the model's rules state that 'name' is required. But for updating name may not be updated (like in the test case) and therefore the rules related to name should not be part of the validation process.

  public function testUpdateWithValidation()
  {
  	$data  = ['description' => 'This is a first test!',
  						'id'          => 42,
  						'name'        => 'valid',
  						'token'       => 42];
  	$model = new ValidModel($this->db);
		$this->assertTrue($model->insert($data));
		$data['description'] = 'This is a second test!';
		unset($data['name']);
		$this->assertTrue($model->update($data)); // assertion fails because of missing 'name'
	}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    databaseIssues or pull requests that affect the database layer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions