简体   繁体   中英

How to dynamically fill Text field based on select field option in `Nova Laravel`

How can the text field be set based on the select box in Nova Laravel? (without using libraries)

eg: if the user selects Option as 100, Option Name should be "First Option" (options array is dynamically called)

$options = [
    100 => "First Option"
    101 => "Second Option"
    102 => "Third Option"
  ];
Select::make('Option')->options($options),
Text::make('Option Name')->readonly();

In the same situation, I've used NovaAjaxSelect\AjaxSelect

For someone stumble same question. I used this one on laravel nova4.

            $options = [
                100 => "First Option",
                200 => "Second Option"
            ];
            Select::make('Options')->options(
                $options
            )->required(),
            Text::make('Option Name')->readonly()->dependsOn(
                ['options'],
                function (
                    Text $field,
                    NovaRequest $request,
                    FormData $formData
                ) use ($options) {
                    $field->default(function ($request) use ($formData, $options){
                        return $options[$formData->options];
                    });
                }
            ),

You may create CustomText and Extends Text Field. Override the resolveDefaultValue. with this function.

Your field will listen to other field and change value of your text field to your choice.

    /**
     * Resolve the default value for the field.
     *
     * @param  \Laravel\Nova\Http\Requests\NovaRequest  $request
     * @return mixed
     */
    protected function resolveDefaultValue(NovaRequest $request)
    {
        if ($this->defaultCallback instanceof Closure) {
            return call_user_func($this->defaultCallback, $request);
        }

        return $this->defaultCallback;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM