Fields with readonly() doesn't uses fillUsing
#4006
Replies: 8 comments 6 replies
-
|
Updating |
Beta Was this translation helpful? Give feedback.
-
|
But fillUsing should be triggered when you submit the form, every time shouldn't it so you can correctly populate the database based on the value of the field? I don't understand why it would be one or the other? For instance, I am using dependsOn to populate the options available in a select field, which will update when the parent is changed. In my real use case instance (not the mock), the i am looking to set an attribute on my model so I can pick it up on my observer and trigger an action if it's not null. Because fillUsing is not being triggered everytime, I am unable to consistently react to form submission. |
Beta Was this translation helpful? Give feedback.
-
|
So my real use case is Parent Field (Manufacturer). When changed, the Child (CarModel) updates it options to make all CarModels stored in the system for that Manufacturer available. If the user wants to add a CarModel, they should select 'New Model' which is an extra option I added into the option array. When this CarModel field is set to 'New Model', I have a text box, which is set to dependOn the CarModel field being 'New Model' - when this is the case it becomes editable. When the form is then submitted, I am looking for the new_model attribute and I can take an action to persist the model_name and get an id and update the correct attribute on the formData before persisting the Car. Because the dependsOn is not allowing the fillUsing to work, this isn't working and I think it's a bug, rather than a feature request because you would expect the fillUsing method to be called 100% of the time, irrelevant of other methods on the field? |
Beta Was this translation helpful? Give feedback.
-
|
Oh, and just to clarify, I submitted the form on both occasions to trigger a database save in my video. FillUsing was not actioned when the dependsOn was. |
Beta Was this translation helpful? Give feedback.
-
|
The
So changing |
Beta Was this translation helpful? Give feedback.
-
|
Maybe I'm missing something here - Im not asking the dependsOn to use fillUsing :). They are two completely independent things. dependsOn allows us to trigger changes to field attributes whereas the fillUsing method helps us populate things how we want when we submit the form. The dependsOn feature works great. I'm just saying that when using it, form submission bypasses fillUsing and it shouldn't. |
Beta Was this translation helpful? Give feedback.
-
|
Update the title, |
Beta Was this translation helpful? Give feedback.
-
/**
* Get the fields displayed by the resource.
*
* @param \Laravel\Nova\Http\Requests\NovaRequest $request
* @return array
*/
public function fields(NovaRequest $request)
{
return [
ID::make()->sortable(),
MultiSelect::make('Text Keys', 'content_a')->options([
'one' => 'One',
'two' => 'Two',
'three' => 'Three',
])->fillUsing(function($request, $model, $attribute, $requestAttribute) {
ray('calling fill using from "Text Keys"');
if ($request->exists($requestAttribute)) {
$value = $request[$requestAttribute];
$model->{$attribute} = in_array($value, ['']) ? null : json_decode($value, true);
}
}),
MultiSelect::make('Numeric Keys', 'content_b')->options([
'1' => 'One',
'2' => 'Two',
'3' => 'Three',
])->fillUsing(function($request, $model, $attribute, $requestAttribute) {
ray('calling fill using from "Numeric Keys"');
if ($request->exists($requestAttribute)) {
$value = $request[$requestAttribute];
$model->{$attribute} = in_array($value, ['']) ? null : json_decode($value, true);
}
})->readonly(),
];
}Above code proved that |
Beta Was this translation helpful? Give feedback.

Uh oh!
There was an error while loading. Please reload this page.
-
Description:
When you have a fillUsing method and a dependsOn method on the same field, the fillUsing doesn't populate an attribute of the model. Removing the dependsOn fixes the issue.
Detailed steps to reproduce the issue on a fresh Nova installation:
Screenshot of my code: https://www.dropbox.com/s/z90i1xju2pwwefy/phpstorm64_2022-04-15_10-46-07.png?dl=0
Video: https://www.dropbox.com/s/ufx4w4yjv17lyl0/chrome_2022-04-15_10-48-53.mp4?dl=0
Beta Was this translation helpful? Give feedback.
All reactions