Form fill in play framework scala doesn't work - scala

I'm creating an application with list of employees. An admin can see the list of employees and change their data. When he clicks "change" button, html form should be filled with chosen employee data so he could change it. I used standard mechanism in play framework but it doesn't work. I was debugging it and everything looks ok (employee data is set to form) but it doesn't display on my html form... I have no idea why...
def loadEmployee(id: Int) = Action { implicit request =>
val loadedEmployee = EmployeeService.getEmployee(id)
val employee = Await.result(loadedEmployee, Duration(5, SECONDS)).get
val employees = Await.result(EmployeeService.listAllEmployees, Duration(5, SECONDS))
val form = EmployeeForm.form.fill(EmployeeFormData(employee.name, employee.additionalInformation, employee.resume))
Ok(views.html.index(form,employees))
}
View file:
#(employeeForm: Form[models.EmployeeFormData],employees : Seq[models.Employee])
#main() {
<form id="employee-data-form" role="form" action='#routes.ApplicationController.addInformation()' method="post" class="form-horizontal" align="center" autocomplete="off">
<fieldset class="employee-fieldset">
<div class="employee-form">
<label class="form-title" style="color: #F8741B; font-size: 22px;font-weight: bold; text-decoration:none">title</label>
</div>
<br/>
<table align="center" cellspacing="20">
<tr>
<td align="left">
<div class="employee-form" id="name_field_label">
<div class="controls col-xs-offset-3 col-xs-9">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-employee"></span></span>
<strong>name</strong> :
</div>
</div>
</div>
</td>
<td align="center">
<div class="employee-form" id="name_field_value">
<div class="controls col-xs-offset-3 col-xs-9">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-employee"></span></span>
<input type="text" id="name" name="name" value="" placeholder="First Name" class="form-control input-lg" required>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td align="left">
<div class="employee-form" id="additionalInformation_field_label">
<div class="controls col-xs-offset-3 col-xs-9">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-employee"></span></span>
<strong>Additional information</strong> :
</div>
</div>
</div>
</td>
<td align="center">
<div class="employee-form" id="additionalInformation_field_value">
<div class="controls col-xs-offset-3 col-xs-9">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-employee"></span></span>
<textarea rows="4" cols="50" id="additionalInformation" name="additionalInformation" value="" class="form-control input-lg" required></textarea>
</div>
</div>
</div>
</td>
</tr>
<tr>
<td align="left">
<div class="employee-form" id="resume_field_label">
<div class="controls col-xs-offset-3 col-xs-9">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-employee"></span></span>
<strong>resume</strong> :
</div>
</div>
</div>
</td>
<td align="center">
<div class="employee-form" id="resume_field_value">
<div class="controls col-xs-offset-3 col-xs-9">
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-employee"></span></span>
<textarea rows="4" cols="50" id="resume" name="resume" class="form-control input-lg" required></textarea>
</div>
</div>
</div>
</td>
</tr>
</table>
<br/>
<div class="form-actions controls ynt-btn-xlarge">
<button type="submit" class="btn btn-primary ynt-btn-orange">Add</button>
</div>
</fieldset>
</form>
<div class="employee-display" >
<fieldset>
<legend align="center"><h3>Registered Employees</h3></legend>
<table cellspacing="20">
<tr>
<th>employeeid</th>
<th>firstname</th>
<th>lastname</th>
<th>resume</th>
</tr>
#for(employee <- employees){
<tr>
<td>#employee.id</td>
<td>#employee.name</td>
<td>#employee.additionalInformation</td>
<td>#employee.resume</td>
<td>delete</td>
<td>loadEmployee</td>
</tr>
}
</table>
</fieldset>
</div>
}

Your controller code looks OK - you're filling a Form[EmployeeFormData] and passing it to your template. The trouble is, you never use your employeeForm inside the template - there's no way that form can be populated.
If you read up on the Play documentation for showing forms in a view template you'll see that there is a whole family of helpers available for this purpose. In many cases, they are almost as easy to write as the "plain HTML" version such as you've used. Here's an example:
Where you had:
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-employee"></span></span>
<input type="text" id="name" name="name" value="" placeholder="First Name" class="form-control input-lg" required>
</div>
You will need:
<div class="input-group">
<span class="input-group-addon"><span class="glyphicon glyphicon-employee"></span></span>
#helper.inputText(
employeeForm("name"),
'id -> "name",
'placeholder -> "First Name",
'class -> "form-control input-lg",
'required -> "true"
)
</div>
Notice how you can pass any number of arbitrary arguments into the generated HTML by prefixing with the ' character. This can be very handy, for example if you would like to set extra data- attributes on the input.

