{"action":"created","issue":{"url":"https://api.github.com/repos/dwightwatson/validating/issues/122","labels_url":"https://api.github.com/repos/dwightwatson/validating/issues/122/labels{/name}","comments_url":"https://api.github.com/repos/dwightwatson/validating/issues/122/comments","events_url":"https://api.github.com/repos/dwightwatson/validating/issues/122/events","html_url":"https://github.com/dwightwatson/validating/issues/122","id":60617508,"number":122,"title":"ValidatingTrait does not work with Accessors","user":{"login":"splondike","id":113718,"avatar_url":"https://avatars.githubusercontent.com/u/113718?v=3","gravatar_id":"","url":"https://api.github.com/users/splondike","html_url":"https://github.com/splondike","followers_url":"https://api.github.com/users/splondike/followers","following_url":"https://api.github.com/users/splondike/following{/other_user}","gists_url":"https://api.github.com/users/splondike/gists{/gist_id}","starred_url":"https://api.github.com/users/splondike/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/splondike/subscriptions","organizations_url":"https://api.github.com/users/splondike/orgs","repos_url":"https://api.github.com/users/splondike/repos","events_url":"https://api.github.com/users/splondike/events{/privacy}","received_events_url":"https://api.github.com/users/splondike/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-03-11T06:44:21Z","updated_at":"2015-03-11T08:52:47Z","closed_at":"2015-03-11T08:52:47Z","body":"Laravel has the concept of [Accessors](http://laravel.com/docs/4.2/eloquent), unfortunately they don't work with ValidatingTrait. Imagine I have a csv of emails in a single database column and want to validate the entries:\r\n\r\n```php\r\nclass ValidatingModel {\r\n   use ValidatingTrait;\r\n   protected $rules = array(\r\n      'emails' => array(\r\n         'array',\r\n         'each' => 'email'\r\n      )\r\n   );\r\n   public function getEmailsAttribute($value) {\r\n      return explode(',', $items);\r\n   }\r\n   public function setEmailsAttribute($items) {\r\n      $this->attributes['emails'] = implode(',', $items);\r\n   }\r\n}\r\n```\r\n\r\nThis model won't be validated correctly as the makeValidator method uses getModel()->getAttributes() which bypasses the accessor. I think validating against the accessors is the right thing to do, however it is a potentially backward incompatible change.\r\n\r\nIf you take on this suggestion, then I think makeValidator needs to iterate over the keys of its rules and call getAttribute for each of them. This covers the accessor case, and also lets us validate relationships.\r\n\r\nIf we go with the above solution, then when iterating the rule keys, be sure to test with having rules which validate indexed array elements (e.g. 'emails.1' => 'regex:somewhere.com$'). The solution might be to truncate the key after the first period character. Also, we can't just use attributesToArray, it can potentially hide user specified fields which isn't what we want."},"comment":{"url":"https://api.github.com/repos/dwightwatson/validating/issues/comments/78225413","html_url":"https://github.com/dwightwatson/validating/issues/122#issuecomment-78225413","issue_url":"https://api.github.com/repos/dwightwatson/validating/issues/122","id":78225413,"user":{"login":"splondike","id":113718,"avatar_url":"https://avatars.githubusercontent.com/u/113718?v=3","gravatar_id":"","url":"https://api.github.com/users/splondike","html_url":"https://github.com/splondike","followers_url":"https://api.github.com/users/splondike/followers","following_url":"https://api.github.com/users/splondike/following{/other_user}","gists_url":"https://api.github.com/users/splondike/gists{/gist_id}","starred_url":"https://api.github.com/users/splondike/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/splondike/subscriptions","organizations_url":"https://api.github.com/users/splondike/orgs","repos_url":"https://api.github.com/users/splondike/repos","events_url":"https://api.github.com/users/splondike/events{/privacy}","received_events_url":"https://api.github.com/users/splondike/received_events","type":"User","site_admin":false},"created_at":"2015-03-11T08:52:47Z","updated_at":"2015-03-11T08:52:47Z","body":"Yeah, it's not a completely safe change. I would consider it for the 5.0 branch though, I think the main point of the accessors for people using your package would be deserialization. In this case validating the deserialized structure would be more useful than the serialized one."}}