cphalcon icon indicating copy to clipboard operation
cphalcon copied to clipboard

[BUG]: Model::getMessages does not allow filtering messages by array of fields

Open SliceOfLife opened this issue 3 years ago • 1 comments

Describe the bug We can validate Model by several fields at once, for example, by Uniqueness validator. But if the filter is producing an error, we can't get only this message by calling Model::getMessages(['field1', 'field2']) because internal implementation allows only strings as messages filter:

    public function getMessages(var filter = null) -> <MessageInterface[]>
    {
        var message;
        array filtered;

        if typeof filter === "string" && !empty filter {
            let filtered = [];

            for message in this->errorMessages {
                if message->getField() == filter {
                    let filtered[] = message;
                }
            }

            return filtered;
        }

        return this->errorMessages;
    }

To Reproduce Steps to reproduce the behavior:

$model->getMessages(['login', 'loginType']); // Returns all Model messages, not just the ones I need

Provide minimal script to reproduce the issue

class User extends Model
{
	public function validation(): bool
	{
		$validator = new Validation();
		$validator->rules(['login', 'loginType'], [
			new Validation\Validator\Uniqueness([
				'model' => $this,
				'message' => 'Login already in use',
			]),
		]);

		return $this->validate($validator);
	}
}

Expected behavior I expect to get messages related to the specified pair of fields.

Details

  • Phalcon version: 5.0.4, but actual for 5.0.x branch: https://github.com/phalcon/cphalcon/blob/5.0.x/phalcon/Mvc/Model.zep#L1927
  • PHP Version: 8.1.14
  • Operating System: Linux
  • Installation type: Compiling from source https://github.com/phalcon/cphalcon/archive/v5.0.4.tar.gz
  • Zephir version (if any):
  • Server: Nginx
  • Other related info (Database, table schema): MariaDB

SliceOfLife avatar Jan 17 '23 11:01 SliceOfLife

Resolved in https://github.com/phalcon/cphalcon/pull/16446

Thank you @SliceOfLife

niden avatar Oct 01 '23 13:10 niden