Related

Laravel 8 updating Data using modal

recently m learning how to update data by using modal.i can successfully inserted data by modal but can not update before that when i press on edit button my modal pops up without fetched data.
here is my blade file with script-
<div>
<h5 style="text-align: center">Modal Practice</h5>
</div>
{{-- ................................... --}}
<div class="container">
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#staticBackdrop">
Add data
</button>
{{-- #dd($alldata) --}}
<table id="myTable" class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Common Name</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody>
#foreach ($alldata as $key => $data)
<tr>
<th scope="row">{{ $key + 1 }}</th>
<td>{{ $data->first_name }}</td>
<td>{{ $data->last_name }}</td>
<td>{{ $data->common_name }}</td>
<td>
<a href="#" class="btn btn-warning btn-sm edit" data-bs-toggle="modal"
data-bs-target="#editModal">Edit</a>
Delete
</td>
</tr>
#endforeach
</tbody>
</table>
</div>
<!-- Add Modal -->
<div class="modal fade" id="staticBackdrop" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="{{ route('savedata') }}" method="POST" class="row g-3">
<div class="modal-body">
#if (session()->has('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
#endif
#csrf
<div class="col-md-6">
<label for="inputEmail4" class="form-label">First Name</label>
<input type="text" class="form-control" name="first_name">
</div>
<div class="col-md-6">
<label for="inputEmail4" class="form-label">Last Name</label>
<input type="text" class="form-control" name="last_name">
</div>
<div class="col-md-6">
<label for="inputEmail4" class="form-label">Common Name</label>
<input type="text" class="form-control" name="common_name">
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Understood</button>
</div>
</form>
</div>
</div>
</div>
{{-- end Add modal --}}
<!-- Edit Modal -->
<div class="modal fade" id="editModal" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
aria-labelledby="staticBackdropLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="staticBackdropLabel">Modal title</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<form action="#" method="POST" id="editForm" class="row g-3">
<div class="modal-body">
#csrf
#method('put')
<div class="col-md-6">
<label for="inputEmail4" class="form-label">First Name</label>
<input type="text" class="form-control" id="fname" name="first_name">
</div>
<div class="col-md-6">
<label for="inputEmail4" class="form-label">Last Name</label>
<input type="text" class="form-control" id="lname" name="last_name">
</div>
<div class="col-md-6">
<label for="inputEmail4" class="form-label">Common Name</label>
<input type="text" class="form-control" id="cname" name="common_name">
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary">Update</button>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</form>
</div>
</div>
</div>
{{-- End Edit Modal --}}
<!-- JavaScript Bundle with Popper -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap#5.0.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous">
</script>
<script src="https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/dataTables.bootstrap5.min.js"></script>
<script src="https://cdn.datatables.net/1.10.25/js/dataTables.jqueryui.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
var table = $('#myTable').DataTable();
table.on('click', '.edit', function() {
$tr = $(this).closest('tr');
if ($($tr).hasClass('child')) {
$tr = $tr.prev('.parent');
}
var data = table.row($tr).data();
console.log(data);
$('#first_name').val(data[1]);
$('#last_name').val(data[2]);
$('#common_name').val(data[3]);
$('#editForm').attr('action', '/savedata' + data[0]);
$('#editModal').modal('show');
});
});
</script>
here is controller-
public function update(Request $request,$id)
{
$alldata = DataSave::find($id);
$alldata->update([
'first_name'=>$request->first_name,
'last_name'=>$request->last_name,
'common_name'=>$request->common_name
]);
return redirect()->route('index')->with('success', 'Update Successful');;
}
}
and here is my route-
Route::get('/index',[SaveController::class,'index'])->name('index');
Route::post('/save',[SaveController::class,'savedata'])->name('savedata');
to get a modal pop-up with fetched data i use something like (look id=""):
<!-- Add Modal -->
<div class="modal fade" id="editModal{{ $id }}" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1"
aria-labelledby="staticBackdropLabel" aria-hidden="true">
</div>
And button link to modal should be like (look data-target=""):
<td>
<a href="#" class="btn btn-warning btn-sm edit" data-toggle="modal"
data-target="#editModal{{ $id }}">Edit</a>
Delete
</td>

