Spring ElementDecoration and AjaxEventDecoration incompatible with Dijit? - spring

I am using Spring 3.0.5, Webflow 2.3.0 and Spring JS for decorating.
One of my forms has the following code:
<form:select path="selection">
<form:option value="" label="Please select"></form:option>
<form:options></form:options>
</form:select>
<noscript>
<button id="btnSelect" type="submit" name="_eventId_dropDownSelectionChange">Select</button>
</noscript>
<script type="text/javascript">
Spring.addDecoration(new Spring.ElementDecoration({
widgetType : "dijit.form.Select",
widgetAttrs : {
forceWidth : true,
missingMessage : "<s:message code="NotEmpty" />",
}
}));
Spring.addDecoration(new Spring.AjaxEventDecoration({
elementId : "selection",
formId : "templateModel",
event : "onChange",
params : { _eventId: "dropDownSelectionChange" }
}));
</script>
The intention is to render a second part of the form depending on what is selected in the dropdown. This is to be achieved either via an AJAX call on the onChange of the dropdown, or in the case of noscript - the pressing of the 'Select' button.
The noscript side of things works. I select something in the dropdown, press 'Select' and the rest of the form is rendered on a refresh.
However, the AJAX call does not have the just-selected value in its request. It's as if the AJAX call happens before the Dijit component has updated its representation. Are these components compatible in the fashion I am using them?
I can get it to work if:
I don't decorate the component as a Dijit component and instead just normal dropdown with an onclick AJAX decoration.
I put the AJAX call on a button instead of on the Dijit onChange

Faced the same issue and the workaround for it is setting the selected value in a hidden field and Spring.AjaxEventDecoration takes care of the rest.
<input id="vehicleType" name="vehicleType" type="hidden" />
<select id="selectVehicle">
<c:forEach var="vehicle" items="${vehicleTypes}">
<option value="${vehicle.type}"><fmt:message key="${vehicle.description}" /></option>
</c:forEach>
</select>
</p>
<script type="text/javascript">
Spring.addDecoration(new Spring.ElementDecoration({
elementId : 'selectVehicle',
widgetType : "dijit.form.Select",
widgetAttrs : {
value : "${vehicleType}",
onChange : function(newValue){
document.getElementById('vehicleType').value=dijit.byId('selectVehicle').get('value');}
}
}));
Spring.addDecoration(new Spring.AjaxEventDecoration({
elementId : "selectVehicle",
formId : "quote",
event : "onChange",
params : {
_eventId : "dropDownVehicleSelectionChange",
fragments : "body"
}
}));
</script>

Related

Play framework write Action with Ok(...) that doesn't load new page

