Laravel livewire: update text field based on first field value - php

I'm struggling to understand how livewire works.
I have two text fields. One is where the user enters information like prefixes and the second field with a read-only attribute where data will be displayed based on the first field value.
But for some reason, I can't populate the second field. All examples on the internet are how to take a value and return it back or generate a dropdown menu.
my blade template:
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:change="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
and
Livewire class:
class DoSomethingClass extends Component
{
public $prefix;
public $part;
public function testing()
{
$this->part = $this->prefix;
}
public function render()
{
return view('livewire.blade-template');
}
}

You should use wire:model to both input fields. Than, if you want to update the 2nd field on the input of the first, you can use the Livewire Lifecycle to update it.
public $prefix;
public $part;
public function updatedPrefix()
{
$this->part = $this->prefix;
}
Check this link for info on the updatedPrefix() hook: https://laravel-livewire.com/docs/2.x/lifecycle-hooks
(You can choose to use your testing() method too, but than you still need to use the wire:model directive to your second input.)

You can make what you're doing work just by adding wire:model="prefix" to your first input as suggested above.
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:change="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
wire:model="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
And the component itself can just stay the same. When someone types anything in the first field then tabs or clicks away it will update the second field.
If you want the second field to update as the user types change it to wire:keydown="testing", it will make a call with every keystroke.
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">prefix</label>
<input wire:keydown="testing"
type="text"
class="form-control"
id="prefix"
name="prefix"
wire:model="prefix"
/>
</div>
<div class="mb-3">
<label for="exampleFormControlInput1" class="form-label">Code</label>
<input wire:model="part"
type="text"
class="form-control"
id="part"
name="part"
value="{{ $part }}"
/>
</div>
As mentioned above, if you use the updatedPrefix() method you won't need the wire:change or wire:keydown as Livewire will update the field as the user types.

Related

Livewire wire:model.defer shows null value on submit (on second time)

this is my blade view example
<x-jet-form-section submit="store">
<div class="col-span-6 sm:col-span-4">
<x-jet-label for="name" value="{{ __('Name') }}" />
<x-jet-input name="name" wire:model.defer="name" type="text" class="mt-1 block w-full" autocomplete="off" />
<x-jet-input-error for="name" class="mt-2" />
</div>
</x-jet-form-section>
on LiveWire Component I have this debug
public function store() {
dd($this);
}
when i submit the form I see the input field value.
Then i close the debug windod (dd) press submit again and now the value is NULL.
If I change wire:model.defer to wire:model.lazy this not happend again. it works.
why ?!

Laravel 5.8 Field 'name doesn't have a default value

Hello im developing a students CRUD in laravel but i have a problem saving the data in my db.
Here is the problem that laravel returns. SQLSTATE[HY000]: General error: 1364 Field 'name' doesn't have a default value
My store function.
public function store(Request $request)
{
$alumno = Alumno::create();
$alumno->fill($request->all());
$alumno->save();
return redirect('/alumnos');
}
My model:
class Alumno extends Model
{
protected $fillable = ['name','apellido','matricula','correo'];
}
My form:
<form action="/alumnos" method="post">
#csrf
<fieldset class="form-fieldset">
<div class="form-group">
<label class="form-label">Nombre<span class="form-required">*</span></label>
<input type="text" class="form-control" name="name" required/>
</div>
<div class="form-group">
<label class="form-label">Apellido<span class="form-required">*</span></label>
<input type="text" class="form-control" name="apellido" required/>
</div>
<div class="form-group">
<label class="form-label">Matricula<span class="form-required">*</span></label>
<input type="number" class="form-control" required name="matricula" />
</div>
<div class="form-group mb-0">
<label class="form-label">Correo Electronico<span class="form-required">*</span></label>
<input type="email" class="form-control" name="correo" required />
</div>
</fieldset>
<input type="submit" class="btn btn-primary" value="Guardar" />
</form>
What im doing wrong? Please help and thank you!!! :)
The problem is from the form action URL try to change the action to {{ action('YourController#store') }}
You should save data as below:
public function store(Request $request)
{
$alumno = new Alumno();
$alumno = $alumno->create($request->all());
return redirect('/alumnos');
}
and it will work fine.
You should add line to migration
$table->string('name')->nullable();
set form as above
<form action="{{action('YourController#store')}}">
For insert all request inputs
Alumno::create($request->all());
You solve this problem in two different ways
Your database structure has to be changed like below. Default field must be Null. Where description field is your database field.
Like below this
Open your model database/migrations/your_model then use like this code
Schema::table('your_model', function (Blueprint $table) {
$table->string('name')->nullable();
});

How to fix edit interface getting only first word of a column in table in Laravel 5.7?