PartialView on each list items

I have a in item list in each row has an edit button, which is supposed to open a partial view with the elements data to edit.
In the loop I use to fill tge datatable, I also create the partial views for each row element.
When I click the buton it always opens the first element partial view, even if I press the Nth element.
How do I get the code to open the corresponding partial view?
Code:
<table class="table table-hover" id="tabela_rota">
<thead>
<tr>
<th>
</th>
<th>
Cliente
</th>
<th>
Rota
</th>
<th>
Distância Ideal
</th>
<th>
Ativa
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<partial>
#{await Html.RenderPartialAsync("Edit", item);}
</partial>
<tr>
<td width="15">
</td>
<td width="15%">
#Html.DisplayFor(modelItem => item.id_cliente)
</td>
<td width="25%">
#Html.DisplayFor(modelItem => item.nome)
</td>
<td width="15%">
#Html.DisplayFor(modelItem => item.distancia_ideal)
</td>
<td width="10%">
#Html.DisplayFor(modelItem => item.activa)
</td>
<td>
<button type="button" class="btn btn-outline-primary" onclick="openNavEdit()">
Editar
</button>
<button type="button" class="btn btn-outline-danger">Delete</button>
</td>
</tr>
}
</tbody>
</table>
JS Function:
function openNavEdit() {
document.getElementById("mySidepanelEdit").style.width = "350px";
}
View:
#model GestaoCircuitos.Models.Rota
#{
ViewData["Title"] = "Editar";
}
<div id="mySidepanelEdit" class="sidepanel">
<div id="top_title">
<h6 id="nova_rota">Editar Rota</h6>
<button class="closebtn" onclick="closeNavEdit()">×</button>
</div>
<div class="row form-row-rotas">
<div class="col-12">
<form asp-action="Edit" class="form-rotas">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="id" />
<div class="form-label-group">
<input id="nome" class="form-control" placeholder="Nome" />
<label asp-for="nome" class="control-label">Nome</label>
<span asp-validation-for="nome" class="text-danger"></span>
</div>
<div class="form-label-group">
<input type="text" asp-for="distancia_ideal" class="form-control" placeholder="Distância ideal" />
<label asp-for="distancia_ideal" class="control-label">Distância ideal</label>
<span asp-validation-for="distancia_ideal" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="activa" /> Ativa
</label>
</div>
<div class="form-group">
<input type="submit" value="Editar" class="btn btn-primary" />
</div>
</form>
</div>
</div>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
It's because you're opening based on ID. An ID attribute needs to be unique throughout a document. If you select on an ID, you'll only get the first element.
You'll have to give each edit modal either a unique ID or a class to select from. Try this:
#model GestaoCircuitos.Models.Rota
#{
ViewData["Title"] = "Editar";
}
<div id="sidepanel-edit-#(item.id)" class=" sidepanel">
<div id="top_title">
<h6 id="nova_rota">Editar Rota</h6>
<button class="closebtn" onclick="closeNavEdit(#(item.id))">×</button>
</div>
<div class="row form-row-rotas">
<div class="col-12">
<form asp-action="Edit" class="form-rotas">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<input type="hidden" asp-for="id" />
<div class="form-label-group">
<input id="nome" class="form-control" placeholder="Nome" />
<label asp-for="nome" class="control-label">Nome</label>
<span asp-validation-for="nome" class="text-danger"></span>
</div>
<div class="form-label-group">
<input type="text" asp-for="distancia_ideal" class="form-control" placeholder="Distância ideal" />
<label asp-for="distancia_ideal" class="control-label">Distância ideal</label>
<span asp-validation-for="distancia_ideal" class="text-danger"></span>
</div>
<div class="form-group form-check">
<label class="form-check-label">
<input class="form-check-input" asp-for="activa" /> Ativa
</label>
</div>
<div class="form-group">
<input type="submit" value="Editar" class="btn btn-primary" />
</div>
</form>
</div>
</div>
</div>
#section Scripts {
#{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}
Above, we add a unique id for each edit edit form. Then we can reference those unique id attributes in the main template:
<table class="table table-hover" id="tabela_rota">
<thead>
<tr>
<th>
</th>
<th>
Cliente
</th>
<th>
Rota
</th>
<th>
Distância Ideal
</th>
<th>
Ativa
</th>
<th></th>
</tr>
</thead>
<tbody>
#foreach (var item in Model)
{
<partial>
#{await Html.RenderPartialAsync("Edit", item);}
</partial>
<tr>
<td width="15">
</td>
<td width="15%">
#Html.DisplayFor(modelItem => item.id_cliente)
</td>
<td width="25%">
#Html.DisplayFor(modelItem => item.nome)
</td>
<td width="15%">
#Html.DisplayFor(modelItem => item.distancia_ideal)
</td>
<td width="10%">
#Html.DisplayFor(modelItem => item.activa)
</td>
<td>
<button type="button" class="btn btn-outline-primary" onclick="openNavEdit(#(item.id)">
Editar
</button>
<button type="button" class="btn btn-outline-danger">Delete</button>
</td>
</tr>
}
</tbody>
</table>
and your Javascript would look like:
function openNavEdit(id) {
document.getElementById("sidepanel-edit-" + id).style.width = "350px";
}
function closeNavEdit(id) {
document.getElementById("sidepanel-edit-" + id).style.width = "0";
}
Something to note, I'm assuming your model has an id attribute.

Vertically center input form in table Twitter Bootstrap

I can't vertically center input form in table using Twitter Bootstrap. I've found solution on the stack but it works only with plain text, doesn't work with input form (class vert-align). Do you have any suggestions?
JSFiddle
<table class="table table-striped">
<tr>
<th>
#
</th>
<th>
<i class="glyphicon glyphicon-check"></i>
</th>
<th>
Item#1
</th>
<th>
Item#2
</th>
</tr>
<div class="row">
<tr>
<div class="col-md-2">
<td class="vert-align">
1
</td>
</div>
<div class="col-md-2">
<td class="vert-align">
<input type="checkbox">
</td>
</div>
<div class="col-md-4">
<td class="vert-align">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span><input type="text" class="form-control" placeholder="...">
</div>
</div>
</td>
</div>
<div class="col-md-4">
<td class="vert-align">
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-refresh"></i></span><input type="text" class="form-control" placeholder="...">
</div>
</div>
</td>
</div>
</tr>
</div>
<div class="row">
<tr>
<div class="col-md-2">
<td class="vert-align">
2
</td>
</div>
<div class="col-md-2">
<td class="vert-align">
<input type="checkbox">
</td>
</div>
<div class="col-md-4">
<td>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-user"></i></span><input type="text" class="form-control" placeholder="...">
</div>
</div>
</td>
</div>
<div class="col-md-4">
<td>
<div class="form-group">
<div class="input-group">
<span class="input-group-addon"><i class="glyphicon glyphicon-refresh"></i></span><input type="text" class="form-control" placeholder="...">
</div>
</div>
</td>
</div>
</tr>
</div>
</table>
and css
.datepicker.dropdown-menu {
z-index: 1151;
}
.table tbody>tr>td.vert-align {
vertical-align: middle;
}

How to align label on same line as form?

