Range Attribute in MVC 5 not working as expected - asp.net-mvc-5

I am learning MVC (version 5) by doing a sample project. Hence I am not using any scaffolding and doing everything from scratch. I have a View Model, which has a Password field with the following validations.
[Display(Name = "Password")]
[DataType(DataType.Password)]
[Required(ErrorMessage = "{0} is Required.")]
[Range(1, 4, ErrorMessage = "{0} has to be between {1} and {2} characters.")]
public string Password { get; set; }
Now in my .cshtml, I have a password textbox rendered using #Html.EditorFor. Since I am learning, I don't have any css added. I have added references to jquery-1.10.2.js, jquery.validate.js and jquery.validate.unobstrusive.js in the cshtml file. The validations are working properly, except for the above password field. If I enter only 1 character in the password textbox, validation passes, whereas anything more than that fails, which is strange. I am not able to understand this. Can anyone please help me with this?

As you are using this as string please use below method
[StringLength(4, MinimumLength = 1, ErrorMessage = "{0} has to be between {1} and {2} characters.")]
What you have done is specified value in range so when you put 1 to 4 in the field its valid if you put 5 its not valid because it exceeds the range.
When you need length not the actual value you should use string length on property.
Hope this will solve the problem

Related

Why is my PXFormula with PXDBShorts Throwing Specified Cast Error?

I have a formula I am using the calculate a non-persisting field. I'm redefining a base DAC field: POVendorItem.VLeadTime. This is my new definition:
#region VLeadTime
[PXShort(MinValue = 0, MaxValue = 100000)]
[PXUIField(DisplayName = "Vendor Lead Time (Days)", Enabled = false)]
[PXFormula(typeof(Sub<Current<usrCFAvgLeadTime>, Current<POVendorInventory.addLeadTimeDays>>))]
public virtual short? VLeadTime { get; set; }
#endregionv
Both fields in the formula are defined as PXDBShorts, yet I keep getting a "Specified cast is not valid" error when trying to access the grid the field is on. Here is the trace:
I've also noticed that if I type out a statement such as short? = short? - short? I also get an error in VS stating I can't convert an int? to a short? which doesn't make sense. For me to correctly calculate it, VS auto corrects the formula to short? = (short?)((short)(short?) - (short)(short?)) adding in a ton of type casts.
To get around the formula, I would attach events to field defaulting for VLeadTime, as well as field updating for usrCFAvgLeadTime and addLeadTimeDays and then recalculate it and set the value. I know it is redundant, but as long as you can set the value to a short in visual studio in the code, it would set for VLeadTime.

SSRS 2016 Report - Populate table from string parameter