Play framework 2.4.x. A button is pressed on my home page that executes some code via Ajax, and returns its results beneath the button without loading a new page. The results wait for a user to input some text in a field and press "submit". Those results Look like this:
<li class="item">
<div>
<h3>Email: </h3>
<a>#email.tail.init</a>
<h3>Name: </h3>
<a>#name</a>
</div>
<div>
<h3>Linkedin: </h3>
<form class="linkedinForm" action="#routes.Application.createLinkedin" method="POST">
<input type="number" class="id" name="id" value="#id" readonly>
<input type="text" class="email" name="email" value="#email" />
<input type="text" class="emailsecondary" name="emailsecondary" value="" />
<input type="text" class="name" name="email" value="#name" />
<input type="text" class="linkedin" name="linkedin" value="" />
<input type="submit" value="submit" class="hideme"/>
</form>
</div>
<div>
<form action="#routes.Application.delete(id)" method="POST">
<input type="submit" value="delete" />
</form>
</div>
</li>
Along with some jquery that slides up a li after submission:
$(document).ready(function(){
$(".hideme").click(function(){
$(this).closest('li.item').slideUp();
});
});
However, since a form POST goes inside an Action that must a return an Ok(...) or Redirect(...) I can't get the page to not reload or redirect. Right now my Action looks like this (which doesn't compile):
newLinkedinForm.bindFromRequest.fold(
errors => {
Ok("didnt work" +errors)
},
linkedin => {
addLinkedin(linkedin.id, linkedin.url, linkedin.email, linkedin.emailsecondary, linkedin.name)
if (checkURL(linkedin.url)) {
linkedinParse ! Linkedin(linkedin.id, linkedin.url, linkedin.email, linkedin.emailsecondary, linkedin.name)
Ok(views.html.index)
}else{
Ok(views.html.index)
}
}
)
Is it possible to return Ok(...) without redirecting or reloading? If not how would you do a form POST while staying on the same page?
EDIT: Here is my attempt at handling form submission with jquery so far:
$(document).ready(function(){
$(".linkedinForm").submit(function( event ) {
var formData = {
'id' : $('input[name=id]').val(),
'name' : $('input[name=name]').val(),
'email' : $('input[name=email']).val(),
'emailsecondary' : $('input[name=emailsecondary]').val(),
'url' : $('input[name=url]').val()
};
jsRoutes.controllers.Application.createLinkedin.ajax({
type :'POST',
data : formData
})
.done(function(data) {
console.log(data);
});
.fail(function(data) {
console.log(data);
});
event.preventDefault();
};
});
This is an issue with the browser's behavior on form submission, not any of Play's doing. You can get around it by changing the behavior of the form when the user clicks submit.
You will first want to attach a listener to the form's submission. You can use jQuery for this. Then, in that handler, post the data yourself and call .preventDefault() on the event. Since your javascript is now in charge of the POST, you can process the data yourself and update your page's HTML rather than reloading the page.
What you need is use ajax to submit a form, check this: Submitting HTML form using Jquery AJAX
In your case, you can get the form object via var form = $(this), and then start a ajax with data from the form by form.serialize()
$.ajax({
type: form.attr('method'),
url: form.attr('action'),
data: form.serialize(),
success: function (data) {
alert('ok');
}
});
In order to accomplish this task, i had to use play's javascriptRouting
This question's answer helped a lot.
I'm not experienced with jquery so writing that correctly was difficult. For those that find this, here is my final jquery that worked:
$(document).ready(function(){
$("div#results").on("click", ".hideme", function(event) {
var $form = $(this).closest("form");
var id = $form.find("input[name='id']").val();
var name = $form.find("input[name='name']").val();
var email = $form.find("input[name='email']").val();
var emailsecondary = $form.find("input[name='emailsecondary']").val();
var url = $form.find("input[name='url']").val();
$.ajax(jsRoutes.controllers.Application.createLinkedin(id, name, email, emailsecondary, url))
.done(function(data) {
console.log(data);
$form.closest('li.item').slideUp()
})
.fail(function(data) {
console.log(data);
});
});
});
Note that my submit button was class="hideme", the div that gets filled with results from the DB was div#results and the forms were contained within li's that were class="item". So what this jquery is doing is attaching a listener to the static div that is always there:
<div id="results">
It waits for an element with class="hideme" to get clicked. When it gets clicked it grabs the data from the closest form element then sends that data to my controller via ajax. If the send is successful, it takes that form, looks for the closest li and does a .slideUp()
Hope this helps

How to validate Laravel 4 form (html) with js framework

I am developed a laravel web application with forms. My client is not satisfied with laravel form validation because its validating from server side and reload the page, its taking more time. I want to validate form from client side.. which js framework is most suitable for laravel 4. I need the laravel form validation methords like min,max,numeric,alphanumeric,alpha. With custom validation error messages.
Also please give the best tutorial link for your suggestion. ?
Have you considered making an AJAX call to the backend, making use of Laravel's form validators, as the user tabs to the next input field? That way the form is validated prior to submit.
I manage the validation from you can use any framework and any CMS try it.
p{color:red;}
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.9/jquery.validate.min.js"></script>
<script type="text/javascript">
(function($,W,D)
{
var JQUERY4U = {};
JQUERY4U.UTIL =
{
setupFormValidation: function()
{
//form validation rules
$("#register-form").validate({
rules: {
roll: {required : true, minlength : 2,digits: true},
lastname: {required : true},
agree: "required"
},
messages: {
roll: {required :"<p>Please enter roll</p>", minlength : "<p>Must be at least 2 characters</p>",digits :"<p>Please enter integer Numper Only</p>" },
lastname: {required :"<p>Please enter lastname</p>" },
agree: "Please accept our policy"
}
});
}
}
//when the dom has loaded setup form validation rules
$(D).ready(function($) {
JQUERY4U.UTIL.setupFormValidation();
});
})(jQuery, window, document);
</script>
<form method="post" action="<?php echo base_url(); ?>classname/functionname/" id="register-form" novalidate="novalidate" />
<input class="form-control" type="text" name="roll" value="">
<input class="form-control" type="text" name="lastname" value="">
</form>

