Skip to content

Issue with Laravel 7.x custom Casts and validation #222

@Mechstud

Description

@Mechstud

Hi, there seems to be an issue with Laravel 7.x providing custom Casts feature: https://laravel.com/docs/7.x/eloquent-mutators#custom-casts

I defined a custom Cast, for eg: ZoneEnum, which implements CastsAttributes interface. Eg: North, South, East and West And in the get() method of this, I return an object of ZoneEnum, based on the value stored in the database. While setting the attribute, I mutate from a ZoneEnum object to its string value, in the set() method implementation.

The problem here is that in the getModelAttributes() function (refer: https://github.com/dwightwatson/validating/blob/master/src/ValidatingTrait.php#L131), you are getting attribute values utilizing the Accessor logic. This makes Laravel's code cast the (already mutated/set) value back to ZoneEnum object. This then fails the validation logic.

Also, I noticed that getAttributes() function alone should be enough; you dont really need to mutate again using getAttributeValue(), unless I am missing something ?

Originally posted by @Mechstud in #221 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions