How to validate <aui:select> in a Liferay Form? - java

In View.jsp I created a form by using . there are some inputs and 3 select-box on it, actually I want to validate all of them. for inputs I used but I don't know how can I validate my
Actually I want to validate these three :
<aui:select id="birthday_day" name="birthday_day">
<aui:select id="birthday_month" name="birthday_month">
<aui:select id="birthday_year" name="birthday_year">
This is my view.jsp
<portlet:actionURL var="myUrl">
</portlet:actionURL>
<aui:form enctype="multipart/form-data" action="<%= myUrl %>" method="POST" name="fm">
<table border="0" bgcolor=#ccFDDEE>
<tr>
<td colspan="2" align="center"><b>Recruiment Form</b></td>
</tr>
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td><b>First Name:<span>*</span></b></td>
<td>
<aui:input name="fname" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Last Name:<span>*</span></b></td>
<td>
<aui:input name="lname" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Father Name:<span>*</span></b></td>
<td>
<aui:input name="father_name" type="text">
<aui:validator name="required"/>
</aui:input>
</td>
</tr>
<tr>
<td><b>Birth Date:<span>*</span></b></td>
<td>
<aui:select id="birthday_day" name="<portlet:namespace/>birthday_day">
<aui:option value="0">روز</aui:option>
<%
for(int i=1;i<=31;i++) {
%>
<aui:option value="<%=i%>"><%=i%></aui:option>
<%
}
%>
</aui:select>
/
<aui:select id="birthday_month" name="birthday_month">
<aui:option value="0">ماه</aui:option>
<%
String[] monthBirthDay = {"فروردین","اردیبهشت","خرداد","تیر","مرداد","شهریور","مهر","آبان","آذر","دی","بهمن","اسفند"};
for(int j=0;j<monthBirthDay.length;j++) {
%>
<aui:option value="<%=j+1%>"><%=monthBirthDay[j]%></aui:option>
<%
}
%>
</aui:select>
/
<aui:select id="birthday_year" name="birthday_year">
<aui:option value="0">سال</aui:option>
<%
String[] yearBirthDay = {"1370","1369","1368","1367","1366","1365","1364"};
for(int j=0;j<yearBirthDay.length;j++) {
%>
<aui:option value="<%=yearBirthDay[j]%>"><%=yearBirthDay[j]%></aui:option>
<%
}
%>
</aui:select>
</td>
</tr>
</table>
</aui:form>
How can I solve this issue??????
Please guide me.

<aui:select showEmptyOption="true" name="select hospital name" required="true">
</aui:select>
THis may help you.

just assign an id to the aui:select tag
<aui:select inlineField="true" label="xxx" name="xxx" id="xxxId">
and then you can get the value with this (javascript + AUI)
var selectedVal = A.one('#<portlet:namespace/>xxxId').val();
with the selected value you can perform any kind of validation.

Related

Java Jstl foreach

