mvc Html.TextBoxFor - model-view-controller

I have a question here about html.textboxfor
I knew that we can use Html.TextBoxFor to restrict the size of the user input
Html.TextBoxFor(model => model.EmployerNumber, new {size=3})
But can we configure it to take numeric value only ?
I try to get Html.ValidationMessageFor to throw error when input value is not numeric.
Many thanks.

a textbox accepts text so its not really capable of being limited just to numbers. the best way to do what you want to do is have a model set up that only accepts numeric values and then use client side validation to alert the user that only numeric values are acceptable.

Related

Laravel validation; human names for array fields

In Laravel form validation you can do this: file_description.* to validate each item of an array according to a set of rules. The problem is, the system automatically returns "file_description.1 is required" as an error message if the field is required and not filled in.
More on that here: https://ericlbarnes.com/2015/04/04/laravel-array-validation/
Now, I'm not a complex man, I just want the field to say "File Description 1 is required". I am aware you can set messages but a) my input arrays are dynamically generated by jquery (click to add more type scenario) so I'd have to use a loop like in the above example b) I feel like there must be a better way.
Is there a way to either extend the core validation system to simply return a humanized name for the array field as Laravel does with regular fields, or is there an option I missed in the docs that allows for this? I'd rather not get involved with doing some regex type search to fix this.

validation.xml change mask value on condition

I am working on struts server side validation(version 1.2). I have used validwhen several times. I have 2 textboxes that submit to server. text1(value comes here from a drop down selection) can have name or id. text 2 holds its value. I have to write validation on text2. Is there a way where i can change mask var-value in validwhen ? i.e if text1 value is name then text2's mask should be a regex for name(alphanumeric and space). if text1 value is id then text2's mask should be a regex for id(only numbers). Is there a way this can be achieved? I don't think we can write 2 different validation on same field.
thanks,
IMO validation like that belongs on the Java side.
You can either wrap it up in a custom validator if it will be used across the app, or as part of the form's validation method, where you could also combine it with standard XML validation.

Struts 1 Textbox to integer validation

In struts 1 if you try to bind a html:text field directly to an integer in the ActionForm then there isn't a chance to validate it correctly when the user enters a non-numberic value.
If the user enters a non-numberic value, then the integer value is always parsed as 0 before it reaches the validate method.
Is there any way supported way that struts provides to handle this situation? Or do I need to always bind to a String first and then parse into an integer later on?
If I am not mistaken, any thing that comes from the UI is a string, even though you have the variable defined as an integer in your form.
So my suggestion would be to declare the variable as string and parse it according to your need.

Kendo UI Datasource and Arrays

I am sending over a series of array values from a posted form to an MVC3 Controller. I was hoping the default modelbinder would be able to parse this but I'm having some difficulty with it.
The array is in the following format:
order[0].[type]=some value.
I think this is the reason the model binder is not parsing my values because I'm not getting anything populated in my model.
What would be another way to handle this?
Probably need to post more of your code so I can see what you are doing exactly. However saying this you need to pass the model to the view/partial view on the response you are trying to retrieve on the post request.
If not you will have to iterate through the Form Collection that will be returned and the Actions Methods type e.g. ActionMethodName(FormCollection form), one issue is name versus id its the name of the Kendo UI control that is used to get the value not the id.
1As far as I remember the right format was:
orders[0].OrderID=13;
orders[0].Name="test";
orders[1].OrderID=15;
orders[1].Name="again test";
The indexing should start from 0 and increase by 1.
Check this out: http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx

Simplest way to add a hidden field for all properties of a Asp.net MVC 3 model?

I need pass through all values of model to the next controller. I could create hidden fields for all properties of the model, but this is always a bit error prone when the model gets new fields.
I tried this:
#Html.HiddenFor(x => x)
but that just leads to the error
CS1660: Cannot convert lambda expression to type 'string' because it is not a delegate type
(I don't like using Tempdata because its so short lived, and unplaned page reloads)
Is there a simple way to accomplish this?
Why not just pull it from the database? If the user can't change the values then why do you need to send it back and forth? A malicious user can modify the hidden field value and try to post it back to the server. Pulling from the database would be faster and more secure.
You have to add the property you are using in the lambda experssion
#Html.HiddenFor(x => x.Username)

Resources