Code Monkey home page Code Monkey logo

Comments (4)

dwightwatson avatar dwightwatson commented on June 3, 2024

Just been taking a look at this. I don't remember the original context, but it looks like we pass the mutated/cast values into the validator (with the exception of date casts - because the date validators don't know how to deal with Carbon instances). I believe the intention was that you should be validating a model "as-is" rather than the raw attributes underneath.

I've taken a look at writing the code to have the raw underlying values of a custom cast passed into the validator, but this introduces an inconsistency with existing casts. I don't think it makes sense that the validator gets the cast value of an "out of the box" caster, but gets the raw value of a "custom" caster.

Perhaps your validations need to be updated to work with the cast value rather than the original, but I can see how that's less than ideal. However, I think that's part of the compromise of doing model validations when the framework has leaned heavily toward request validations.

I hope this makes sense, but as it stands now this is expected behaviour and unlikely to change.

from validating.

Mechstud avatar Mechstud commented on June 3, 2024

from validating.

dwightwatson avatar dwightwatson commented on June 3, 2024

An exception was made for Carbon instances, but not for other casts - all other casts are applied, and so are custom casts.

from validating.

Mechstud avatar Mechstud commented on June 3, 2024

This comment is for other library users, and facing similar issue(s) with Custom Casts in Laravel 7.x, may utilize my fix. In my Base Model class, I overrided the getModelAttributes() function to use getAttributes() only. As, in Laravel 6+ (afaik), it is already mutated using set mutators. Here is a code snippet:

use Watson\Validating\ValidatingModel;

abstract class BaseModel extends ValidatingModel
{
    /**
     * @overload
     */
    public function getModelAttributes()
    {
        return $this->getAttributes();
    }
}

Your respective Concrete Model classes, will need to extend from the BaseModel class:

class Post extends BaseModel
{
  // other implementations
}

from validating.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.