parent form with multiple inputs child form - yii

how do i save multiple rows in child without using any extensions?
Within parent form I have a child form, user just input whatever row is needed. Right now it only saves the first row.
controller:
//some parent stuff form here
$valid = true;
$arrAttrData = array();
if($model->save()){
if(isset($_POST['ProductAttribute'])){
foreach($_POST['ProductAttribute'] as $i=>$attrItem){
$attr = new ProductAttribute;
$attr->attributes = $attrItem;
if ( $model->product_id )
$attr->product_id = $model->product_id;
if ( ! $attr->validate() )
$valid = false;
else $arrAttrData[] = $attr;
}
}
if ( $valid ){
foreach( $arrAttrData as $attr ){
$attr->product_id = $model->product_id;
$attr->save();
}
}
}
parent:
//some parent stuff here
<div id="attribute">
<?php
$this->renderPartial('_attr', array(
'attr' => $attr,
));?>
</div>
_attr:
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm', array(
'id'=>'attr-form',
'htmlOptions'=>array('enctype'=>'multipart/form-data'),
)); ?>
<?php echo $form->errorSummary($attr); ?>
<table>
<tbody>
<tr>
<td>Attribute's Name</td><td>Attribute Price</td><td>Description</td>
</tr>
<?php for ($i=0;$i<5;$i++):?>
<tr>
<td><?php echo CHtml::activeTextField($attr,'[$i]name',array('class'=>'span12','placeholder'=>'Red or X-Small')); ?></td>
<td><?php echo CHtml::activeTextField($attr,'[$i]cost',array('class'=>'span4','placeholder'=>'89.99')); ?></td>
<td><?php echo CHtml::activeTextField($attr,'[$i]description',array('class'=>'span12','placeholder'=>'Spicy Red Thingy')); ?></td>
</tr>
<?php endfor?>
<br>
<?php echo CHtml::error($attr, '[$i]name'); ?>
<?php echo CHtml::error($attr, '[$i]cost'); ?>
<?php echo CHtml::error($attr, '[$i]description'); ?>
</tbody>
</table>
<?php $this->endWidget(); ?>
[edit]:
form html view
<table>
<tbody>
<tr>
<td>Attribute's Name</td><td>Attribute Price</td><td>Description</td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
<tr>
<td><input class="span12" placeholder="Red or X-Small" name="ProductAttribute[$i][name]" id="ProductAttribute_$i_name" type="text" maxlength="155"></td>
<td><input class="span4" placeholder="89.99" name="ProductAttribute[$i][cost]" id="ProductAttribute_$i_cost" type="text"></td>
<td><input class="span12" placeholder="Spicy Red Thingy" name="ProductAttribute[$i][description]" id="ProductAttribute_$i_description" type="text" maxlength="155"></td>
</tr>
</tbody>
</table>

In your controller , try this
if($model->save()){
if(isset($_POST['ProductAttribute'])){
foreach($_POST['ProductAttribute'] as $i=>$attrItem){
$attr = new ProductAttribute;
$attr->attributes = $attrItem;
if ( $model->product_id )
$attr->product_id = $model->product_id;
if ($attr->validate() )
$attr->save();
}
}
unset($attr);
} // foreach
}

Related

How to pass value to model (Vuejs 3)

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

Can not send list of objects from view to controller using tag helper in asp.net core 2.2

I have the following view and I want to send list of object from view to controller and I used asp-for tag helper for data binding but action in controller receives null
#model IEnumerable<GoodsList>
<form method="post" asp-action="SubmitList" asp-controller="Submit">
<table class="table table-bordered">
<thead>
<tr>
<th width="2%">number</th>
<th width="20%">Name</th>
<th width="20%">Brand</th>
<th width="20%">Quantity</th>
<th width="20%">Scale</th>
<th width="8%">operation</th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<tr>
<td>#item.Number</td>
<td><input type="text" readonly="readonly" asp-for="#item.GoodsName" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#item.BrandName" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#item.Quantity" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#item.ScaleName" class="form-control" /></td>
<td>
<select class="form-control" asp-items="#(new SelectList(item.Status,"Id","Name"))">
</select>
</td>
</tr>
}
<tr>
<td colspan="6">
<textarea class="form-control" rows="3" readonly="readonly" cols="5">#Model.Select(s => s.Description).First()</textarea>
</td>
</tr>
</tbody>
</table>
<a class="btn btn-primary">Back</a>
<input type="submit" value="Submit" class="btn btn-success" style="width:auto">
</form>
And Here is my controller which receives null
[HttpPost]
//It receives null
public IActionResult SubmitList(IEnumerable<GoodsList> model)
{
return View();
}
And the Model
public class GoodsList
{
public GoodsList()
{
Status = new List<ApprovalStatus>();
}
public int Number { get; set; }
public string GoodsName { get; set; }
public string BrandName { get; set; }
public int? Quantity { get; set; }
public string UserName { get; set; }
public string RankName { get; set; }
public int? RequestId { get; set; }
public string ScaleName { get; set; }
public IList<ApprovalStatus> Status { get; set; }
}
Any solution?
Thanks in advance
Change your razor view like below:
#model IEnumerable<GoodsList>
<form method="post" asp-action="SubmitList" asp-controller="Submit">
<table class="table table-bordered">
<thead>
<tr>
<th width="2%">number</th>
<th width="20%">Name</th>
<th width="20%">Brand</th>
<th width="20%">Quantity</th>
<th width="20%">Scale</th>
<th width="8%">operation</th>
</tr>
</thead>
<tbody>
#{ var i = 0;}
#foreach (var item in Model)
{
<tr>
<td>#item.Number</td>
<td><input type="text" readonly="readonly" name="[#i].GoodsName" asp-for="#item.GoodsName" class="form-control" /></td>
<td><input type="text" readonly="readonly" name="[#i].BrandName" asp-for="#item.BrandName" class="form-control" /></td>
<td><input type="text" readonly="readonly" name="[#i].Quantity" asp-for="#item.Quantity" class="form-control" /></td>
<td><input type="text" readonly="readonly" name="[#i].ScaleName" asp-for="#item.ScaleName" class="form-control" /></td>
<td>
<select class="form-control" name="[#i].Status[0].Id" asp-items="#(new SelectList(item.Status,"Id","Name"))">
</select>
</td>
</tr>
i++;
}
<tr>
<td colspan="6">
<textarea class="form-control" rows="3" readonly="readonly" cols="5">#Model.Select(s => s.Description).First()</textarea>
</td>
</tr>
</tbody>
</table>
<a class="btn btn-primary">Back</a>
<input type="submit" value="Submit" class="btn btn-success" style="width:auto">
</form>
Result:
Another way:
#model IList<GoodsList> //change this
//..
<tbody>
#for(var i = 0;i<Model.Count();i++)
{
<tr>
<td>#Model[i].Number</td>
<td><input type="text" readonly="readonly" asp-for="#Model[i].GoodsName" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#Model[i].BrandName" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#Model[i].Quantity" class="form-control" /></td>
<td><input type="text" readonly="readonly" asp-for="#Model[i].ScaleName" class="form-control" /></td>
<td>
<select class="form-control" asp-for="#Model[i].Status[0].Id" asp-items="#(new SelectList(Model[i].Status,"Id","Name"))">
</select>
</td>
</tr>
}
<tr>
<td colspan="6">
<textarea class="form-control" rows="3" readonly="readonly" cols="5">#Model.Select(s => s.Description).First()</textarea>
</td>
</tr>
</tbody>
</table>
<a class="btn btn-primary">Back</a>
<input type="submit" value="Submit" class="btn btn-success" style="width:auto">
</form>

