Play framework get form value - html

I have a form with an accept button and a reject button. When a user presses the accept button I want the value of accepted in my database to contain the value true. If the user presses reject then I want the value of accepted in my database to contain the value false. The code I have below does not work. Are there any suggestions on how I can get this working properly?
public static void acceptOrRejectResponseForm(Long id,String accept,String reject) {
Response responseForm = Response.findById(id);
if(accept != null && !accept.isEmpty()){
responseForm.accepted = true;
}
else if(reject != null && !reject.isEmpty()){
responseForm.accepted = false;
}
accepted.save();
}
This my html
<form action="#Admin.acceptOrRejectResponseForm(response.id)" method="GET">
<input type="submit" value="accept" name="accept">
<input type="submit" value="reject" name="reject">

don`t create two submit buttons.
Create two hidden fields with default value 0. Call name accept and reject. Than create two buttons with onclick action. If you click on accept button with the onclick action you just set the hidden field accept on 1 and reject on 0. When you click reject button you set hidden accept field on 0 and reject on 1. Than you make submit for the form in js. In the code get those two fields and check witch one is set for 1 and witch one is set to 0. That way you can set it in db

Related

Angular 4 : disable button as long as form not validated

I have a form with required fields and 2 buttons : first button submits the form and another button is to download the file.
On the one hand, as long as form is not valid, the submit button is disabled (and it enables when the form is valid) :
component.html :
<button type="submit" [disabled]="!formGroupTest.valid" >Valider</button>
=> It works
On the other hand, as long as form is not valid, the "download" button is disabled and it enables when : form valid AND button submit is clicked :
component.html :
<button [disabled]="!formGroupTest.valid || !buttonSubmitEnabled">Download</button>
For that, in component.ts, i initialize a boolean at false, and it becomes true when button submit clicked (method sendForm()) :
private buttonSubmitEnabled: boolean = false;
sendForm() {
this.buttonSubmitEnabled=true;
}
When I fill the form for the first time, it works perfectly => I click on the submit button and the download button becomes enable. However, after the first time, if I decide to change required fields and it returns the form as invalid, the 2 buttons are disabled (logically), but when I fill the fields correctly then the "download" button becomes enable. I understood the problem, because the 2 conditions to enable the button download are respected : Form valid AND button submit clicked once.
So, what if I want it to work every time, I think it's necessary to put the Boolean "buttonSubmitEnabled" at false each time the form is invalid, but I don't know how to do it.
FormGroup has a valueChanges property that you can subscribe to. Every form change will trigger this observable. You can use this to reset the submission boolean.
this.myForm.valueChanges.subscribe(x => this.buttonSubmitEnabled = false);
In keyup event of those input fields call a function disableDownload()
<input (keyup)="disableDownload()">
And inside disableDownload(), just make download buttonSubmitEnabled false
disableDownload() {
this.buttonSubmitEnabled = false;
}
Only make the buttonSubmitEnabled true once the form is submitted

Toggle switch to pass unchecked value

I'm using a checkbox to create a toggle switch as shown in this tutorial
The switch lives in a form where questions can be added dynamically. On submission the form posts as array of each answer back to the page to be processed however as the off switch doesn't pass a value back to the form the answers get out of sync with the answers for the other text fields. Is there any way to set a value for the off switch, i.e. when a check box is left unchecked?
I've tried to use the following to set my off checkboxes to off however it just seems to animate all the switches to on on form submission, anyone any ideas as to what I'm doing wrong?
$('form').submit(function(e){
var b = $("input:checkbox:not(:checked)");
$(b).each(function () {
$(this).val(0); //Set whatever value you need for 'not checked'
$(this).attr("checked", true);
});
return true;
});
You probably want to use Javascript to set a value for each checkbox "switch" in one of two ways:
Option 1: in the html of the switch elements/checkboxes, set the value attribute to zero by default. Then add a javascript click handler for the toggle to check its current value and toggle to the opposite state/value.
Option 2: add Javascript to the form's submit handler (on submit) that checks for any switch elements which have no values and set them to zero before processing form.
Either way should pass a value at all times, and your form should be able to keep track of all input states.
This snippet did the trick, as Anson suggested this finds all the checkboxes and sets them to either on or off on form submission:
$('form').submit(function () {
$(this).find('input[type="checkbox"]').each( function () {
var checkbox = $(this);
if( checkbox.is(':checked')) {
checkbox.attr('value','1');
} else {
checkbox.after().append(checkbox.clone().attr({type:'hidden', value:0}));
checkbox.prop('disabled', true);
}
})
});

How to clear ng-show validation message on clear button click

I am checking the input field with validation and showing validation message like "contact person field accept only characters" If I click cancel button It will close the popup. Again, If I open the same popup to add new data already shown ng-show validation message is still displaying. Validation Message is not clearing. How to clear ng-show validation messages in cancel button click
I have used form.inputname="" to clear the form input values. But How do I clear
validation messages in angularjs
You can use following code here MyForm is form name. And place Submitted to your ng-show condition with && operator in validation message in html code
$scope.Submitted = false;
$scope.MyForm.$rollbackViewValue();
$scope.MyForm.$setSubmitted();
$scope.MyForm.$setPristine();
$scope.MyForm.$setUntouched();
Try this
<button type ="button" ng-click="hideNotifications()" >Cancel </button>
In Controller
$scope.hideNotifications = function () {
$scope.message = false;
}

checkbox value is not sent in HTTPRequest if unchecked

There is a checkbox in my jsp page
<input type="checkbox" name="myCheck" id="myCheck">
I am submitting the form and on server side,I am trying to retrieve it
using
request.getParameter("myCheck");
If I have checked the checkbox,it is coming in request as true else it is not present in request object.
My Requirement is that It should also come as false if I have not checked it.
Any suggestion?
UPDATE
I am using GET method and yes it is present in the form which I am submitting.
From accepted answer in stackoverflow
How you can know if its checked
Clarification..
For example:
<input type="hidden" name="checkboxNamesList" value="nameCheckbox1" />
<input type="hidden" name="checkboxNamesList" value="nameCheckbox2" />
<input type="hidden" name="checkboxNamesList" value="nameCheckbox3" />
<input type="hidden" name="checkboxNamesList" value="nameCheckbox4" />
Then, you can get checkboxNamesList from the request (it will be a String[]) so you will have all the checkboxes params names. If you get a parameter for one of the checkboxes name and value is null, it means that checkboxe was not checked
Well, as unchecked checkboxes are not present in the request you don't know whose checkboxes in the JSP were not checked but you need to know it in order to write in your data file something like checkbox_name1=unchecked.
So, how to do that? First, you need to know what checkboxes (uncheck or not) were present in the request. For that, you can use the code below and get name of all checkboxes present in the JSP:
String[] checkboxNamesList= request.getParameterValues("checkboxNamesList");
Then, let look for unchecked checboxes:
for (int i = 0; i < checkboxNamesList.length; i++) {
String myCheckBoxValue = request.getParameterValues(checkboxNamesList[i]);
// if null, it means checkbox is not in request, so unchecked
if (myCheckBoxValue == null)
writer.append(checkboxNamesList[i] + "=unchecked");
// if is there, it means checkbox checked
else
writer.append(checkboxNamesList[i] + "=checked");
}
I cannot understand why the value is not in the request object. I think one possible mistake is the missing value attribute.
To your second question:
In your backend you have to define a default value first and then check to null:
public boolean myCheckBox = false;
if (request.getParameter("myCheck") != null) {myCheckBox = true}

Meteor reset value of html input

I currently have an input box, which is inside a modal, setup like the following
<div style="padding-bottom: 8px">
<label for="edit-user-email">Email Address</label>
<input type="email" class="form-control" id="edit-user-email" value="{{getEmail}}">
</div>
And I have a UI helper to fill in the value setup below
UI.registerHelper "getEmail", () ->
editUsersDep.depend()
try
#console.log Session.get("currentUser")
return Session.get("currentUser").email.address
catch e
return
Now when I edit the value of the box, lets say changing the value from test#gmail to test2#gmail and then close my modal without actually updating the value for the currentUser session variable, and then re-open the modal I am expecting/wanting the value in that box to return to test#gmail.com but instead it is staying at test2#gmail.com.
The Session variable is being set as such. this corresponds to a row in a table. That works fine.
"click #foreman-edit-projects": () ->
FlashMessages.clear()
editUsersDep.changed()
supersOfForeman = []
projectsAdded = Meteor.users.findOne({_id: this._id}).on_projects
allSupers = Meteor.users.find({"roles.__global_roles__": {$in: ["admin", "super"]}, username: {$ne: "site.admin"}}).fetch()
for admin in allSupers
if admin.has_foreman
if this._id in admin.has_foreman
supersOfForeman.push(admin._id)
else
continue
Session.set("editSupersForUser", supersOfForeman)
Session.set("editProjectsForUser", this.on_projects)
Session.set("currentUser", this)
$("#editProjectsModal").modal("toggle")
The console.log I put in there to test that the method was actually being called when the modal pops up, which it does, but the value isn't changing. The log also always happens after I open the modal. Any thoughts?
I'm a bit confused by your question. I think you meant to say, "...instead it is staying at test2#gmail.com". If that's right, you might want to edit your question to reflect that.
Also, where are you setting the Session variable?
Assuming that you are trying to get the current user's email address, you can use Meteor.user() rather than constantly trying to set your session variable.
So, you can replace it with:
UI.registerHelper "getEmail", () ->
editUsersDep.depend()
try
return Meteor.user().emails[0].address
catch e
return