Laravel-Data validateAndCreate not implementing validation rules

40 Views Asked by At

I am expecting that the validateAndCreate function should implement the rules recieved from getValidationRules however it appears it isnt.

Here is the setup:

in my DTO:

namespace App\DTO\Stableford;

use Spatie\LaravelData\Data;
use Spatie\LaravelData\Attributes\Validation\DigitsBetween;

class GetStablefordPointsRequestDTO extends Data
{
    public function __construct(
        public int $par,
        #[DigitsBetween(1, 18)]
        public int $index,
        public int $score,
        public float $handicap
    ) {
    }
}

in my controller:

  $requestdata = GetStablefordPointsRequestDTO::validateAndCreate($request);

I have tried using getValidationRules() which returns the following (correct):

"par" => array:2 [
    0 => "required"
    1 => "numeric"
  ]
  "index" => array:3 [
    0 => "required"
    1 => "numeric"
    2 => "digits_between:1,18"
  ]
  "score" => array:2 [
    0 => "required"
    1 => "numeric"
  ]
  "handicap" => array:2 [
    0 => "required"
    1 => "numeric"
  ]

However when I hit the controller with the folling payload it allows the creation of the object:

{
    "par": 5,
    "index": 2,
    "score": 1,
    "handicap": 0
}
0

There are 0 best solutions below