<td> tag ignores the width that i've setted

I've created this table that's well formatted in chrome but in ie8 the < td> tag ignores the percentages that i've setted, then if i add table-layout:fixed as someone suggested ie8 ignores the proportion and set them to 25-25-25-25 in the field CAP Via etc...
If i remove the doctype it works (i can't remove it because i need it for something else)
<!DOCTYPE HTML>
<html style ="height: 100%">
<body>
<table cellpadding=4 align=center width=772 border=1 style="border-collapse:collapse;">
<tr>
<td valign=top colspan=4>
<center>
<b>INFORMAZIONI CLIENTE</b>
</center>
</td>
</tr>
<tr>
<td align"left" valign=top colspan=4>
Nome e Cognome del Cliente/Azienda:<br>
<input type="Text" align="LEFT" size="120">
</td>
</tr>
<tr>
<td align"left" valign=top colspan=4>
Via:
<br>
<input type="Text" align="LEFT" size="120">
</td>
</tr>
<tr>
<td align"left" valign=top style="width:1%;">
CAP:
<br>
<input type="Text"align="LEFT" size="4">
</td>
<td align"left" valign=top style="width:1%;">
Città:
<br>
<input type="Text" align="LEFT" size="65">
</td>
<td align"left" valign=top style="width:1%;">
Prov:
<br>
<input type="Text" align="LEFT" size="2">
</td>
<td align"left" valign=top style="width:99%;">
Data:
<br>
<input type="Text" align="LEFT" size="1" > / <input type="Text" align="LEFT" size="1" ><nobr> / <input type="Text" align="LEFT" size="4">
</td>
</tr>
<tr>
<td align"left" valign=top colspan=4>
Info aggiuntive
<br>
<input type="Text" align="LEFT" size="120">
</td>
</tr>
</table>
</body>
</html>

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!

POST request not working in form

I'm trying to submit some form using POST method. Here is par of form:
<form action="interlopers.php" mehod="post" id = "interlopersForm" name="interlopersForm" onsubmit="return validateInterlopersForm()">
<table border="0">
<tr>
<td> <label for="ast_num" > Ast. num </label> </td>
<td> <input type="text" name="ast_num" id = "ast_num"
value="<?php if(isset($_REQUEST['ast_num'])) { echo htmlentities ($_REQUEST['ast_num']); } ?>"
size="6"> </td>
</tr>
<tr>
<td><label for="cut_off"> Cut-off </label></td>
<td><input type="text" name="cut_off" id="cut_off" size="6" ></td>
</tr>
<tr>
<td><label for="data"> Data </label></td>
<td><input type ="checkbox" name="data" id = "dataSDSS" value="SDSS" checked> SDSS <br>
<input type ="checkbox" name="data" id="dataWISE" value="WISE" checked > WISE <br>
<input type ="checkbox" name="data" id = "dataSp" value="Sp" checked> Taxonomy</td>
</tr>
<tr>
<td> <label for="isFinalStep"> Just interlopers? </label> </td>
<td> <input type = "checkbox" name="isFinalStep" id = "isFinalStep"> </td>
</tr>
<tr> <input type="hidden" name="submitted" value="1"> </tr>
<tr>
<td colspan="2" align="center"> <input type="submit" value="Calculate" > </td>
</tr>
</table>
</form>
But, instead of getting POST request im getting GET request. I'm using apache server and php5 on Ubuntu. I tried this on local-server and on remote host, but still GET instead of POST.
Change mehod="post" to method="post".
You wrote wrong.