I currently update my database records with an input like this
$this->my_model->update($id, $this->input->post()));
(This is after I have performed a validation on all the input. The "model" also has a white_list of data to expect.)
Problem: if I am updating a checkbox, and it is "unticked" (i.e. 'false') - then that field is not 'posted' by the browser.
For example - if I tick checkbox_two - but leave checkbox_one unticked, $_POST shows:
[field_one] = "some value"
[field_two] = "some value"
[checkbox_two] = 1
Therefore my model will not update that field - since it is not part of the post.
The same does not happen in reverse - because a "ticked" checkbox is posted as "1" - and thus is correctly updated.
[field_one] = "some value"
[field_two] = "some value"
[checkbox_one] = 1
[checkbox_two] = 1
Question: Does anyone have an elegant solution to handle this, other than having to always specifically check for each checkbox?
<input type="hidden" name="example" value="FALSE" />
<input type="checkbox" name="example" value="TRUE" />
This practice is easy and logical. If checkbox is checked, $this->input->post('example') == 'TRUE' else, $this->input->post('example') == 'FALSE'.
The latest given value of a name="" overrides previous, thus givin prio checkbox > hidden.
EDIT: this solution is equal to given most rated answer in Rick Calder's comment. I've also come to this conclusion on my own, though.
Related
Current I am trying to metadata binding xml, following this blog. When I did the maxLength of Input. But I got the following error.
error screenshot
demo service
init model with destination:
initModel: function() {
var sServiceUrl = "/odsrv/V2/Northwind/Northwind.svc/";
var oModel = new OM(sServiceUrl, true);
this.setModel(oModel, "oRefModel");
sap.ui.getCore().setModel(oModel, "oRefModel");
}
xml view:
<content>
<Label text="{oRefModel>/#Category/CategoryName/#type}"/>
<Input maxLength="{oRefModel>/#Category/CategoryName/#maxLength}"/>
</content>
The Label for type works fine if remove Input.
How to solve this problem...
Version with expression binding could be like that
<Input maxLength="{= isNaN(${oRefModel>/#Category/CategoryName/#maxLength}) ? 0 : parseInt(${oRefModel>/#Category/CategoryName/#maxLength})" />
typeof check is needed cause at the start of binding process value of property probably is 'NaN' and it gives an error and whole process will stop.
If you can improve that version please do :)
<Input maxLength="{parts:[{path:'oRefModel>/#Category/CategoryName/#maxLength'}],formatter: 'your.formatter.toNum' }" />
Formatter Code
toNum : function(maxlen){
return parseInt(maxlen);;
}
Converting string to integer is the key
Even shorter and without writing a custom formatter function: Use the built-in parseInt function.
<Input
maxLength="{
path: 'oRefModel>/#Category/CategoryName/#maxLength',
formatter: 'parseInt'
}" />
For some reasons expression binding does not work, maybe someone could tell me why:
<Input maxLength="{= parseInt(${oRefModel>/#Category/CategoryName/#maxLength}) }" />
I have the following jsp code which uses struts tag:
<input type = "radio"
id = "<s:property value="name"/>"
name = "<s:property value="name"/>"
value = "<s:property value="value"/>"
<s:if test="fieldValue==null">
<s:if test="defaultOption==true">
checked="checked"
</s:if>
</s:if>
<s:else>
<s:if test="value==fieldValue">
checked="checked"
</s:if>
</s:else>
/>
Eclipse says that:
Start tag (input) not closed properly, expected >.
But I cannot find where is the error. Please help.
It is probably an eclipse bug because with Netbeans there is no error
if you dont want to see the error with Eclipse (and in that case only) :
in the first if, put test like that :
fieldValue==null && defaultOption==true
and get rid of the internal if
it should be the same
PS : try to find a way to use s:radio maybe
Eclipse complains because it does not accept tags (for example <s:if>) inside input tag, you can deactivate JSP validation as suggested in comments .
Go to project properties and uncheck JSP Content Validator, then clean your project
If you don't want to disable validation, here is a suggested solution:
<s:if test="fieldValue==null">
<s:if test="defaultOption==true">
<input type = "radio" id = "<s:property value="name"/>" name = "<s:property value="name"/>" value = "<s:property value="value"/>" checked="checked" />
</s:if>
</s:if>
<s:else>
<s:if test="value==fieldValue">
<input type = "radio" id = "<s:property value="name"/>" name = "<s:property value="name"/>" value = "<s:property value="value"/>" checked="checked" />
</s:if>
</s:else>
Just as a note, your JSP code give same result(I guess it's just for illustration :) ).
I am trying to do multiple things based on the values of my checkboxes. I have this on the htlm form :
<input id="one" checkbox" type="checkbox" value="1">
<input id="two" checkbox" type="checkbox" value="2">
And in Sinatra
post '/process-data' do
one = params[:one]
two = params[:two]
if one = "1"
"One"
end
if two = "2"
"Two"
end
end
I am unable to get the result from second if statement, my question is, how do i get Sinatra to be able to get the values from the second if.
try:
"One" if one == "1"
"Two" if two == "2"
You're using = rather than == or ===.
I have a field with a proper label that I can fill in with capybara without a problem:
fill_in 'Your name', with: 'John'
I'd like to check the value it has before filling it in and can't figure it out.
If I add after the fill_in the following line:
find_field('Your name').should have_content('John')
That test fails, although the filling just before worked as I've verified by saving the page.
What am I missing?
Another pretty solution would be:
page.should have_field('Your name', with: 'John')
or
expect(page).to have_field('Your name', with: 'John')
respectively.
Also see the reference.
Note: for disabled inputs, you'll need to add the option disabled: true.
You can use an xpath query to check if there's an input element with a particular value (e.g. 'John'):
expect(page).to have_xpath("//input[#value='John']")
See http://www.w3schools.com/xpath/xpath_syntax.asp for more info.
For perhaps a prettier way:
expect(find_field('Your name').value).to eq 'John'
EDIT: Nowadays I'd probably use have_selector
expect(page).to have_selector("input[value='John']")
If you are using the page object pattern(you should be!)
class MyPage < SitePrism::Page
element :my_field, "input#my_id"
def has_secret_value?(value)
my_field.value == value
end
end
my_page = MyPage.new
expect(my_page).to have_secret_value "foo"
If you specifically want to test for a placeholder, use:
page.should have_field("some_field_name", placeholder: "Some Placeholder")
or:
expect(page).to have_field("some_field_name", placeholder: "Some Placeholder")
If you want to test the user-entered value:
page.should have_field("some_field_name", with: "Some Entered Value")
I was wondering how to do something slightly different: I wanted to test whether the field had some value (while making use of Capybara's ability to re-test the matcher until it matches). It turns out that it's possible to use a "filter block" to do this:
expect(page).to have_field("field_name") { |field|
field.value.present?
}
If the field is a hidden one with an id of 'some_field', then you can use
expect(find("input#somefield", :visible => false).value).to eq 'John'
This the easiest way:
expect(page).to have_field("name", with: "your name")
<input type="text" id="name" />
I have a form for users to input quantities. The form has client-side validation to ensure that the value is an integer and within a given range. The action page has server-side validation to ensure that the value is an integer and greater than zero.
However, one type of value gets through the validation and is causing my INSERT/UPDATE queries to throw exceptions. That value is an integer with a plus-sign - ie "7+" or "12+".
When such a value is entered, the ColdFusion-generated JavaScript validation throws a JavaScript error:
_CF_checkformAddToCart = function(_CF_this)
{
//reset on submit
_CF_error_exists = false;
_CF_error_messages = new Array();
_CF_error_fields = new Object();
_CF_FirstErrorField = null;
//form element itemQuantity 'INTEGER' validation checks
if (!_CF_checkinteger(_CF_this['itemQuantity'].value, false))
{
_CF_onError(_CF_this, "itemQuantity", _CF_this['itemQuantity'].value, "Error on itemQuantity, please enter an integer value for quantity that is not greater than 500");
_CF_error_exists = true;
}
//form element itemQuantity 'RANGE' validation checks
if (!_CF_checkrange(_CF_this['itemQuantity'].value, 0.0,500.0, false))
{
_CF_onError(_CF_this, "itemQuantity", _CF_this['itemQuantity'].value, "Error on itemQuantity, please enter an integer value for quantity that is not greater than 500");
_CF_error_exists = true;
}
}
Once I cancel out of the error popup, it goes to the action page, where I [try to] validate the value like so:
<cfif IsValid("integer", form.itemQuantity) AND form.itemQuantity GT 0>
<cfquery>
INSERT ....
However, if try this...
<cfset x = Int("7+") />
...ColdFusion throws an error.
Is it an integer or not ColdFusion???
How can get around this and validate my form input correctly?
isNumeric(form.itemQuantity) will return false for "7+", so to fully validate your input as an int, you can do this
<cfif isNumeric(form.itemQuantity) and IsValid("integer", form.itemQuantity) AND form.itemQuantity GT 0>
Due to the weird and wonderful nature of ColdFusion being typeless. It doesn't know what type of data you are working with and it tries to guess.
Its evaluating that 7+ is a valid. The validation built into ColdFusion makes a lot of assumptions and guesses.
My advise would be to not use it and to write your own validation routines that can be enhanced to do whatever you require.
For example
A user enters
2,075
Is this valid or invalid. Well if you have your own validation you can decide, you can say sure this is an integer and remote the , or you can say no they can't do that.
It's a small investment upfront that will pay off in the long run.
Turns out I can use LSParseNumber() to convert it to valid integer. So, now I'm testing for a valid integer, then resetting it using LSParseNumber() before attempting any database inserts:
<cfset addItemQty = 0 />
<cfif IsValid("integer", Trim(form.itemQuantity))>
<cfset addItemQty = LSParseNumber(Trim(form.itemQuantity)) />
</cfif>
I guess I'll have to re-engineer the front-end client-side validation to properly validate.