tl;dr I want to save the value of a textbox to the model ONLY IF the checkbox is checked.
First off, I know my code is very messy. I've been trying to improvise my code off of another stack overflow question so it is first and foremost wrong. What I am trying to do is I have these restrictions to an event, some are bool and some are numbers. For example, no smoking is a bool, but minimum age is a number. These are optional though, the user doesn't have to set any. So I want to have a checkbox saying that they want the restriction but to also save the value to the model. Here is what I have so far:
<p>
<input type="checkbox" id="ageLowCapCB"
#(((Model.currentRestrictions.AgeLowCap != null) &&
(Model.currentRestrictions.AgeLowCap != 0)) ? "checked = 'checked'" : "")
/>
Low Age Limit
#Html.TextBoxFor(e => e.currentRestrictions.AgeLowCap, new {id = "ageLowCap"})
</p>
and the script/jquery for this I have is:
$("#ageLowCapCB").click(function () {
var isChecked = $(this).is(":checked");
$("#ageLowCap").val(isChecked ? /*I don't know what to put here*/);
});
The question I pulled this from was using values of "T" or "F" for true and false so it was easy for them to fill in the comment section putting "T" : "F" and that was it. But I need to pull the value of the text box and set it to the value of Model.currentRestrictions.AgeLowCap. Does anyone have any good ideas on what to do in a weird situation like this?
Unfortunately, there is on way of passing your javascript values to c# view model. However there is some workarounds. You can create action which will accept whatever you want to pass from javascript to c# and then when you need to pass that value(s), that can be done by making ajax request to that action and in that action you can persist that value(s). From there you have many options like in database, session and etc.
Question is not very clear to my understanding. What I understand is if Checkbox is checked display textbox which accepts integer value. All these are model fields.
Approach 1: Write a JavaScript function to detect if Checkbox checked attr is true, Display textbox to accept input from user; Write function for textbox to accept only integers. Onkeypress you can call that function.
Approach 2: If you do not have to take integer value from user in textbox, then on post validate if checkbox is checked and assign value to integer textbox if it is true.
-Thanks
Related
When using the .each function with JQuery like below is there anyway to automatically group the results by type to save some time;
$(selector).each(function () {
//do stuff here for each found
});
The above will obviously go through each found element one by one but with the logic within my code I'm just detecting what field type the field is and I would action each field type to do the same thing. I'm just trying to save doing all the logic for the same field type over and over again.
To detail a little more, I have a value and it needs to go into may different fields types. Simple text fields are fine as they're just insert in using .val() but for other default and custom fields that I have such as picklists and multi-select boxes I need to do some logic around the value for these fields so the appropriate values are set, but not all these type fields don't exist so don't want to do the necessary logic beforehand if those fields don't actually exist. So if I had 50 picklist fields I would obviously only want to do that logic once and set the values for all picklist fields to this value(s) that the logic had set. I just thought they're might be a simple method of JQuery that I'm missing here?!
You can do some manual testing and run your code a different way
var all = $(selector),
text = all.filter('[type="text"]'),
select = all.filter('select');
text.val(someValue);
if (select.length){ // select elements exist
// do logic and apply to all
select.val(selectSpecificValue);
}
That's the html for the input field:
<input id="sku_input_field" type="text" name="items[0<?php echo $uniqueSuffix; ?>][sku]" class="input-text" />
And the javascript code that I am trying:
$('sku_input_field').value = jsonResponse.sku;
document.getElementById("sku_input_field").value = jsonResponse.sku;
So, you see, I am trying 2 approaches, and it doesn't work. I use FireBug to check and the response is NOT empty. I can see all the values that I am setting into it. The value of the field though still remains the same as the one I am typing.
It is that I type an id, and when the object with that id is found in the database I return a json response with some values (this happens in PHP). And one of these values, in the json response, is the one I want to set as a new value of the input field.
The value of the sku attribute is what I want to set as value of the input field. As you see, the response is not empty.
Should be :
jQuery
$('#sku_input_field').val(jsonResponse.sku);
// Provided jsonResponse.sku is not empty
// Also make sure jquery library is added.
Pure Javascript :
document.getElementById('sku_input_field').value = jsonResponse.sku;
p.s : Please check whether you are using same ID for any other element. ID needs to be unique.
I have a series of various fields on a Dynamics CRM 2011 form. I'm using javascript to carry out various checks. One of these is setting particular fields to "required" to prevent the user saving the form until the field is assigned a value. (Making them mandatory)
Xrm.Page.data.entity.attributes.get("new_FieldA1").setRequiredLevel("required");
However, some of my fields are radio buttons. The above code doesn't seem to work correctly for these. I think a Yes/No selection of "No" is seen as null anyway. Which means if "No" is selected, I still get a message "You must provide a value for FieldA1".
Can anyone suggest a work around so "No" is allowed?
Thanks.
To check mandatory fields, (if particular fields are completed on the form) the user selects a radio button as the last option at the bottom of the form ("Mark this form as complete?") When Yes is selected, the following function carries out some basic checks:
function FormSaveAlert()
{
if (Xrm.Page.getAttribute("new_formcompleted").getValue() == true)
{
if (confirm("Are you sure this form is complete? \n Once saved, this form cannot be modified again.") == false)
{
Xrm.Page.getAttribute("new_formcompleted").setValue(false);
}
else
{
var HF1 = Xrm.Page.getAttribute("new_hf1field");
HF1.setRequiredLevel("required");
if (HF1.getValue()==null) {HF1.setValue(false);}
//Xrm.Page.data.entity.attributes.get("otherfield").setRequiredLevel("required");
}
}
}
If the form is marked as complete, and a certain field is required but contains null, a message appears preventing a save. Default message in Dynamics - "You must provide a value for HF1"
I did a test and I can replicate the issue, if I set a two options to null and after I select No, getValue function returns null instead of false.
(I used only Google Chrome, but because is a supported customization it must works for all browsers)
If inside your requirements is possible (shows No as default value), I suggest to enforce the default value by code as this example:
var fieldA1 = Xrm.Page.getAttribute("new_FieldA1");
fieldA1.setRequiredLevel("required");
if (fieldA1.getValue()==null) { fieldA1.setValue(false); }
A required field must have a value, but a bit field always has a value, so I would actually expect this to be seen as containing a value even when it appears not to.
Can you post the onChange code for x_formcompleted too, since that is what is triggering the error?
You have two fields here, and I can't quite see the logic of how they interact.
Surely you need only one, which if ticked asks them to confirm and if they change their mind it gets reset.
Also consider using an option set with values yes / no / null (there is a built-in global option set for this). Make Null ("unassigned") the default, and make the field required (not by script, just always in the field properties).
I am trying to build simple shopping cart where the user input books information and price, the javascript file would validate the input then after inputting several books info, the user would be able to view the order with books details and total price. The problem is that even if there was an error with the dataentry, the wrong information would be stored and hence I get a NaN for the total price.
In function Validate() you start off with var correct = 'true' and later have an if statement checking if it is still true, however there is no code before that if which could possibly change var correct to a value other than true. I'm guessing you forgot to add correct = 'false' in each of the ifs you have checking the variables.
Also, if you want var correct to be a boolean rather than a string, get rid of the "" around true.
var Price=document.getElementById('Price').value;
if(isNaN(Price)||Price="")
{alert("Please enter numbers only for price");
correct = false;
}
It's never a good idea to perform client side navigation. What if someone has javascript disabled? (Blackberry phones). They will pass your validation, and boom, everything messes up.
As for your question, it would be easier to answer if you could have an isolated test case in a fiddle.
EDIT: As others said, it's simply because you aren't doing correct = false every time in the if statements.
I am on CakePHP 1.2 at the office and, following my last question, I would like to send the array key of the selected option in a SELECT input instead of sending its actual value. I have tried a few things with the Model::beforeSave() function, without success.
I am aware that the data posted by CakePHP does not include the whole array, but only the selected value.
Here is what the function looks like at the moment:
function beforeSave(){
$this->Post->set('category_id', = array_keys($this->data['Annonce']['category_id']);
# debug($this->data);
}
Would there be a way to store the array keys into an hidden input and changing this input value depending on the user's selected item in the SELECT input, and to also omit sending the user's input but still send the hidden value?
$categories = Set::combine($categories,'{n}.categories.id', '{n}.categories.nom');
This did it for me... CakePHP assigns the array_keys() values automatically to the value field of the input.