My edit interface edit.blade.php only gets the first word of the name from my database, this is what the index.blade.php looks like
and when i click on the edit icon of the 3rd line it leads me to edit.blade.php which gives me this
"Nom d'établissement" textfield only gets the first word from the database
Everything looks fine in the database:
this is my edit.blade.php form:
<form method="post" action="{{ route('etablissements.update', $etablissement->id) }}">
#method('PATCH')
#csrf
<div class="col-5">
<div class="form-group">
<label for="nom">Nom Etablissement :</label>
<input type="text" class="form-control" name="nom" value={{ $etablissement->nom }} />
</div>
<div class="form-group">
<label for="price">E-Mail :</label>
<input type="text" class="form-control" name="email" value={{ $etablissement->email }} />
</div>
<div class="form-group">
<label for="quantity">Telephone:</label>
<input type="text" class="form-control" name="telephone" value={{ $etablissement->telephone }} />
</div>
</div>
<button type="submit" class="btn btn-primary">Confirmer</button>
</form>
this is edit function in the controller:
public function edit($id)
{
$etablissement = Etablissement::find($id);
return view('etablissements.edit', compact('etablissement'));
}
and this is update function in the controller:
public function update(Request $request, $id)
{
$request->validate([
'nom'=>'required',
'email'=> 'required',
'telephone' => 'required|numeric'
]);
$etablissement = Etablissement::find($id);
$etablissement->nom = $request->get('nom');
$etablissement->email = $request->get('email');
$etablissement->telephone = $request->get('telephone');
$etablissement->save();
return redirect('/etablissements')->with('success', 'Utilisateur édité');
}
Quote the value attribute.
<input type="text" class="form-control" name="nom" value="{{ $etablissement->nom }}" />
Without quotes, the second word in$etablissement->nom is interpreted as another attribute rather than part of the value of the value attribute.
The email and telephone values are showing up correctly because there are no spaces, but you should quote those as well just in case.

How to get array data in form's value field in laravel?

I am passing a variable with form view to get values from database in form.
public function getGeneralSettings()
{
$generalsetting = DB::table('generalsettings')
->where('id', '=', 1)
->get();
return view('backend.generalsettings.add-general')->with('general', $generalsetting);
}
What i am getting is :
[{"id":1,"sitename":"eqwe","tagline":"ewqewq","logo":"1492084338.jpg","firstemail":"qwewq","secondemail":"ewqewq","firstphone":21321,"secondphone":32132,"address":"
dassa<\/p>","facebook":"dqw","twitter":"q","linkedin":"q","google":"q","youtube":"q","instagram":null,"reddit":"q","rss":"q","created_at":"2017-04-13 11:52:18","updated_at":"2017-04-13 11:52:18"}]
I wana show these values in my form fields.
<div class="form-group">
<label for="sitename">Site Name</label>
<input type="text" id="sitename" name="sitename" class="form-control" value="" placeholder="Site name">
</div>
<div class="form-group">
<label for="tagline">Tagline</label>
<input type="text" id="tagline" name="tagline" placeholder="Tagline" value="" class="form-control">
</div>
How to call them in particular value field. Please help
simply use {{}}
<div class="form-group">
<label for="sitename">Site Name</label>
<input type="text" id="sitename" name="sitename" class="form-control" value="{{$general->sitename}}" placeholder="Site name">
</div>
<div class="form-group">
<label for="tagline">Tagline</label>
<input type="text" id="tagline" name="tagline" placeholder="Tagline" value="{{$general->tagline}}" class="form-control">
</div>
Also use first() instead of get() to get single object
Change your controller code to
public function getGeneralSettings()
{
$generalsetting = DB::table('generalsettings')
->where('id', '=', 1)
->first();
return view('backend.generalsettings.add-general')->with('general', $generalsetting);
}
Whenever you want to retrieve a single row result, use first() instead of get(), get() is used for getting multiple records.

Change Symfony2 FOSUserBundle registration form POST parameters?

This is how the registration form of FOSUserBundle looks like:
<form action="/Symfony/web/signup/" method="POST" class="fos_user_registration_register">
<div id="fos_user_registration_form">
<input type="hidden" id="fos_user_registration_form__token" name="fos_user_registration_form[_token]" value="c248f3ef17b082803ae9948c03d137c380f0dc24"/>
<div>
<label for="fos_user_registration_form_username">Username:</label><input type="text" id="fos_user_registration_form_username" name="fos_user_registration_form[username]" required="required" maxlength="255" pattern=".{2,255}"/>
</div>
<div>
<label for="fos_user_registration_form_email">Email:</label><input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]" required="required"/>
</div>
<div>
<label for="fos_user_registration_form_plainPassword_first">Password:</label><input type="password" id="fos_user_registration_form_plainPassword_first" name="fos_user_registration_form[plainPassword][first]" required="required"/>
</div>
<div>
<label for="fos_user_registration_form_plainPassword_second">Verification:</label><input type="password" id="fos_user_registration_form_plainPassword_second" name="fos_user_registration_form[plainPassword][second]" required="required"/>
</div>
</div>
<div>
<input type="submit" value="Register"/>
</div>
So, as you can see,
<input type="email" id="fos_user_registration_form_email" name="fos_user_registration_form[email]"
MAIN QUESTION: How can I change the id to something like id="email" and also the name to something like name="email"? And it has to work, obviously.
Here you can see: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Resources/views/Registration/register_content.html.twig {{ form_widget(form) }}, but I can't trace this to where it goes. I also presume the RegistrationFormHandler would have to be edited to support these parameters.
Alter buildForm function in RegistrationFormType class:
# FOSUserBundle/Form/Type/RegistrationFormType.php
class RegistrationController extends ContainerAware
{
// ...
public function getName()
{
return 'fos_user_registration';
}
}
Change fos_user_registration to whatever you want.

Categories