Replies: 3 comments 10 replies
-
I don't see this as an issue, the method is called validateAndCreate so yeah first it validates and then it creates an object. Though indeed you could first create an object with a magic method and then validate it. This might not be true for all cases, since we actually see magic methods as a last resort. If everything goes well laravel-data doesn't need them. First creating an object and then validating it is extremely dangerous because PHP could start throwing exceptions with wrong property types. I think you could add a createAndValidate method in this case, since you're able to get the validation rules after the object is created and you could manually validate the object. But I don't think this method needs to be in the package since validation is now already such complicated topic. Adding a whole new way to do it makes it even more complicated. |
Beta Was this translation helpful? Give feedback.
-
Hello, I hope you had a long, relaxing weekend! I am sorry, but you are viewing this matter from a maintainers perspective, and I am creating a user issue. The objective fact here is the library does not work as expected. It is not my opinion, not even a matter of opinion. You might not have the motivation or resources to fix it now, it's understandable, but if you expect to run with the library in the current state, it WILL be a huge issue once more people start to use it. Talking purely from a user-of-your-functionality point of view, It's surprising (and undocumented) that:
|
Beta Was this translation helpful? Give feedback.
-
I came across a similar issue this afternoon using the from magic method.
After a quick deep dive I could see the magic from method uses However when using Laravel Data for packages you will need to include the LaravelDataServiceProvider which then resolves the data config. class TestCase extends Orchestra
{
protected function setUp(): void
{
parent::setUp();
$this->loadLaravelMigrations(['--database' => 'testbench']);
}
protected function getEnvironmentSetUp($app)
{
$app['config']->set('database.default', 'testbench');
$app['config']->set('database.connections.testbench', [
'driver' => 'sqlite',
'database' => ':memory:',
'prefix' => '',
]);
}
protected function getPackageProviders($app)
{
return [
LaravelDataServiceProvider::class,
];
}
} @freekmurze Perhaps I should submit a PR for docs package testing here or perhaps a new Testing section under ADVANCED USAGE? |
Beta Was this translation helpful? Give feedback.
-
output:
So the only difference from issue #394 code is:
public string $title_of_hit_song;
- it's no longer optional.It has to be present, and the custom
from
method sets it properly. However, because the validation is launched first, it never comes to that, effectively NEVER calling the magic method and ALWAYS throwing validation exception, making it IMPOSSIBLE for the magicfrom
method to be invoked viavalidateAndCreate
if any validation rules are present.Beta Was this translation helpful? Give feedback.
All reactions