Open bertramakers opened 11 months ago
Hmm potentially would consider supporting this stuff in the future, but for now I wonder if one of these would support your use-case:
create
or update
method, after values have been set on the model:public function create(object $model, Context $context): object
{
if ($model->template && $model->name) {
throw new UnprocessableEntityException([
'source' => ['pointer' => '/data'],
'detail' => 'You can only specify one of template or name',
]);
}
// persist the model...
return $model;
}
Field\Str::make('name')
->validate(function ($value, $fail, $context) {
if ($value && $context->body()['data']['relationships']['template']['data'] ?? null) {
$fail('You can only specify one of template or name');
}
})
Field\ToOne::make('template'),
Field\Str::make('name')
->validate(function ($value, $fail, $context) {
if ($value && $context->model->template) {
$fail('You can only specify one of template or name');
}
})
I think the first solution is probably best for now. As I said, will consider building the schema-compatible dependent stuff in, but probably only for v1.1 or later.
Thanks @tobyzerner , I'll go with either solution 1 or 2 for now then :)
Hi,
I'm not sure if it's feasible, but it would be helpful to make fields required but only in certain scenario's. For example:
Str::make('a')->depentRequired(['b', 'c'])
to mark it as required only if fields b and c are also present.It's maybe feasible for 1 pair of fields like this, but it will become more complex if there are multiple. Then you'd need something like this I think:
(You could of course always generate it like this with an
allOf
wrapper, even for 1oneOf
pair.)It's this latter functionality that I'm mostly interested in at this point, but sadly it's also the less obvious one to implement. Maybe I can also work around it for our use case by using a custom action/endpoint like you mentioned in #36
More concretely, our use case is that we want to allow a resource to be created from a template. So we'd have templates with an id each, and allow users/clients to create a new resource (of a specific type called "actions") with a template id. If they instead want to create the resource from scratch, they have to provide one or more required fields.
For example, either:
OR
Maybe you also come up with a different solution for this?
The
dependentRequired()
on the other hand is not something that we need right now, but seems something that is easy enough to implement and that could be useful in the future.