How to pass value to model (Vuejs 3) - vue.js

in my Vuejs3 project, there is a form in which the new row will be generated by pressing a button, so I put an array to handle new rows then I need to validate inputs, but after validation of the input of the array, the value didn't pass to model, but it works without the validation.. please help me to understand the mistake that I did.
The Form:
<table class="table table-stripped table-border">
<thead>
<tr>
<th>#</th>
<th style="width: 18%">
Medikament <span class="text-danger">*</span>
</th>
<th style="width: 9%">Milligram</th>
<th style="width: 9%">
Packung <span class="text-danger">*</span>
</th>
<th style="width: 7%">
Stück <span class="text-danger">*</span>
</th>
<th style="width: 19%">Apothekenname</th>
<th style="width: 24%">Adresse der Apotheke</th>
<th style="width: 14%">Postleitzahl</th>
</tr>
</thead>
<tbody>
<tr v-for="(item, index) in patientRequestForCreationDtos" :key="index">
<td>{{ index + 1 }}</td>
<td>
<input type="text" v-model="item.drugName" class="form-control" />
</td>
<td>
<input type="number" v-model="item.milligram" class="form-control" />
</td>
<td>
<input type="number" v-model="item.box" class="form-control" />
</td>
<td>
<input type="number" v-model="item.total" class="form-control" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStoreName" :readonly="patientDetails.isElga == false" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStoreAddress" :readonly="patientDetails.isElga == false" />
</td>
<td>
<input type="text" class="form-control" v-model="item.drugStorePostalCode" :readonly="patientDetails.isElga == false" />
</td>
<td>
<button type="button" #click="deleteRequestItemRow(index)" class="btn btn-sm btn-danger">
-
</button>
</td>
</tr>
</tbody>
</table>
The Array:
patientRequestForCreationDtos: [{
milligram: null,
box: null,
total: null,
drugStoreName: "",
drugStoreAddress: "",
drugStorePostalCode: "",
drugName: "",
}, ],
The validation function:
checkValidation() {
if (!this.patientRequestForCreationDtos.drugName) {
Swal.fire("drug name is required...");
return;
}
return true;
},
```js
---
it always says => drug name is required..

this.patientRequestForCreationDtos is array. maybe you can do this.
checkValidation(patientRequestForCreationDto) {
if (!patientRequestForCreationDto.drugName) {
Swal.fire("drug name is required...");
return;
}
return true;
},

if you'll have only one element in patientRequestForCreationDtos then you gotta choose first element in the array and then check its property
checkValidation() {
if (!this.patientRequestForCreationDtos[0].drugName) {
Swal.fire("drug name is required...");
return;
}
return true
},
also if your patientRequestForCreationDtos is always gonna be an array then you might find this helpful

Related

Is Vue 3 backwards compatible with Vue 2?

In different posts around the internet (for example this one) I found that Vue 2 code should work with Vue 3 but for me it does not. For example I have simple component
<template>
<div class="overflow-x-auto bg-white rounded-lg shadow overflow-y-auto relative">
<table class="table">
<thead>
<tr class="text-left">
<th>
<label>
<input type="checkbox">
</label>
</th>
<th>User ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Gender</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<label>
<input type="checkbox" name="1">
</label>
</td>
<td> <span>{{ id }}</span> </td>
<td> <span>Cort</span> </td>
<td> <span>Tosh</span> </td>
<td> <span>{{ email }}</span> </td>
<td> <span>Male</span> </td>
<td> <span>327-626-5542</span> </td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
export default {
data() {
return {
id: 1,
email: 'test#mail.com'
}
},
mounted() {
alert('mounted');
},
created() {
alert('created');
}
};
</script>
The table is generated just fine, but both mounted and created not fired when component made. Also the email and id not shown. Only the table is shown.
Your code works:
Vue.createApp({
data() {
return {
id: 1,
email: 'test#mail.com'
}
},
created() {
alert('created')
},
mounted() {
alert('mounted')
}
}).mount('#app')
<script src="https://unpkg.com/vue#next"></script>
<div id="app" class="overflow-x-auto bg-white rounded-lg shadow overflow-y-auto relative">
<table class="table">
<thead>
<tr class="text-left">
<th>
<label>
<input type="checkbox">
</label>
</th>
<th>User ID</th>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
<th>Gender</th>
<th>Phone</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<label>
<input type="checkbox" name="1">
</label>
</td>
<td> <span>{{ id }}</span> </td>
<td> <span>Cort</span> </td>
<td> <span>Tosh</span> </td>
<td> <span>{{ email }}</span> </td>
<td> <span>Male</span> </td>
<td> <span>327-626-5542</span> </td>
</tr>
</tbody>
</table>
</div>

ASP.NET Core - How would I set a name with ID of HTML element on CSHTML

How would I set a name with ID of HTML element on CSHTML?
<tr>
<td>
#item.Items.ItemID
</td>
<td>
#item.Items.ItemModelDescription
</td>
<td class="text-right">
<input id="#item.Items.ItemID + 'UnitPrice'" class="form-control text-right" value="#item.Items.ItemUnitPrice" />
</td>
<td class="text-right">
<input id="#item.Items.ItemID + 'Quantity'" class="form-control text-right" value="#item.Quantity" oninput="return change_quantity('#item.Items.ItemID')"/>
</td>
<td class="text-right">
#(item.Quantity * item.Items.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.Items.ItemID"><span class="fa fa-trash"></span></a>
</td>
</tr>
I can't get the value of HTML element using javascript is there anyway or proper way of setting an id of each quantity input? Or any keywords to search regarding this one.
According to your code and description, I assume you want to calculate the cost based on the Quantity and the ItemUnitPrice. If that is the case, please refer to the following sample code, you can refer it to change your code:
ViewModel:
public class ItemViewModel
{
public int ItemId { get; set; }
public string ItemDescription { get; set; }
public decimal ItemUnitPrice { get; set; }
public decimalc Quantity { get; set; }
}
Index.cshtml: we could set the id attribute like this id='Quantity_#item.ItemId', after rendering, the output like Quantity_XXX:
#model IEnumerable<WebApplication6.Models.ItemViewModel>
<table class="table">
<thead>
<tr>
...
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>
#item.ItemId
</td>
<td>
#item.ItemDescription
</td>
<td class="text-right">
<input id="ItemUnitPrice_#item.ItemId" class="form-control text-right" type="text" value="#item.ItemUnitPrice" />
</td>
<td class="text-right">
<input id='Quantity_#item.ItemId' class="form-control text-right txtquantity" type="text" value="#item.Quantity" oninput="return change_quantity(this,#item.ItemId)" />
</td>
<td class="text-right">
#(item.Quantity * item.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.ItemId"><span class="fa fa-trash"></span></a>
</td>
</tr>
}
</tbody>
</table>
Then at the end of the Index.cshtml page, add the following JavaScript: we could use the parent() and closest() method to find the current row, and then find the relates elements in this row.
#section Scripts{
<script>
function change_quantity(e, itemid) {
//find the item unit price.
var price = $("#" + "ItemUnitPrice_" + itemid).val();
//find the quantity value.
var quantity = $("#" + "Quantity_" + itemid).val();
//calculate the cost and change the html content.
$(e).parent().closest("tr").find(".text-right:last").html((price * quantity).toFixed(2).toString());
}
</script>
}
The output like this:
you can use this approach
#foreach (var item in Model)
{
<tr id="tr-#item.ItemId">
<td>
#item.ItemId
</td>
<td>
#item.ItemDescription
</td>
<td class="text-right">
<input id="ItemUnitPrice_#item.ItemId" class="form-control text-right" type="text" value="#item.ItemUnitPrice" />
</td>
<td class="text-right">
<input id='Quantity_#item.ItemId' class="form-control text-right txtquantity" type="text" value="#item.Quantity" oninput="return change_quantity(this,#item.ItemId)" />
</td>
<td class="text-right">
#(item.Quantity * item.ItemUnitPrice)
</td>
<td>
<a class="btn btn-sm btn-danger btn-rounded" asp-controller="purchaseorderheader" asp-action="Remove" asp-route-id="#item.ItemId"><span class="fa fa-trash"></span></a>
</td>
</tr>
}

Setting a Form Value in Vue Js

I am using Struts 1.x in my application. Recently i used Vue js.So, I use the same jsp page for Adding and updating (in this case company). So When i click edit, the details are fetched the page is redirected to that page and form values are set from the action class.
Here is the html portion
<form action="company.do" method="post" name="companyForm">
<html:hidden property="method" value="doAddUpdateCompany" />
<html:hidden property="companyId" value="${companyForm.companyId}" />
<div class="row" >
<div class="col-md-9">
<div class="grid simple">
<div class="grid-title ">
<h4>
<logic:equal value="addCompany" property="method" name="companyForm">
New <span class="semi-bold">Company</span>
</logic:equal>
<logic:equal value="updateCompany" property="method" name="companyForm">
Edit <span class="semi-bold">Company</span>
</logic:equal>
</h4>
<div class="tools"> </div>
</div>
<div class="grid-body">
<table class="tbl_wall" style="width: 100%" >
<tr >
<td class="fi_wall" >Company Name<span class="notify style16"> *</span></td>
<td class="fi_wall" ><html:text property="companyName" name="companyForm" styleClass="med_txt" /></td>
<td> </td>
</tr>
<tr>
<td class="fi_wall" valign="top" >Company Code<span class="notify style16"> *</span> (3 Letter)</td>
<td class="fi_wall" valign="middle" ><html:text property="companyCode" maxlength="3" styleClass="med_txt" name="companyForm" /></td>
<td> </td>
</tr>
<tr id="cmp">
<td class="fi_wall">Company Type </td>
<td class="fi_wall"><select name="companyType" v-model="companyType">
<option value="-1">select</option>
<option v-for=" result in results " v-bind:value="result.commonId">
{{ result.name}}</option>
</select></td>
</tr>
<tr>
<td colspan="3" align="center">
<button type="button" class="btn btn-success" onclick="doSave()" ><i class="fa fa-save"></i> Save</button>
</td>
</tr>
</table> </form>
and the vue Portion
<script type="text/javascript">
new Vue({
el: '#cmp',
data: {
results: [],
companyType : '-1'
},
mounted() {
axios.get('/easyoffice/companyType.do?method=getCompanyTypeAjax').then(response => {
this.results = response.data,
this.companyType = document.forms['companyForm'].companyType.value
})
.catch(e => {
this.errors.push(e)
})
}
});</script>
i used this.companyType = document.forms['companyForm'].companyType.value to set form value but it is not working. Any Thoughts?

Error TokenMismatchException in VerifyCsrfToken.php line 68:

I want to do multiple edit, I want to edit from the data I checked following script .blade, I found the TokenMismatchException error in VerifyCsrfToken.php line 68: when updating.
<form name="form" action="{{url('/update-kb')}}" method="post" onsubmit="return deleteConfirm();"/>
<div class="table table-responsive">
<table id="example1" class="table table-bordered">
<thead>
<tr class="info">
<th width="3%"><input type="checkbox" name="select_all" id="select_all" value=""/></th>
<th>No</th>
<th>Data lengkap</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
<?php $i = 1; ?>
#foreach( $datasiswa as $row )
<tr>
<td>
<label class="checkbox-inline"><input type="checkbox" name="checked_id[]" class="checkbox" value="{{$row->id}}"/>
</label>
</td>
<td width="5%">{{ $i }}</td>
<td width="95%">
<table class="table">
<tr>
<td class="info">Nama panggilan</td>
<td>{{$row->nm_panggilan}}</td>
<td class="warning">Pekerjaan ibu</td>
<td>{{$row->pekerjaan_ibu}}</td>
</tr>
<tr>
<td class="info">Jenis kelamin</td>
<td>{{$row->jenis_kelamin}}</td>
<td class="warning">No. Handphone</td>
<td>{{$row->hp_ibu}}</td>
</tr>
<tr>
<td class="info">Tempat, Tanggal lahir</td>
<td>{{$row->tempat}}, {{$row->tanggal_lahir}}</td>
<td class="warning">Alamat</td>
<td>{{$row->alamat}}</td>
</tr>
<tr>
<td class="info">Status anak</td>
<td>{{$row->status_anak}}</td>
<td class="warning">Golongan darah</td>
<td>{{$row->goldar}}</td>
</tr>
<tr>
<td class="info">Agama</td>
<td>{{$row->agama}}</td>
<td class="warning">Nama wali</td>
<td>{{$row->nm_wali}}</td>
</tr>
<tr>
<td class="info">Kewarganegaraan</td>
<td>{{$row->kewarganegaraan}}</td>
</tr>
<tr>
<td class="info">Anak ke-</td>
<td>{{$row->anak_ke}}</td>
</tr>
<tr>
<td class="info">Kelas</td>
<td>{{$row->kelas}}</td>
</tr>
</table>
</td>
<td>
<a href="{!! url('/'.$row->id.'/edit-siswa') !!}">
<button class="btn btn-default btn-block"><i class="fa fa-edit"></i></button><br>
</a>
<a href="{!! url('/'.$row->id.'/delete-siswa') !!}">
<button class="btn btn-danger btn-block"><i class="fa fa-trash"></i></button>
</a>
</td>
</td>
</tr>
<?php $i++; ?>
#endforeach
</tbody>
</table>
<div class="col-md-3">
<input type="submit" class="btn btn-danger" name="delete_submit" value="Hapus"/>
</div>
</div>
</form>
But I still have error, what causes it?
public function updatekb($id, Request $request)
{
$data = Datasiswa::find($id);
if (isset($request->delete_submit)) {
$idArr = $request->checked_id;
foreach ($idArr as $id) {
DB::update('update tb_siswa, tb_pernyataan set tb_pernyataan.kelas = "cekcek" where tb_pernyataan.kelas = "TK A" and tb_siswa.sekolah = "KB TK KHALIFAH 25" and id = "' . $id . '" ');
}
return back();
Session::flash('sukses', 'Data berhasil di update', compact('data'));
}
}
Add below to your form element.
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
Some answers are suggesting to disable csrf protection which is possible but NOT RECOMENDED. This leaves your application vulnureable.
Laravel use CSRF token to verify user request. so you have to use it on yevery request if you want to disable it then you can disable it from See here how to disable it or you can use it as
{{ csrf_field() }}
or
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
You can resolve this issue in two ways:-
First One:-
{{ csrf_field() }}
or
<input type="hidden" name="_token" value="{{ csrf_token() }}"/> // add this in form
Or the other (simpler) way, inside your app\Http\Middleware/VerifyCsrfToken.php add
protected $except = [
'update-kb', // your route name
];
Hope it helps!

Multiple files upload in selenium webdiver

I want to upload 5 files,but my 'file input' is same name/id,how can i possible to upload five files.
My HTML code is:
<div>
<table id="listtable">
</table>
<br/>
<input type="hidden" name="delFiles" id="deletefiles"/>
<table id="filetable">
<tbody>
<tr>
<td>
<input type="file" size="27px" id="page" name="page"/>
</td>
<td>
<a href="#">
<img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
</a>
</td>
</tr>
<tr>
<td>
<input type="file" size="27px" name="page"/>
</td>
<td>
<img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
</td>
</tr>
<tr>
<td>
<input type="file" size="27px" name="page"/>
</td>
<td>
<img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
</td>
</tr>
<tr>
<td>
<input type="file" size="27px" name="page"/>
</td>
<td>
<img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
</td>
</tr>
<tr>
<td>
<input type="file" size="27px" name="page"/>
</td>
<td>
<img name="del" onclick="removeRow(this);" title="delete" alt="delete" src="images/user_delete.png"/>
</td>
</tr>
</tbody>
</table>
<br/>
<br/>
</div>
This works on Chrome:
driver.findElement(By.id("input1")).sendKeys("path/to/first/file-001 \n path/to/first/file-002 \n path/to/first/file-003");
You would do so the same as you would if you were only uploading one file.
driver.findElement(By.id("input1")).sendKeys("path/to/first/file");
driver.findElement(By.id("input2")).sendKeys("path/to/second/file");
driver.findElement(By.id("input3")).sendKeys("path/to/third/file");
driver.findElement(By.id("input4")).sendKeys("path/to/fourth/file");
driver.findElement(By.id("input5")).sendKeys("path/to/fifth/file");
driver.findElement(By.id("upload")).click();
Obviously, you'll need to put in your own correct IDs or whatever.
that easy
like
//input[#type="file"]
will point to the first input tag
and
(//input[#type="file"])[{INDEX}]
where INDEX is the number of the of the input tag
note: indexing in xpath starts from 1
OR you can use the
file_tag_list =driver.find_elements_by_xpath(//input[#type="file"])
function that the python syntax you can find it for different languages just google it.
this function will return a list of webdriver element
and then you can
file_tag_list[0].send_keys(filepath)
file_tag_list[1].send_keys(filepath)
Holy ##### it even works in PHP:
public function waitForAjax()
{
while(true)
{
$ajaxIsComplete = array(
'script' => 'return jQuery.active == 0',
'args' => array()
);
$ajaxIsComplete = $this->execute($ajaxIsComplete);
if ($ajaxIsComplete) {
break;
}
}
}
Thank you :)