I have the following code for login screen using bootstrap. Please help in aligning the label in the same line as the form input field.
<div class="container">
<div class="row">
<div class="col-md-6">
<div>
<form class="form-horizontal">
<fieldset>
<legend>Existing Customer</legend>
<div class="control-group">
<label class="control-label" for="user">User Name:</label>
<div class="control-group">
<input type="text" class="textbox" id="user">
</div>
<label class="control-label" for="pwd">Password:</label>
<div class="controls">
<input type="text" class="textbox" id="pwd">
</div>
<div class="controls">
<input class="btn btn-primary" name="commit" type="submit" value="Log In" />
</div>
</div>
</fieldset>
</form>
</div>
</div>
<div class="col-md-6">
<legend>New Customer</legend>
</div>
Hi use table which solves your alignment issues.
-------------------------------------------------------------
<html>
<head>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-6">
<div>
<form class="form-horizontal">
<fieldset>
<legend>Existing Customer</legend>
<table>
<tr>
<td>
<div class="control-group">
<label class="control-label" for="user">User Name:</label>
</td>
<td>
<div class="control-group">
<input type="text" class="textbox" id="user">
</div>
</td>
</tr>
<tr>
<td>
<label class="control-label" for="pwd">Password:</label>
</td>
<td>
<div class="controls">
<input type="text" class="textbox" id="pwd">
</div>
</td>
</tr>
<div class="controls">
<input class="btn btn-primary" name="commit" type="submit" value="Log In" />
</div>
</div>
</table>
</fieldset>
</form>
</div>
</div>
<div class="col-md-6">
<legend>New Customer</legend>
</div>
</body>
i think you should take css from ui expert and move forward in your project. the page
lay out should be designed as the developer only should be able to use(apply) the css in
the UI layer
Here is the code using Bootstrap 3:
<div class="container">
<div class="row">
<div class="col-md-6">
<form class="form-horizontal" role="form">
<fieldset>
<legend>Existing Customer</legend>
<div class="form-group">
<label class="col-sm-2 control-label" for="user">User Name:</label>
<div class="col-sm-4">
<input type="text" class="textbox form-control" id="user">
</div>
</div>
<div class="form-group">
<label class="col-sm-2 control-label" for="pwd">Password:</label>
<div class="col-sm-4">
<input type="text" class="textbox form-control" id="pwd">
</div>
</div>
<div class="form-group">
<input class="btn btn-primary" name="commit" type="submit" value="Log In" />
</div>
</fieldset>
</form>
</div>
<div class="col-md-6">
<legend>New Customer</legend>
</div>
</div>
and the respective fiddle: http://jsfiddle.net/G2cgf/
Submit a margin-left to the button, like this:
<input class="btn btn-primary" name="commit" type="submit" value="Log In" style="margin-left: 15px;"/>

Zend: How do i create custom form elements using setDecorators()?

I need the form html like below:
<form>
<div class="es_form_txt">Name</div>
<div class="es_form"><input type="text" value="" class="email1"></div>
<div class="clear1"></div>
<div class="es_form_txt">Gender</div>
<div class="es_form">
<input type="radio" value="" name=""> Male
<input type="radio" value="" name=""> Female
<input type="radio" value="" name=""> Other
</div>
<div class="clear1"></div>
<div class="es_form_txt">Country Code</div>
<div class="es_form"><input type="text" value="" class="email1"></div>
<div class="clear1"></div>
<div class="es_form_txt">Phone</div>
<div class="es_form"><input type="text" value="" class="email1"></div>
<div class="clear1"></div>
<div class="clear1"></div>
<div class="es_form"> </div>
<div class="es_form"><input type="button" class="button" value="Update"></div>
<div class="clear"></div>
</form>
I tried all sorts of solution in here not working! Could anyone point me in right direction?
I find that when I need to display specific HTML when using Zend_Form the most reliable solution is usually to use the ViewScript decorator.
Build a normal form using Zend_Form.
<?php
//application/forms/Search.php
class Application_Form_Search extends Zend_Form
{
public function init()
{
$this->setMethod('POST');
//assign the view script to the decorator, this can also be done in the controller
$this->setDecorators(array(
array('ViewScript', array(
'viewScript' => '_searchForm.phtml'
))
));
// create new element
$query = $this->createElement('text', 'query');
// element options
$query->setLabel('Search Keywords');
// add the element to the form
$this->addElement($query);
//submit element
$submit = $this->createElement('submit', 'search');
$submit->setLabel('Search Site');
$submit->setDecorators(array('ViewHelper'));
$this->addElement($submit);
}
}
Then build the actual script used to render the form.
<!-- ~/views/scripts/_searchForm.phtml -->
<article class="search">
<!-- set the action and the method -->
<form action="<?php echo $this->element->getAction()?>" method="<?php echo $this->element->getMethod()?>">
<table>
<tr>
<!-- you can render individual decorators. -->
<th><?php echo $this->element->query->renderLabel()?></th>
</tr>
<tr>
<td><?php echo $this->element->query->renderViewHelper()?></td>
</tr>
<tr>
<!-- or you can render a complete element -->
<td><?php echo $this->element->search?></td>
</tr>
</table>
</form>
</article>
This currently renders as:
<article class="search">
<form action="/video/index/display" method="post">
<table>
<tr>
<th>
<dt id="query-label">
<label for="query" class="optional">Search Keywords</label>
</dt>
</th>
</tr>
<tr>
<td>
<input type="text" name="query" id="query" value="" placeholder="Movie Title" size="20">
</td>
</tr>
<tr>
<td>
<input type="submit" name="search" id="search" value="Search Video Collection!">
</td>
</tr>
</table>
</form>
</article>
Some experimentation will be required to get exactly the output you require.