Edit Form method Fill (package vform) - laravel

I request help me with this situation, I am trying to show a form to edit it but the idrol field returns an object, how can I access it to show me the user's role correctly
<div class="form-group row">
<label class="col-md-3 form-control-label" for="text-input">Asignar Rol</label>
<div class="col-md-9">
<select class="form-control" :class="{'is-invalid': form.errors.has('idrol')}"
v-model="form.idrol"
name="idrol">
<option value="0" disabled>Seleccione Rol</option>
<option v-for="role in arrayRoles" :key="role.id" :value="role.id" v-text="role.name">
</option>
</select>
<has-error :form="form" field="idrol"></has-error>
</div>
</div>
Without using the fill method, I can access the property as follows
this.idrol =data['roles'][0].id it shows the role,
but in this way I would not be taking advantage of the benefits of the vform package
Edit Method
editModal(user) {
console.log(user);
let me = this;
me.form.reset();
me.form.clear();
me.modal = 1;
me.tipoAccion = 2;
me.form.fill(user);
}
I appreciate your help

Related

th:field syntax inside th:each is incorrect

I am using spring boot and thymeleaf combination for my project.
Following is the code snippet for Get controller -
#GetMapping("/split")
public String VerticalSplit(Model model) {
MessageContent messageContent0 = new MessageContent();
MessageContent messageContent1 = new MessageContent();
List<MessageContent> messageContentList = new ArrayList<MessageContent>();
messageContentList.add(0, messageContent0);
messageContentList.add(1, messageContent1);
DisplayMessage displayMessage = new DisplayMessage();
model.addAttribute("displayList", displayService.getAllDisplays());
model.addAttribute("groupList", groupService.getAllGroups());
model.addAttribute("messageContentList", messageContentList);
model.addAttribute("displayMessage", displayMessage);
return "combo_vertical_split";
}
And respective html code is as follows -
<html>
<head></head>
<body>
<div class="col-12">
<label class="form-label"> Name of Message </label>
<input type="text" name="name" class="form-control" placeholder="Enter Name Of Message" required="required" th:value="${displayMessage.name}" />
</div>
<hr />
<div th:each="messageContent, iStat : ${messageContentList}">
<h6 align="center" th:text="'Section '+${iStat.count}"></h6>
<div class="form-group">
<label class="form-label"> Select the content type </label>
<select name="messageContentType" id="messageContent" class="form-control" th:onchange="ShowHideTextMediaDiv();" th:field="${messageContent.type}"> <option class="form-select" th:value="Text">Text</option> <option class="form-select" th:value="Image">Image</option> <option class="form-select" th:value="Video">Video</option> </select>
<br />
</div>
</div>
</body>
</html>
When I am trying to load the page, i am getting error as follows -
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'messageContent' available as request attribute
The point which I am not getting is that even though I have declared the messageContent in controller, why am i getting the above mentioned error.
I tried multiple syntax of
th:field="${messageContent.type}"
as follows -
th:field="*{messageContent.type}" //its silly as i have not declared the form object but still i tried
th:field="${messageContentList[__${iStat.index}__].type}"
and few other combinations as well, but nothing worked and almost everytime i got the same error.
You're getting the Neither Bindingnor plain target... error because you have used the th:field in your HTML, but you haven't defined an object from which to bind to it.
You can add your input elements in a form element and add a th:object attribute bound to your model's messageContent object.
You can do something similar to this:
<form th:object="${messageContent}">
<div class="form-group">
<label class="form-label"> Select the content type </label>
<select name="messageContentType" id="messageContent" class="form-control" th:onchange="ShowHideTextMediaDiv();" th:field="*{type}">
<option class="form-select" th:value="Text">Text</option>
<option class="form-select" th:value="Image">Image</option>
</select>
<br />
</div>
</form>

How can i show data from my controller in a textbox?

Hi i calculated a discount in my controller by requesting in a form the percentage and the sale_price, so i did this:
public function store(Request $request){
dd($request->all());
$presupuestoProducto = new PresupuestoProducto();
$presupuestoProducto->sale_price = $request->sale_price;
$presupuestoProducto->discount_percentage = $request->discount_percentage;
$presupuestoProducto->discount = ($sale_price * $discount_percentage)/100;
$presupuestoProducto->save();
Session::flash('success');
return redirect()->route('presupuestos-productos.view');
}
But now i want to make the result of $presupuestoProducto->discount to appear in a textbox. So it would be like an autocomplete field. So up to now i have it this way in my view.
<div class="form-group col-md-2">
<label for="sale_price">Precio de Venta</label>
<input type="number" pattern="[0-9]+([\.,][0-9]+)?" step="00.01" name="sale_price" class="form-control">
</div>
<div class="form-group col-md-3">
<label for="discount_percentage">Porcentaje de descuento (%)</label>
<input type="number" name="discount_percentage" class="form-control">
</div>
<div class="form-group col-md-3">
<label for="discount">Descuento</label>
<input type="number" name="discount" class="form-control" value="discount">
</div>
As you can see my last div is an input but i want it to be a text box that automatically appears the result of $presupuestoProducto->discount as i fill in the first two inputs. How should i do this?
you have to use view() and pass the $presupuestoProducto with compact() then you access to the var in your view with it's name
$presupuestoProducto->save();
return view("ProductView",compact( 'presupuestoProducto'))
in view : ProductView
<div class="form-group col-md-3">
<label for="discount">Descuento</label>
<input type="number" name="discount" placeholder="discount" class="form-control" value={{$presupuestoProducto->discount}} >
</div>
but if you wanna use route you can try route parameter