I'm having a few problems with some JSTL code. The thing is that i'm trying to compare the id of the user that logged in with the id from the reservation class which has a foreign key of the user id.And after doing that i'm trying to populate the page with update links based on the reservation id.
Here's the code of what i tried but i can't find a way to make it work.
<table>
<tr>
<th>Reservation Id</th>
<th>Data CheckIn</th>
<th>Data CheckOut</th>
<th>Numar Persoane</th>
<th>Numar Camere</th>
<th>Action</th>
<%
List<ReservationBean> theReserv = (List<ReservationBean>) request.getAttribute("RESERVATION_LIST");
%>
<% int userId = (Integer) request.getSession().getAttribute("userId"); %>
<c:forEach var="tempReservation" items="${RESERVATION_LIST}">
<!-- set up a link for each res -->
<c:url var="tempLink" value="UserControllerServlet">
<c:param name="command" value="LOAD"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<c:url var="deleteLink" value="UserControllerServlet">
<c:param name="command" value="DELETE"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
</c:forEach>
<% for(ReservationBean res : theReserv){
/* int userId = (Integer) request.getSession().getAttribute("userId"); */
if(userId == res.getUserId()){ %>
<tr>
<td> <%= res.getReservationId() %> </td>
<td> <%= res.getDataCheckin() %> </td>
<td> <%= res.getDataCheckout() %> </td>
<td> <%= res.getNrPersoane()%> </td>
<td> <%= res.getNrCamere() %> </td>
<td>Update
|
<a href="${deleteLink}"
onclick="if(!(confirm('Are you sure you want to delete this reservation?'))) return false">Delete</a>
</td>
</tr>
<%}%>
<%}%>
</tr>
</table>
In the second for i'm comparing the id of the user that logged in with the id of the user from the reservation class but of course the updated links will contain the last value of the first for and i don't know how to do what i did in pure java with JSTL to have only one for and get the correct values of the update link or delete ones.Do you guys have any clue on how to do that?
I've tried something like this
<table>
<tr>
<th>Reservation Id</th>
<th>Data CheckIn</th>
<th>Data CheckOut</th>
<th>Numar Persoane</th>
<th>Numar Camere</th>
<th>Action</th>
<%
List<ReservationBean> theReserv = (List<ReservationBean>) request.getAttribute("RESERVATION_LIST");
%>
<% int userId = (Integer) request.getSession().getAttribute("userId"); %>
<c:forEach var="tempReservation" items="${RESERVATION_LIST}">
<!-- set up a link for each res -->
<c:if test="${tempReservation.userId}" == userId >
<c:url var="tempLink" value="UserControllerServlet">
<c:param name="command" value="LOAD"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<c:url var="deleteLink" value="UserControllerServlet">
<c:param name="command" value="DELETE"/>
<c:param name="reservationId" value="${tempReservation.reservationId}"/>
</c:url>
<tr>
<td>${tempReservation.reservationId}</td>
<td>${tempReservation.dataCheckin}</td>
<td>${tempReservation.dataCheckout}</td>
<td>${tempReservation.nrPersoane}</td>
<td>${tempReservation.nrCamere}</td>
<td>Update
|
<a href="${deleteLink}"
onclick="if(!(confirm('Are you sure you want to delete this student?'))) return false">Delete</a>
</td>
</tr>
</c:if>
</c:forEach>
But that if condition doesn't work or i don't know how to compare jstl code with java code
Edit2: I made it work! You had to compare it something like this
<c:if test="${tempReservation.userId == userId }">
Thanks for help!

Sending data using <a href="#"> in the same page and display the result (FORM) using JSP without changing the (FORM) coding

<div id="main_search">
<table>
<tr>
<td>Product</td>
<td>:</td>
<td>
<input type="checkbox" name="product" class="prod_checkbx" value="Bilateral-VC">Bilateral-VC
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Bilateral Non-VC">Bilateral Non-VC
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget Domestic">Budget Domestic
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget High">Budget High
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Budget International">Budget International
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="IDD Buffet Base">IDD Buffet Base
<br>
<input type="checkbox" name="product" class="prod_checkbx" value="Premium">Premium
<br>
</td>
</tr>
<tr>
<td>Destination</td>
<td>:</td>
<td>
<input id="destination" type="text" name="destination" />
</td>
</tr>
<tr>
<td colspan="9" align="center">
<a id="search" name="search" class="button orange small" style="cursor:pointer" href="#">SEARCH</a>
</td>
</tr>
</table>
</div>
<div id="search_result" style="overflow: auto;">
<h2>Floor Price Edit</h2>
<table id="result-tbl" class="searchTable">
<tr>
<th>Destination</th>
<th>Product</th>
</tr>
<tr>
<% String dest=r equest.getParameter( "destination"); %>
<td>
<%=d est %>
</td>
</tr>
<% if (dest !=n ull) { %>
<sql:setDataSource var="db" driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3307/icars" user="root" password="" />
<sql:query var="dest_result" dataSource="${dbsource}">
SELECT * from icx_pricing_desk WHERE DESTINATION LIKE '
<%=d est %>%'
</sql:query>
<c:forEach var="row" items="${dest_result.rows}">
<tr>
<td>
<c:out value="${row.DESTINATION}" />
</td>
</tr>
</c:forEach>
<% } %>
<td>
<% String products[]=r equest.getParameterValues( "product"); %>
<% if(products !=n ull) { %>
<% for(int i=0; i<products.length; i++) { %>
<%=products[i]%>
<sql:query var="prod_result" dataSource="${db }">SELECT * FROM icx_pricing_desk WHERE PRODUCT = '
<%=products[i]%>'</sql:query>
<c:forEach var="row" items="${prod_result.rows}">
<tr>
<td>
<c:out value="${row.PRODUCT}" />
</td>
</tr>
</c:forEach>
<% } } %>
</td>
</table>
</div>
This is some kind of form without <form> tag and for submit button it didn't use <input type="submit"> like normal form. It use <a href="#"> to submit the data.
The question is how do you send the data using the <a href="#">???
In addition, when i click the SEARCH button both <% String dest = request.getParameter("destination"); %> and <% String products[]= request.getParameterValues("product"); %> didn't received any data.
When using the standard form, both can display result. This code is written in same page(JSP).
--The first coding cannot be changed because it was the assignment and need to solve it.--
Add a form tag in your code like
<form name="form1" method="POST">
<table>
<!-- Your code ->
<a id="search" name="search" class="button orange small" style="cursor:pointer" href="javascript:void(0);" onclick="document.form1.submit();">SEARCH</a>
</table>
</form>
Things to remember
Form tag: method(POST), Name of form(form1)
Method called on onclick of a tag

