-
-
Notifications
You must be signed in to change notification settings - Fork 86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
livewireValue is not defined #105
Comments
There are two problems. To get around it you will have to
public function updatedStaffId($value)
{
if(filled($value) && ($value === "2" || $value === "5")){
$this->show_birthday_and_certificate = false;
} else {
data_set($this, 'form_data.birthday', ($this->model->birthday ?? "") );
data_set($this, 'form_data.certificate', ($this->model->certificate ?? "") );
$this->show_birthday_and_certificate = true;
}
} |
Thank you for your answer, the problem wasn't solved, but it's the same. |
@swara-mohammed |
Hello again @swara-mohammed I figured out what is going wrong here :) When Livewire hides the I have a So the only way I can figure out at this moment is to add class OBSERVEWhen hiding the field with classes, the data properties will always exist in You might want to filter out those properties when saving your component. Depending on the state of Suggestion for your component. <?php
namespace App\Http\Livewire\Forms\Persons;
use App\Models\Person;
use App\Models\Staff;
use Illuminate\Support\Collection;
use Tanthammar\TallForms\Input;
use Tanthammar\TallForms\Select;
use Tanthammar\TallForms\TallFormComponent;
use Tanthammar\TallFormsSponsors\DatePicker;
use Tanthammar\TallFormsSponsors\Email;
use Tanthammar\TallFormsSponsors\Tel;
class CreateOrUpdatePerson extends TallFormComponent
{
public Collection $breads;
public string $formTitle = '';
public array $staffs = [];
public bool $show_birthday_and_certificate = true;
public function mount(?Person $person)
{
//Gate::authorize()
$this->formTitle = $person->exists ?
__('Edit :item ', ['item' => $person->name])
: __('Add :model ', ['model' => __('Person')]);
$this->breads = new Collection([
['text' => __('Persons'), 'url' => 'persons'],
['text' => __($this->formTitle)],
]);
// set the checkboxes options
$this->staffs = Staff::orderBy('id', 'desc')->pluck('id', 'name')->all();
$this->mount_form($person); // $person from hereon, called $this->model
}
protected function formAttr(): array
{
return [
'formTitle' => $this->formTitle,
'wrapWithView' => true,
'showDelete' => false,
'inline' => true
];
}
protected function onCreateModel($validated_data): void
{
Person::create($this->filterValidatedData($validated_data));
}
protected function onUpdateModel($validated_data): void
{
$this->model->update($this->filterValidatedData($validated_data));
}
protected function filterValidatedData($validated_data): array
{
return $this->show_birthday_and_certificate ? $validated_data : \Arr::except($validated_data, ['birthday', 'certificate']);
}
//optional method, identical to parent
protected function onDeleteModel()
{
$this->defaultDelete();
}
public function updatedStaffId($value): void
{
if (($value === "2" || $value === "5") && filled($value)) {
$this->show_birthday_and_certificate = false;
} else {
$this->show_birthday_and_certificate = true;
}
}
protected function fields(): array
{
return [
Select::make(__('Staff'), 'staff_id')
->options($this->staffs, false) //I think your data is [ $id => $label ]? If not, remove false, or change pluck()
->rules('required'),
Input::make(__('Name'), 'name')
->rules('required')
->autocomplete('new-text'),
Email::make(__('Email'), 'email')
->autocomplete('new-email'),
Tel::make(__('Phone'), 'phone')
->autocomplete('new-tel'),
DatePicker::make(__('Birthday'), 'birthday')
->rootAttr($this->show_birthday_and_certificate ? [] : ['class' => 'hidden'], true)
->locale('en')
->placeholder('Select a date...')
->includeExternalScripts(), //only included once, if multiple DatePicker fields
Input::make(__('Certificate'), 'certificate')
->rootAttr($this->show_birthday_and_certificate ? [] : ['class' => 'hidden'], true)
->rules('nullable')
->autocomplete("off"),
Input::make(__('Address'), 'address')
->rules('nullable')
->autocomplete('new-address'),
];
}
} |
I will add this to the documentation as well. |
Thank you for your solution, but I felt another bug. I'll write the solution below.
solved:
|
@swara-mohammed Yes, that is why I wrote you need to upgrade to the latest version. I discovered that bug when I tested your component. :) |
Returns an error when changing a select several times.
The text was updated successfully, but these errors were encountered: