I am using an EditForm component with validations in Blazor WASM app.
The problem I have is that if I enter an input to InputText and then later decide remove it, the form acts as if I entered empty value. The consequences are either
The field turns red since the value is invalid due to constraints such as [MinLength]
The field turns green if there is no constraint on length
I would like it to act the same as if the input was never entered. So, basically, skip the validation. In ideal world, I would also like to have null instead of empty string in the bind model, but that is not as critical, since I can arrange that in submit action.
Any ideas how to simply achieve this?
Related
I'm not authorized to show the code but I have a problem:
When using the recording feature of CUIT on VS 2015, The test yields an error part way through the playback.
A date entry field is a masked input string field like this "MM/DD/YYYY HH:MM". You can type the values freely into the field. The issue is when doing playback, CUIT attempted to enter the string value of what is captured in the control's final state as "05/09/2017 12:42". The "/" and ":" of the string's value causes the cursor to tab through the masked input, resulting in an erroneous entry. The actual string required to account for all of the tabbing is literally "05///09///2017 12::42" but when I use that hard-coded value, it errors out while attempting to check for the longer version. States that it can't set the control to that value.
Is there a way to tell the CUIT to evaluate an overridden value so that it doesn't try to enter the string stored within the control which contains "/" and ":"?
You need to modify the value in the ...ExpectedValues class that holds the recorded date-time. Coded UI sends the recorded characters (or more accurately, the values from the ...ExpectedValues class) to the application and the application you are testing adds the / and : characters in the approprate places. The Coded UI recorder records both the entered and the generated characters.
Change the recorded 05/09/2017 12:42 value to be 05092017 1242. This can be done via the UI Map editor if the same date-time is always needed. Commonly the date-times are provided via the data source of a data driven test, or they are generated by the test itself. In either case it should be easy to provide data without the / and : or to add code to remove them before they are used. The wanted values are then written, when the test runs, into the ...ExpectedValues class.
See here for some additional notes on the ...ExpectedValues class and on data driving tests.
I'm struggling on logic here - can i get some ideas please! :)
basically i have a c# MVC3 application that accepts a huge amount of text (100+ text areas), modifies it, and then outputs it.
i want to check the length of the combined text boxes and have the process fail validation if they are over X length.
the only solution i can think of is by combining all the text into a string server side and get the length. I'm expecting my competitors to fully abuse the system and attempt to overload my servers when i go live, so i want to try and validate on the client side too if possible.
can anyone think of an efficient way to do this (client or server-side if you have a nice idea).
You could use maxlength css property or you could decorate your model with [StringLength] data annotation to check length of the string
Build a custom validator using a technique similar to this answer by Daron Dimitrov. That will do the check on both client and server side and you can use a ViewModel to decorate the attribute to apply to all of the inputs.
Consider a data entry form where the user needs to enter times in military format. They wish to key from a keypad to speed up their work but don't want to key the colon (:) between the hours/minutes.
The column is formatted to allow time and will automatically convert the time to the standard AM/PM formats. Unfortunately, it requires the colon. Therefore a conversion method should be run on this input to attempt to convert it to a time value.
The method to do this is not part of the answer I'm looking for. It could represent any reason why someone would want to change a value keyed by a user into some other value. The key is that it needs to happen before validation so the DataError event is not raised.
I assumed that CellValidating would be the right place for this so I wrote what I expected to work:
private void sampleDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
// Ensure we're looking at the column that requires our attention
if (sampleDataGridView.Columns[e.ColumnIndex].Name == "timeCol")
sampleDataGridView[e.ColumnIndex, e.RowIndex].Value = convertMilitary(e.FormattedValue);
}
When stepping into this code after the cell is written to, the event args tell me that the formatted value is indeed what was keyed, but attempting to set this to the current value of the cell doesn't help validation. On closer inspection I learned that the value is still what it was prior to user entry.
This leaves me at an impasse because I don't see where else I would put this. I feel that I'm in the right spot but I'm missing something. Where do I put this conversion so the DataError event isn't raised from a failed validation?
Its possible that there is a different property I need to set? Its like I should be changing what the user keyed, not the cell value directly like I am attempting to do...
Have you looked at using the CellParsing event? I seem to recall using this when I needed to do something similar.
say I have a textBox and a property to get and set its value:
public SomeText
{
get { return HttpUtility.HtmlEncode(textBox.Text); }
set { textBox.Text = HttpUtility.HtmlEncode(value); }
}
I have used HtmlEncode to prevent Javascript injection attacks. After thinking about it though I'm thinking I only need the HtmlEncode on the getter. The setter is only used by the system and can not be accessed by an external user.
Is this correct?
A couple points;
First:
You should really only encode values when you display them, and not any other time. By encoding them as you get the value from the box, and also when you paste in, you could end up with a real mess, that will just get worse and worse any time someone edits the values. You should not encode the values (against HTML/Javascript injection - you DO need to protect against SQL injection, of course) upon saving to the database in most cases, especially if that value could later be edited. In such a case, you actually need to decode it upon loading it back... not encode it again. But again; it's much simpler only to encode when displaying it (which includes displaying for editing, btw)
Second:
HtmlEncode protects against injecting HTML - which can include a <script> block which would run Javascript, true. But this also protects against generally malicious HTML that has nothing to do with Javascript. But protecting against Javascript injection is almost a different thing; that is, if you might ever display something entered by the user in, say, a javascript alert('%VARIABLE'); you have to do a totally different kind of encoding there than what you are doing.
Yes. You only need to encode strings that you have accepted from the users and you have to show inside your pages.
I have a winform app with a TimeSpan column that displays the hours/minutes part of a date. When the user enters text it is converted to a TimeSpan using TimeSpan.TryParse(). This works as expected when the user input is "11:00" in setting a value of 11 hours. The problem is that if the use enters "1100" it is parsed as 1100 days which is not what I want, nor is simply saying "bad input" in the `CellValidating event satisfactory behavior.
The users input is provided in the readonly property DataGridViewCellValidatingEventArgs.FormattedValue so I can't change the value being passed through the call chain. DataGridViewTextBoxCell.EditedFormattedValue is also read only and I can't find any other event or property that lets override the default behavior.
This is very frustrating. I can write a many stepped fall through validater that can handle multiple user input formats and get the intended value from each; but unless I throw away all the strongly typed data binding that the framework offers and instead create a shim object that stores all values as strings there doesn't seem to be any way to do so.
Somehow among the 10 billion events in the DataGridView I managed to overlook CellParsing. Overriding it lets me do what I need to do.