Let's assume I have the following C# object:
public class BusinessDetails
{
public string Name { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
}
Long story short my solution [A] sends a serialised (JSON) List<BusinessDetails> to another external solution [B] that in turn feeds the RDL in question with this and other provided parameters (strings); therefore, on the RDL level I am limited to strings and strings only.
This is all good for simple, one value string parameters but I am interested in dynamically populating table from (JSON 'flattened'/string) List<BusinessDetails> in the mentioned RDL.
I see the available parameter types are:
Text
Boolean
Date/Time
Integer
Float
I guess my question is - can I use the Text/string param to dynamically populate a table in RDL?
Thanks.
short anser NO!
long answer: you dont need that, using the report viewer control you can use the list directly as datasource, when using rdlc instead of rdl you can even consume the class in the reportdesigner, you dont have to come up with ways to align your data source mapping with the data you are recieving
read this for further info: Creating a PDF from a RDLC Report in the Background

Integer getting default value 0 where it required to be NULL in Java [duplicate]

This question already has an answer here:
h:inputText which is bound to Integer property is submitting value 0 instead of null
(1 answer)
Closed 7 years ago.
My requirement looks simple, but I feel I'm doing something wrong in code.
All I need is to insert "null" in Oracle Database when nothing is passed from InputText field of UI.
PaymantSpecFormPage.xhtml
<h:inputText id="startWeek" value="#{flowScope.paymentSpecVO.paymStartWk}" styleClass="inputTypeForm" maxlength="4"/>
PaymentSpecVO.java
public class PaymentSpecVO extends BaseVO<PaymentSpec> {
private Integer paymStartWk;
public Integer getPaymStartWk() {
return paymStartWk;
}
public void setPaymStartWk(Integer paymStartWk) {
this.paymStartWk = paymStartWk;
}
}
And in my DB Table
COLUMN_NAME DATA_TYPE NULLABLE DEFAULT_VALUE
------------ --------- -------- -------------
PAYM_START_WK NUMBER(4,0) Yes null
And when I enter nothing in inputText, then instead of null, 0 is getting entered in Table, which has different meaning in my business logic, please help me to do necessary code changes.
And when I enter nothing in inputText, then instead of null, 0 is getting entered in table
This problem is recognizable as Tomcat's EL parser not taking into account that you're using Integer instead of int. You need to add the following VM argument to Tomcat startup options:
-Dorg.apache.el.parser.COERCE_TO_ZERO=false
See also:
h:inputText which is bound to Integer property is submitting value 0 instead of null
Work around for faulty INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL in Mojarra JSF 2.1
It solved my half of problem, and now its storing null values, but while on retrieve it shows 0 again
This problem is recognizable as JDBC under the covers using ResultSet#getInt() instead of ResultSet#getObject(). This is however essentially a different problem than the first one. You didn't tell anything about how your database access layer is setup, so it's hard to point out a concrete solution.
I don't know how you are reading that xhtml.
At java layer, you can override either the constructor of PaymentSpecVO or the setter for paymStartWk
Try this
public class PaymentSpecVO extends BaseVO<PaymentSpec> {
private String paymStartWk;
public String getPaymStartWk() {
return paymStartWk;
}
public void setPaymStartWk(Integer paymStartWk) {
this.paymStartWk = paymStartWk+"";
}
}
But then you might be needing a lot of parsing there.
Edit*
If you enter nothing to your "input Text" the passed value is null but since you're catching it with an Integer, it interprets null to 0.
So when you save it, even after manipulating methods and variables to null , it will store "0" , because basically thats the default value.
Another solution I think is making a custom class that stores Integers
Example :
public class IntegerVal extends Number {
private Max_val = 10000000;
private Min_val = -10000000;
private String default = null;
}
you can do whatever implementations you want ,, like returning null,
You can use that as a substitute to the Integer Class you are using , if you dont wanna use String

BDD: SpecFlow - Scenario Outline Behaviour Not As Expected

Using scenario outlines in SpecFlow, e.g.
Scenario Outline: Invalid Login Details
Given some pre-conditions...
When user "Larry" enters username <username> and password <password>
Then the message "Invalid Login Details" should be displayed
Examples:
|username|password|
|larry06 | |
|larry06 |^&*%*^$ |
|%^&&** |pass123 |
| |pass123 |
I expected that the "When" step would be evaluated as such:
public void WhenUserEntersUsernameAndPassword(String username, String password){}
And that the scenario would be run 4 times - for each row of the table, passing the values as required. That's not the case.
Instead, SpecFlow creates 1 of 4 required step definitions:
[When(#"""(.*)"" provides the following new username larry(.*) and password ")]
public void WhenUserEntersUsernameLarryAndPassword(string p0, int p1)
{
//TODO
}
And to get the remaining 3 to 'work' I need to manually write methods explicitly matching other values in the table.
I've since realised that I can just say:
When "Larry" enters username "<username>" and password "<password>"
And I get:
[When(#"""(.*)"" provides the following ""(.*)"" and ""(.*)""")]
public void WhenUserEntersUsernameAndPassword(string p0, string name, string pass)
{
//TODO
}
Perfect.
But all documentation seems to suggest I don't need "" and that should just work (e.g. https://github.com/cucumber/cucumber/wiki/Scenario-outlines). I note:
"Your step definitions will never have to match a placeholder. They will need to match the values that will replace the placeholder"
I just don't really see the value of writing separate step definitions for each line of the table.
Is this nuance specific to SpecFlow?
When "Larry" enters username <username> and password <password>
will match
[When(#"""(.*)"" enters username (.*) and password (.*)")]
public void WhenEntersUsernameAndPassword(string p0, string name, string pass)
{
//TODO
}
so the documentation is fine.
The problem you experienced is that the auto generation of step text doesn't anticipate what regexs to insert without the "..." - which I guess it must be using as an indicator that you are passing interchangable strings - matchable with "(.*)", but if you don't want the quotes, you can still manually correct it just fine.

SubSonic How to Execute a SQL Statement?

My site is using Subsonic 2.2 on my site.
I have 1 weird situation where I need to run some ad-hoc SQL statements.
public IList<string> GetDistincList(string TableName, string FieldName)
{
string sqlToRun = string.Format("SELECT DISTINCT {0} FROM {1} ORDER BY {0}", FieldName, TableName);
Query query = new Query(TableName);
query.PleaseRunThis(sqlToRun);
query.ExecuteReader();
}
Can anyone help me here? As it appears, I just want to return a generic list of strings.
Thanks!
Subsonic has a great method called ExecuteTypedList() so you can do somethink like this.
List<int> result = DB.Select(Table.Columns.Id)
.Distinct()
.From<Table>()
.OrderBy(Table.Columns.Id)
.ExecuteTypedList<int>();
or even with pocos:
public class UserResult
{
public int Id {get;set;}
public string Name {get;set;}
}
List<UserResult> users = DB.Select(
User.Columns.UserId + " as Id", // the as ... is only needed if your
User.Columns.UserName + " as Name" // column name differs from the
).From<User>() // property name of your class
.ExecuteTypedList<UserResult>();
Unfortunately this method doesn't work for string since it requires
a) a valuetype
b) a class with a parameterless constructor since the method uses reflection to map the columns from the result to the properties of the class
However I wrote an extension method a while ago that works for string:
Use the Subsonic.Select() ExecuteTypedList Method with String
Look at my own answer in the link.
If you add the extensionmethod to your code you can do:
List<String> result = DB.Select(User.Columns.UserName)
.From<User>()
.ExecuteTypedList();
Use the CodingHorror class.
Here's the SubSonic 3 way of doing it: http://www.subsonicproject.com/docs/CodingHorror
The SubSonic 2 way is similar:
Dim ch As SubSonic.CodingHorror
ch.Execute("delete from #tablename", table)

Resources