So, I was trying to figure out a simple validation logic when building a little custom app on using Laravel Nova.
Problem
Basically, based on the user requirement, upon creation of a new "Facility" object, validation needs to be performed to ensure that there is sufficient available credit in the Client object before the facility can be created. This check only needs to be performed on Create, and does not need to be performed on Update.
Basically, the thinking goes that we're trying to prevent user entry error, but if the user still deliberately updates the value to beyond the available credit, then it must be a legitimate transaction. Additional requirement, am trying to meet this user requirement with as little code as possible without making new Vue components or external libraries, etc if possible.
What doesn't work
Using the built-in max() validation method on Nova was not productive because the validation appeared to be a front-end validation only and doesn't seem to run again upon the value submission. However, at the point of loading up the front-end, the user might not have yet selected the Client object, so the value would not be pre-loaded.
I then realised that in the Nova documentation, there was an example of customer Closure rules to validate resource values!
This is great! Unfortunately, my object would not be referenced within the scope of the closure function. Hmm.. I'm sure PHP allows for variables from the parent scope to be injected. And sure enough, it does!
So. I guess, problem solved. Code logic for now:
Oh wells. Clearly some refactoring would be helpful.. but just dumping the example here in case anyone finds it helpful.
Update!
Did some refactoring so it looks less terrible. Basically i'm using array_splice to replace the "Amount" field with the updated validation logic based on the client object.