I want to get data to the dropdown by using another dropdown in the same page

I have a dropdown on my page to load districts. And I have another dropdown to load Divisions according to that selected district. The below code I have got all the divisions. But I want only the divisions according to the selected districts. How can I do?
#php
$districtAll=\App\District::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="district">9. District</label>
<div class="col-md-8">
<select name="district">
<option type="text" class="detail-wp" value="">Select District</option>
#foreach($districtAll as $all)
<option value="{{$all->DISTRICT_ID}}">{{$all->DISTRICT}} </option>
#endforeach
</select>
</div>
</div>
<br>
#php
$getDsAll=\App\DsDivision::all();
#endphp
<div class="row">
<label class="col-md-4 control-label" for="ds_division">10. DS Division</label>
<div class="col-md-8">
<select name="ds_division" class="form-control">
<option type="text" class="detail-wp" value=""></option>
#foreach($getDsAll as $all)
<option value="{{$all->DIVISION_ID}}">{{$all->DIVISION}} </option>
#endforeach
</select>
</div>
</div>
You can do it using ajax request:
$('select[name=district]').on('change', function(){
$districtId = $(this).val();
$.get('/division/district/'+$districtId).done(function(res){
$.each(res, function (key, value) {
$('select[name=ds_division]').append(`<option value='` + value.id + `'>` + value.name +`</option>`);
});
});
to make this working you will need a route like
Route::get("/division/district/{district}", "DivisionController#getByDistrictId");
DivisionController#getByDistrictIdwill be
public function getByDistrictId($district)
{
return Division::where('district_id',$district)->get();
}
this code just to give you an idea of how you can do it.

Dropdown menu from any table in database - Laravel

I have a form, which has some text field.
Let me explain this field:
So_number from table sales_order
so_date from table sales_order
customer_name from table customers
and then I've got linked all of model and controller and views
My controller is linked of my 3 models.
and I try to call with {{}} this, as same as like variable as I made,
I call the details with this
<td>{{$so->so_number}}</td>
this is my web.php of details
Route::get('/so/{so_number}/details', 'So_Controller#details');
this is my So_Controller#details:
public function details($so_number)
{
$data_so = \App\Sales_Order::all();
$data_so_detail = \App\Sales_Order_Details::all();
$data_customer = \App\Customer_Model::all();
return view('salesorder.details', ['data_so_detail' => $data_so_detail, 'data_so' => $data_so, 'data_customer' => $data_customer]);
}
This is my views of my details.index.php
<div class="modal-body">
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<input type="text" name="so_number" value="{{$data_so->so_number}}" class="form-text" required>
<span class="bar"></span>
<label for="so_number">Sales Order Number</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<input type="text" name="so_date" value="{{$data_so->so_date}}" class="form-text" required>
<span class="bar"></span>
<label for="so_date">Sales Order Date.</label>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<label for="customer_name">Customer Name</label>
<select name="customer_name" id="customer_name">
#foreach ($data_customer as $customerName)
<option value="{{$customerName->customer_name}}"></option>
#endforeach
</select>
</div>
</div>
<div class="col-md-6">
<div class="form-group form-animate-text" style="margin-top:40px !important;">
<label for="customer_id"></label>
<select name="customer_id" id="customer_id">
#foreach ($data_customer as $customerId)
<option value="{{$customerId->customer_id}}"></option>
#endforeach
</select>
</div>
</div>
And the error is
ErrorException (E_ERROR)
Property [so_number] does not exist on this collection instance. (View: /Applications/XAMPP/xamppfiles/htdocs/belajar_laravel/resources/views/salesorder/details.blade.php)
Previous exceptions
Property [so_number] does not exist on this collection instance. (0)
My expected output is list of items in dropdown.
Please help me.
In your details.blade.php, you have written
{{$data_so->so_number}}
whereas in controller, you have written
$data_so = \App\Sales_Order::all();
all() returns a collection which is like a 2 dimensional array. Either you have to iterate through $data_so to access 'so_number' or in controller you have to write
$data_so = \App\Sales_Order::first(); //this is an example, first() or find() will return an instance of the model and you can directly access it like $data_so->so_number.
In another case, check your method details($so_number).
In case you want to retrieve from \App\Sales_Order a record for $so_number, you may use find().
You may read https://laravel.com/docs/5.8/eloquent-relationships to connect multiple models.

Can't make multiple select in registration form

I use the basic registration form but I want to add a multiple select, a User can have multiple Skills and select them when he register.
My problem is that when I select multiples values Laravel only take the last one, so I search on the web for a solution and they say to add [] after my class name, so this is what I do :
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" multiple="" class="form-control">
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
But now, when I submit the form the page reload and stay in the register form, and the user is not register... I don't know what to do, anyone of you have a solution ?
Here is how I check the competences in my registerController
$competences = $data['competences'];
foreach ($competences as $comp){
$user->competences()->save(Competence::find($comp));
}
Use multiple="multiple" OR just multiple in blade.php:
<div class="form-group row">
<div class="col-md-6">
<?php $competences = \App\Competence::all(); ?>
<select name="competences[]" class="form-control" multiple>
<option value="" selected disabled style="display:none">choose your school</option>
#foreach ($competences as $competence)
<option value="{{ $competence->id }}">{{ $competence->name }}</option>
#endforeach
</select>
</div>
</div>
then in your controller, verify that you're actually getting the data in the form of an array by just printing $data
return $data;

Resources