Code Monkey home page Code Monkey logo

Comments (6)

arthurkirkosa avatar arthurkirkosa commented on May 20, 2024
/**
 * Whether the model should throw a ValidationException if it
 * fails validation. If not set, it will default to false.
 *
 * @var boolean
 */
protected $throwValidationExceptions = true;

All in the readme

from validating.

mikebronner avatar mikebronner commented on May 20, 2024

Got that all in there. It throws exceptions when calling $model->save(), just not when calling $model->isValid('saving');

I was able to work around the issue by adding the following code:

        if (!$user->isValid('saving')) {
            $exception = new ValidationException(get_class($user) . ' model could not be persisted as it failed validation.');
            $exception->setModel($user);
            $exception->setErrors($user->getErrors());
            throw $exception;
        }

Looking at the source for the validation trait, is appears that the trait method performValidation() is overriding the observer method of the same name. The trait method does not throw an exception, while the observer method does.

from validating.

dwightwatson avatar dwightwatson commented on May 20, 2024

Haha, yeah it is. My reasoning was that asking a model if it isValid() isn't necessarily an exceptional event. It just tells you whether it is or not. On the other hand, attempting to save a model that isInvalid() should be considered an exceptional event.

I suppose one thing I could do is move the throwing of an exception into a method on the model, say throwValidationException(). That it would be slightly easier for you to accomplish what you're after (but still not exactly what you're going for...):

if ( ! $user->isValid('saving')) $user->throwValidationException()

Actually, another alternative is (I'm thinking as I type!) is another method, like isValidOrFail('saving') which would achieve what you're after without being totally weird about it. What do you think?

from validating.

mikebronner avatar mikebronner commented on May 20, 2024

Yes, I agree with the semantics :). Asking if something IS or ISNOT should return true or false. Is isInvalid() simply the inverse of isValid(), or will it actually throw an exception? If so, I'll be happy to use that.
I like the isValidOrFail() idea, it's semantically clear and conforms to Laravel conventions. I could also see validate() throwing an exception, but not sure if that has naming conflicts.

from validating.

dwightwatson avatar dwightwatson commented on May 20, 2024

isInvalid() simply returns the inverse of isValid(). I'll take a look at implementing isValidOrFail() and isInvalidOrFail() methods tonight. Can't have a validate() method as it seems to conflict with a few other packages, one of which being Sentry I believe, would have to make it work with our performValidation method.

from validating.

dwightwatson avatar dwightwatson commented on May 20, 2024

Tagged in 0.9.1.

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.