How to remove Dojo Dijit from Spring MVC project

How to remove Dojo Dijit from javascript call
How can I change the following code to use just a html select we dont want to use dijit or dojo.. Can someone please help us out
<script type="text/javascript">
Spring.addDecoration(new Spring.ElementDecoration({
elementId : "borough",
widgetType : "dijit.form.Select",
widgetAttrs : {
promptMessage : "Enter Borough",
required : true,
onChange : function() {
Spring.remoting.submitForm(
'submit',
'member',
{_eventId: 'loadSchools', fragments:'contents'}
);
return false;
} }}));
</script>
Delete the Dojo stuff and repace it with plain old pure Java Script: onchange='this.form.submit()':
<form ...>
<select name='someField' onchange='this.form.submit()'>
<option>A</option>
<option>B</option>
<option>C</option>
</select>
</form>

sending values to database and return the results to same page with Ajax onclick

can one help on how can i submit the form to search page script on index page and return the results of search script on specific DIV id search_results we out going to the search.php or refresh the index page using AJAX onClick
Am not good in Ajax but i try my best to come out with the following code which does what am looking for but is loading the page we out clicking anything i need a user to trigger the event when he/she enters what they are looking for? Any answer or suggestion is greatly appreciate
<script language="javascript" src="js/jquery-8.js"></script>
<script language="javascript">
var grbData = $.ajax({
type : "GET",
url : "search_m.php",
data : "q=",
success: function (html) {
$("#more-info").html(html);
}
});
</script>
<div id="more-info"></div>
I wish the above code to use this following htm form
<form method="get" style="width:230px; margin:0 auto;" id="find">
<input type="image" src="images/searchthis.png" id="search_btn">
<input type="text" id="search_toggle" name="q" placeHolder="type to start searching">
</form>
Add a click event to the search_btn element.
$('#search_btn').click(function(e) {
e.preventDefault();
$.ajax({
type : "GET",
url : "search_m.php",
data : $('#search_toggle').val(),
success: function (html) {
$("#more-info").html(html);
}
});
HTH.

Using Spring Webflow and Dijit it looks like I can only add one Decoration per element (field)?

Using Spring Webflow and Dijit it looks like I can only add one Decoration per element (field)?
I am working on a Spring Webflow project and I was loading a list of schools once the user selects a Borough from a dropdown. The form was working great until I added a dijit.form.Select widgettype to it. Please look at the code below.
Can I use both?
The Issue I am having is on the Spring MVC side the bean now has the value "borough" in the borough field and NOT the value the user entered!
<form:select path="borough" id="borough" >
<form:option value="UNKNOWN" label="Unknown" />
<form:option value="X" label="Bronx" />
<form:option value="K" label="Brooklyn" />
<form:option value="M" label="Manhattan" />
<form:option value="Q" label="Queens" />
<form:option value="R" label="Staten Island" />
<form:option value="O" label="All Other Schools" />
</form:select>
<script type="text/javascript">
Spring.addDecoration(new Spring.ElementDecoration({
elementId : "borough",
widgetType : "dijit.form.Select",
widgetAttrs : {
promptMessage : "Enter Borough",
required : true }}));
Spring.addDecoration(new Spring.AjaxEventDecoration({
elementId: "borough",
event: "onChange",
formId:"member",
params: {fragments:"body", _eventId: "loadSchools"}}));
</script>
I fixed the issue. I removed the Spring.AjaxEventDecoration call and changed the Spring.ElementDecoration to the following:
<script type="text/javascript">
Spring.addDecoration(new Spring.ElementDecoration({
elementId : "sex",
widgetType : "dijit.form.Select",
widgetAttrs : {
promptMessage : "Enter Sex",
required : true,
onChange : function() {
Spring.remoting.submitForm(
'submit',
'customer',
{_eventId: 'sexchange', fragments:'contents'}
);
return false;
} }}));
</script>
I am not 100% clear on why the Ajax call did not work but I have my project working with this code now!

Resources