Play Framework: How to access again my submitted form during bad request, my code can't access the list again

21 Views Asked by At

index.scala.html

@(form: Form[applicationmodel.ApplicationModel])
@if(form.hasErrors) {
@for((key, value) <- form.errors) {
     <p>@value(0).message</p>
}
} else {
<form action="/search" method="GET">
            <label class="search-label">ID</label>
            <input type="text" name="empId" id="empId" value="@(form.get().empId)"/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label class="search-label">HIRE DATE</label>
            <input type="text" name="hireDateFrom" id="hireDateFrom" value="@(form.get().hireDateFrom)" autocomplete="off"/>
                <div id="curly-dash">~</div>
            <input type="text" name="hireDateTo" id="hireDateTo" value="@(form.get().hireDateTo)" autocomplete="off"/>
                <br>
            <label class="search-label">FIRST NAME</label>
            <input type="text" name="firstName" id="firstName" value="@(form.get().firstName)"/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <label class="search-label">MANAGER</label>
            <input type="text" name="managerName" id="managerName" value="@(form.get().managerName)"/>
                <br>
            <div>
                <label class="search-label">LAST NAME</label>
                <input type="text" name="lastName" id="lastName" value="@(form.get().lastName)"/>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <input type="image" src="@routes.Assets.at("images/BtnSearch.jpg")">
            </div>
        </form>
    </div>
    <form action="/addEdit" method="POST" id="list-form">
        <table id="table-list">
            <caption class="position-relative">
                <img src="@routes.Assets.at("images/Search.jpg")" id="btn-search" class="btn-size">
                <h1 class="inline-block">Employee Records</h1>
                <img src="@routes.Assets.at("images/Add.jpg")" id="btn-add" class="btn-size">
            </caption>
            <thead>
                <tr>
                    <th class="solid-green-border column-title">EMPLOYEE_ID</th>
                    <th class="solid-green-border column-title">FIRST NAME</th>
                    <th class="solid-green-border column-title">LAST NAME</th>
                    <th class="solid-green-border column-title">EMAIL</th>
                    <th class="solid-green-border column-title">PHONE NUMBER</th>
                    <th class="solid-green-border column-title">HIRE DATE</th>
                    <th class="solid-green-border column-title">JOB</th>
                    <th class="solid-green-border column-title">SALARY</th>
                    <th class="solid-green-border column-title">COMMISSION_PCT</th>
                    <th class="solid-green-border column-title">DEPARTMENT</th>
                    <th class="solid-green-border column-title">MANAGER</th>
                    <th class="solid-green-border column-title">ACTION</th>
                </tr>
            </thead>
            <tbody>
                @for(item <- form.get().emplist) {
                <tr>
                    <td class="solid-green-border column-content">@item.employeeId</td>
                    <td class="solid-green-border column-content">@item.firstName</td>
                    <td class="solid-green-border column-content">@item.lastName</td>
                    <td class="solid-green-border column-content">@item.email</td>
                    <td class="solid-green-border column-content">@item.phoneNo</td>
                    <td class="solid-green-border column-content">
                        <div class="position-relative">
                            <span class="class-hiredate">@item.displayHiredate</span>
                            <input type="image" src="/assets/images/imgDate.jpg" class="icon-size date" disabled>
                        </div>
                    </td>
                    <td class="solid-green-border column-content">@item.job.jobTitle</td>
                    <td class="solid-green-border column-content">@item.displaySalary</td>
                    <td class="solid-green-border column-content">@item.commission</td>
                    <td class="solid-green-border column-content department">@item.deptName</td>
                    <td class="solid-green-border column-content">@item.managerName</td>
                    <td class="solid-green-border column-content">
                        <input type="image" src="@routes.Assets.at("images/Edit.jpg")" class="icon-size edit"></td>
                </tr>
                }
            </tbody>
        </table>
         <input type="image" src="@routes.Assets.at("images/BtnSave.jpg")"  id="btn-save">
        </form>
}

What syntax should i use to access the form again during bad request and get that emplist,form.get() is of no use whenever it is in bad request it seems like the form itself is not the same during index

Controller Index

@Transactional
public static Result index() {
System.out.print("asdf");
appModel = new ApplicationModel();
Form<ApplicationModel> form = 
Form.form(ApplicationModel.class).fill(appModel.init(appModel));
return ok(index.render(form));
}

Controller upon submitting i wish to display it all again as is when i submit it with errors,i can only display as of now errors not retain its values

@Transactional
public static Result addEdit() {
Form<ApplicationModel> form = 
Form.form(ApplicationModel.class).bindFromRequest();
if (form.hasErrors()) {
  return badRequest(index.render(form));
}
form.get().update();
ApplicationModel newAppModel = new ApplicationModel();
form = Form.form(ApplicationModel.class).fill(newAppModel.init(newAppModel));
return ok(index.render(form));
}
1

There are 1 best solutions below

0
kenjiro jaucian On

Solution: After many trial and errors

Enclose it with this and access the specific value you want to achieve

@for(i <- 0 to {form("emplist").indexes.size.toInt} - 1) {
}

*I personally hope this would help future generations using play framework to help them accessing forms values with iteration :) *