Closed ctadlock closed 10 months ago
After digging further into the Laravel code I settled on this for the validation data as its a modified version of public function attributesToArray()
public function validationData(): array {
return $this->addCastAttributesToArray($this->getAttributes(), []);
}
@ctadlock Thanks for pointing out. ππΌ I will add the helper method, validationData() {...} in ValidatorTrait.php. So that people can customize the data in the model itself based on the need. You can expect a new release in next few days with this change.
@ctadlock I have made a new release v1.4.0. Please update the package to latest version to get the validationData(...) method helper. π
Thanks for raising this issue. ππΌ
This code is calling model's
toArray()
to get the data to validate, but that data does not include hidden attributes. The concept of hidden is to hide attributes from public view; they should be included in the data set for validation. You cant use model'sgetAttributes()
either as that transforms the data to be prepped for the database which changes its form. For example attributes that are set and casted as an array are represented as a string, which will fail if thearray
validation rule is applied. You cant use model'sattributesToArray()
as that does not include the default values set inprotected $attributes = [..]
.https://github.com/theriddleofenigma/laravel-model-validation/blob/d5d6703c96718be23a3deab0c3e37a96b5fde77d/src/ModelValidator.php#L73
I have a similar internal validation framework to this project. When I ran into this bug I found that every other example and project like this has the same bug. How I solved it was with merging these two arrays.
getAttributes()
has the defaults andattributesToArray()
has the data in the proper casted format.If there is a better solution Id like to know.