Display results on the same JSP issue

I have a created a small Java web application. I have made use of DAO and a JSP file. My issue is that when I enter an isbn number in the input field, if the book is found in the database, it displays me "Book Found" and the corresponding details. But if I enter an isbn number in the input field, and if the book is not found in the db, it still displays me "Book Found" but with isbn number 0 and book title "null" which I don't want. In that case, it should display me only "Book not found".
Note that the problem lies mainly in the JSP page.
Here is my full code: http://pastebin.com/cTZy4w6V (using pastebin since the code is too long)
Here is the JSP code:
<jsp:useBean id = "bm" class="book.ManagerBook" scope = "session"/>
<h1> Welcome to ABC Library</h1>
<form>
<table>
<tr>
<td> Enter Details </td>
<td><input type="text" name="isbn"></td>
<td><input type="submit" name="find" value="find"></td>
</tr>
</table>
<input type="hidden" name="submitted" value="true">
</form>
<%
Boolean submitted = Boolean.parseBoolean(request.getParameter("submitted"));
if(submitted){
int isbn = Integer.parseInt(request.getParameter("isbn"));
Book b2 = bm.findBook(isbn);
%>
<table>
<tr>
<td colspan=2>
<h2>Book Found</h2>
</td>
</tr>
<tr>
<td><h3>ISBN</h3></td>
<td><h3>Title</h3></td>
</tr>
<tr>
<td><%= b2.getIsbn()%></td>
<td><%= b2.getTitle() %></td>
</tr>
</table>
<%}else if(!submitted){ %>
<h3> Book Not Found</h3>
<% } %>
If possible, don't use scriptlets in jsp. It's a bad practice.
Try something like this:
if(submitted){
int isbn = Integer.parseInt(request.getParameter("isbn"));
Book b2 = bm.findBook(isbn);
if (b2.getTitle() != null) {
%>
<table>
<tr>
<td colspan=2>
<h2>Book Found</h2>
</td>
</tr>
<tr>
<td><h3>ISBN</h3></td>
<td><h3>Title</h3></td>
</tr>
<tr>
<td><%= b2.getIsbn()%></td>
<td><%= b2.getTitle() %></td>
</tr>
</table>
<% } else { %>
<h3> Book Not Found</h3>
<% } %> }

Displaying List elements in JSTL <c:/forEach> with counter variable

