MaxLength attribute and unobtrusive validation - jquery-validate

If I add a MaxLengthAttribute to a property like this:
[MaxLength]
[DataType(DataType.MultilineText)]
public string Notes { get; set; }
The markup is rendered like this:
<textarea class="form-control" data-bind="value: Notes" data-val="true" data-val-maxlength="The field Notes must be a string or array type with a maximum length of '-1'." data-val-maxlength-max="-1" id="Notes" name="Notes"></textarea>
and the validation result looks something like this:
This is obviously not the intended result. The text area should allow A LOT more characters than '-1'.
I can think of multiple ways of addressing this (e.g. removing the attribute via jQuery, manually updating the rule with javascript, etc).
What is the most elegant way of addressing this issue? Is this a bug with MVC/Validator

You are not specifying the desired maximum length of the string, what do you expect? This overload of the MaxLength attributes takes an integer as argument which specifies the actual maximum allowed length of the string. When you use the parameter-less constructor it will use the maximum length allowed by the database, not sure why jQuery Validation chose to implement this as -1 though.

Related

Is it possible in XPATH to find an element by attribute value, not by name?

For example I have an XML element:
<input id="optSmsCode" type="tel" name="otp" placeholder="SMS-code">
Suppose I know that somewhere there must be an attribute with otp value, but I don’t know in what attribute it can be, respectively, is it possible to have an XPath expression of type like this:
.//input[(contains(*, "otp")) or (contains(*, "ode"))]
Try it like this and see if it works:
one = '//input/#*[(contains(.,"otp") or contains(.,"ode"))]/..'
print(driver.find_elements_by_xpath(one))
Edit:
The contains() function has a required cardinality of first argument of either one or zero. In plain(ish) English, it means you can check only one element at a time to see if it contains the target string.
So, the expression above goes through each attribute of input separately (/#*), checks if the attribute value of that specific attribute contains within it the target string and - if target is found - goes up to the parent of that attribute (/..) which, in the case of an attribute, is the node itself (input).
This XPath expression selects all <input> elements that have some attribute, whose string value contains "otp" or "ode". Notice that there is no need to "go up to the parent ..."
//input[#*[contains(., 'otp') or contains(., 'ode')]]
If we know that "otp" or "ode" must be the whole value of the attribute (not just a substring of the value), then this expression is stricter and more efficient to evaluate:
//input[#*[. ='otp' or . = 'ode']]
In this latter case ("otp" or "ode" are the whole value of the attribute), if we have to compare against many values then an XPath expression of the above form will quickly become too long. There is a way to simplify such long expression and do just a single comparison:
//input[#*[contains('|s1|s2|s3|s4|s5|', concat('|', ., '|'))]]
The above expression selects all input elements in the document, that have at least one attribute whose value is one of the strings "s1", "s2", "s3", "s4" or "s5".

Square sq-payment-form Money amount not populating

the example form(C#), sq-payment-form hard codes the amount to charge as
Money amount = new Money(100, Money.CurrencyEnum.USD); I believe I need to change the 100 to reference the charge value in the form. however, I can't find any information on how to accomplish.
I have added a field to the form id "tc"
<div class="sq-field">
<label class="sq-label sq-field--in-wrapper">Total Charge</label>
<input class="sq-field" type="text" id="tc" />
<div id="sq-amount"></div>
</div>
and put an alert in the .js file to display tc.value
i see the value in the alert but in the onpost method i cannot seem to reference the field using response.form("tc")
I have tired referencing both
string tcharge = Request.Form["sq-amount"].;
string tcharge = Request.Form["tc"]();
to no avail..... all the examples on the square site i have found hard code the charge amount. one of the examples on the Square site actually has a comment as follows;
"// Monetary amounts are specified in the smallest unit of the applicable currency.
// This amount is in cents. It's also hard-coded for $1.00,
// which isn't very useful.
Money amount = new Money(100, Money.CurrencyEnum.USD);"
what am I missing
both the form objects are null so the transaction fails. I am fairly new to C# and would appreciate any assistance.
If you're wanting the customer to be able to enter a value and pass it to your backend to charge, you would just need to implement another form field (like the hidden card nonce). For example:
<input type="text" name="sq-amount">
When the form is submitted, you would be able to retrieve the value that was entered like you have above (you would probably want to add in some checks to ensure it's a valid amount etc):
string tcharge = Request.Form["sq-amount"].;
...
Money amount = new Money(Int32.Parse(tcharge), Money.CurrencyEnum.USD);"

ASP.NET MVC 3.0 automatically adding rows and cols attribute

We have some code that calls the Html.TextArea(string name, IDictionary<string, object> htmlAttributes) extension method. This method is adding rows="2" cols="20" automatically. I see in Reflector that these are internal values (part of an implicitRowsAndColumns dictionary).
Is there a way to force ASP.NET MVC to not output these attributes? I don't understand why their code would do this in the first place since CSS is a much better way to establish the size of a textarea.
Try including new { rows = "", cols = "" } to your TextAreaFor call.
It is likely because validators require the rows and cols attributes. But just because they are required does not mean they need to have a value.
stackoverflow.com/questions/2649283/avoid-textarea-rows-cols-error

Model Validation to allow only alphabet characters in textbox

How can I annotate my model so I can allow only alphabets like A-Z in my text-box?
I know that I can use regex but can anyone show how to do that on text-box property itself using data annotation.
You could annotate your model like this:
[RegularExpression(#"^[a-zA-Z]+$", ErrorMessage = "Use letters only please")]
string TextBoxData {get; set;}
Then in your view you would use the helper
#Html.EditorFor(model => model.TextBoxData)
#Html.ValidationMessageFor(model => model.TextBoxData )
You can use annotations for regular expression validation (if i understood your questions), something like that
[RegularExpression("[a-zA-Z]",ErrorMessage="only alphabet")]
You could write like this
It matches First character must be an alpha word
and following that matches any number of characters/hyphen/underscore/space
[RegularExpression(#"^[a-zA-Z]+[ a-zA-Z-_]*$", ErrorMessage = "Use Characters only")]

Xpath: find an element value from a match of id attribute to id anchor

I would like to find the value of an element matched on id attribute for which I only have the ref - the bit with #, the anchor.
I am looking for the value of partyId:
< party id="partyA" >
< partyId >THEID< /partyId >
but to get there I only have the href from the following
< MyData >
< MyReference href="#partyA" />
Strip the # sign does not look good to me.
Any hints?
Because you haven't provided complete XML documents, I have to use // -- a practice I strongly recommend to avoid.
Suppose that
$vDataRef
is defined as
//MyData/MyReference/#href
and its string value is "#partyA", then one possible XPath expression that selects the wanted node is:
//party[#id=substring($vDataRef,2)]
In case the XML document has a DTD in which the id attribute of party is defined to be of type ID, then it is more convenient and efficient to use the standard XPath function id():
id(substring($vDataRef,2))
Assuming you have your ID as a variable already (lets say $myId), then try using:
//party[contains($myId, #id)]
The contains() function will look to see on each matching node whether or not the partyId attibute is in the value that you pass in.
Alternatively (as that could be considered 'ropey'), you can try:
//party[#id=substring($myId, 2, 1 div 0)]
the substring() function should be a little more precise.

Resources