I have a couple of forms which are using the saveFoo method, one being a file uploader, and one uses checkboxes.
Followed your examples (very handy i must add) and it all appeared to work fine, except it wouldnt save to the db table.
On the file upload, i tried with updatedFoo method, and the file would be uploaded and stored, but the value wouldnt be saved to the db table.
On the checkboxes, despite multiple boxes being checked, a dump of the $validated_value returns null.
I will freely admit, i've just started paying with this, and this could be a 'user error' and not necessarily a bug, but after seeing it on two separate things, i wondered if it may be related.
Environment:
- Tall-forms version: 4.1.2
- Livewire version: 1.2
- Laravel: 8.6
- Is it a Create or Update form?: Update
- Have you installed and configured the laravel-blade-icons package?: Yes
Fields Definition
Component example (checkboxes)
namespace App\Http\Livewire\Forms;
use Livewire\Component;
use Tanthammar\TallForms\Input;
use Tanthammar\TallForms\TallForm;
use Tanthammar\TallForms\Checkboxes;
use App\Models\Source;
use App\Models\Gateway;
use App\Models\Channel;
class GatewayChannelsUpdateForm extends Component
{
use TallForm;
public $channels;
public function mount(Gateway $gateway)
{
//Gate::authorize()
$this->fill([
'wrapWithView' => false, //see https://github.com/tanthammar/tall-forms/wiki/installation/Wrapper-Layout
'showGoBack' => false,
'showReset' => false,
'showDelete' => false,
'inline' => false,
'showTitle' => false,
]);
$this->mount_form($gateway); // $gateway from hereon, called $this->model
}
// Optional method, this already exists in the TallForm trait
public function onUpdateModel($validated_data)
{
$this->model = $this->model->update($validated_data);
}
public function beforeFormProperties()
{
// get existing relations, read the checkboxes docs about integer values
$this->model->channel_ids = array_map('strval', optional($this->model->channels)->pluck('id')->all());
// set the checkboxes options
$this->channels = Channel::orderBy('name')->get()->pluck('id', 'name')->all();
}
public function fields()
{
return [
Checkboxes::make('Select gateway channels', 'channel_ids')
->options($this->channels)
->relation()
->rules('nullable|array|exists:channels,id')
->wire('wire:model.defer')
];
}
public function saveChannelIds($validated_value)
{
//get the options that the user selected via the field name
$selected_channels = $validated_value;
//if the user unselected all available options it means we have to detach any existing relations
dd($selected_channels);
if (blank($selected_channels)) {
$this->model->channels()->detach(); //detach all existing relations
} else {
//in this example we scoped the events to the users current team, so we check which events the user is allowed to attach
//this is also a good example on how to validate the submitted data
$allowed_to_sync = filled($selected_channels) ? array_intersect($selected_channels, array_values($this->channels)) : [];
if (filled($allowed_to_sync)) $this->model->channels()->sync($selected_channels);
}
}
}
bug fixed