I have a scriptlet for-loop that displays movie schedules from a List<Schedule>.
<%
#SuppressWarnings("unchecked")
List<Schedule> scheduleList =
(ArrayList<Schedule>) session.getAttribute("scheduleList");
for (int ctr = 0; ctr < scheduleList.size(); ctr++) {
%>
<tr>
<td class="center">
<input type="radio" name="selection" value="<%=ctr%>" required title="Please select a schedule." />
</td>
<td><%=scheduleList.get(ctr).getMallId()%></td>
<td class="center"><%=scheduleList.get(ctr).getScheduleCinema()%></td>
<td>PHP <%=scheduleList.get(ctr).getSchedulePrice()%></td>
<td><%=scheduleList.get(ctr).getScheduleDate()%></td>
<td><%=scheduleList.get(ctr).getScheduleTime()%></td>
<td class="center"><%=scheduleList.get(ctr).getScheduleSeats()%></td>
</tr>
<%
}
%>
Here is the Schedule bean object for reference:
public class Schedule {
private int scheduleId;
private int movieId;
private String mallId;
private int scheduleCinema;
private BigDecimal schedulePrice;
private Date scheduleDate;
private Time scheduleTime;
private int scheduleSeats;
// getters and setters
}
I have managed to convert it to JSTL with my desired number of iterations (related thread).
<c:forEach var="ctr" begin="0" end="${scheduleList.size()-1}">
<tr>
<td class="center">
<input type="radio" name="selection" value="${ctr}" required title="Please select a schedule." />
</td>
<td>${schedule.mallId}</td>
<td>${schedule.scheduleCinema}</td>
<td>${schedule.schedulePrice}</td>
<td>${schedule.scheduleDate}</td>
<td>${schedule.scheduleTime}</td>
<td>${schedule.scheduleSeats}</td>
</tr>
</c:forEach>
Output:
Firefox Inspector:
<tr>
<td class="center">
<input type="radio" title="Please select a schedule." required="" value="0" name="selection"></input>
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td class="center">
<input type="radio" title="Please select a schedule." required="" value="1" name="selection"></input>
</td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
Unfortunately, the schedule details are not displaying.
How do I display the data of each Schedule element from the List<Schedule> in JSTL?
(The ctr is important by the way, its value will be needed in the next action.)
Instead of
<c:forEach var="ctr" begin="0" end="${scheduleList.size()-1}">
Use:
<c:forEach var="schedule" items="${scheduleList}" varStatus="ctr">
<tr>
<td class="center">
<input type="radio" name="selection" value="${ctr.index}" required title="Please select a schedule." />
</td>
For ctr you could maintain separate counter variable and increment within your forEach.
You can use varStatus as well as var variable in c:forEach
varStatus="loop"
and use it as ${loop.index}
e.g. see the example

how do I dynamically print out a page that is loaded into text boxes?

I have a blur function that writes to the data base
$(".thoughts_box").blur(function(){
var box_id= $(this).attr("id").split("_")[$(this).attr("id").split("_").length-1];
writeListValue(1,box_id,$(this).val());
});
Here is my table
<td><input class="printArea_1 thoughts_box" name="activities_thoughts_1" id="activities_thoughts_1" type="text" style="width:345px;" placeholder="Type or click list button >" /></td>
<td><input type="button" id="thoughts_1" class="list_btn ext_thoughts" value="List >" name="_1" /></td>
</tr>
here is how I am printing it out
function print_list() {
$('input[id=printLoad_1]').val($('input[class=printArea_1]').val());
$('input[id=printLoad_2]').val($('input[class=printArea_2]').val());
$('input[id=printLoad_3]').val($('input[class=printArea_3]').val());
$('input[id=printLoad_4]').val($('input[class=printArea_4]').val());
$('input[id=printLoad_5]').val($('input[class=printArea_5]').val());
for( var i=1; i<=5; i++ ) {
if( document.getElementById('printLoad_'+i).value === '' ) {
document.getElementById('num_'+i).style.display = 'none';
}
}
$(".printing_list").printElement(
{
overrideElementCSS:[
'/css/print_fixer.css',
{ href:'/css/print_fixer.css',media:'print'}],
//leaveOpen:true,
//printMode:'popup',
});
}
here is the page that prints out I need to find a way to dynamically print out what the user puts into the text fields. can anybody please help me.
<div class="printing_list" id="printList" >
<img id="print_logo" src="/images/print_header_med.png">
<div align="left" id="printHead_text"></div>
<br />
<div align="left" class="listPrint_info" style="width:700px;"></div>
<br /><br />
<table width="100%" style="line-height:0px; margin-left:20px;" >
<tr id="num_1">
<td><input id="printLoad_1" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_2">
<td><input id="printLoad_2" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_3">
<td><input id="printLoad_3" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_4">
<td><input id="printLoad_4" type="text" style="width:700px;" />
</td>
</tr>
<tr id="num_5">
<td><input id="printLoad_5" type="text" style="width:700px;" />
</td>
</table>
</div>
elclanrs has the right idea. The code you wrote isn't maintainable. Try something like this:
// My print_list function assumes that I have some textboxes that are class
// textboxClass
function print_list() {
Array.filter( document.getElementsByClassName('textboxClass'), function(elem){
$(".printing_list").printElement( elem );
});
}
If you assign class textboxClass to all of your text boxes, this will loop through each of them, and you can do whatever you want.

Categories

Resources