[Bug]: Autocomplete validation within DataGrid appears to not work
Blazorise Version
1.4
What Blazorise provider are you running on?
Bootstrap5
Link to minimal reproduction, or a simple code snippet
I'm in an Edit template. The below works and validates (I'm using UseValidation="true")
<Validation Validator="ValidationRule.IsNotEmpty" >
<TextEdit @bind-Value="swe"></TextEdit>
</Validation>
But the below does not... it doesn't seem to use the validation at all. The ValueField is an Int32 if that helps?
Steps to reproduce
(not sure why but it's stripipng out the validation tags when I post this, however, it's the autocomplete wrapped in a validation tag) <Validation Validator="ValidationRule.IsNotEmpty" ><Autocomplete Data="Clients" Filter="AutocompleteFilter.Contains" HighlightSearch="true" FreeTyping="false" MinLength="0" @bind-SelectedValue="@context.CellValue" Placeholder="Select client..." TextField="x => x.Name" ValueField="x => x.Id"/></Validation>"
What is expected?
hoping the autocomplete would get validated too
What is actually happening?
the autocomplete is not validated and the Grid attempts to save, resulting in an error
What browsers are you seeing the problem on?
Chrome
Any additional comments?
The Field for the DataGridColumn is bound to an Int32
I think that ValidationRule if reserved for actual empty strings. You will need to provide your own ValidationRule that checks ints.
https://blazorise.com/docs/components/validation
The first example shows a method an implementation of a custom validation named ValidateEmail
Thanks, I got around it using:
<Autocomplete Validator="_ => _.Status = Convert.ToInt32(context.CellValue) > 0 ? ValidationStatus.Success : ValidationStatus.Error "
Is that "best practice" do you think? seems to work, only hitch is that once I do select a valid value, the outline remains red (even though it is now valid). Let me know if there's a better way to do this :-)
I would personally recommend doing something similar to our ValidationRule class to centralize, reuse your validators instead of binding an anonymous delegate.
But at the end of the day, it's up to you, and your project.
If it's not validating correctly then that might be a bug. We'll take a look as soon as possible.
No problem, yes I have moved it into a validation service. All looks well, although it does not revalidate if I select a value, still claims it's empty
I've recorded a GIF:
In the second half of the gif, you can see another problem, unsure if it's the autocomplete or validation. Basically I have 3 "lee ramsingh" entries, with Ids of 38, 41 and 42. The autocomplete only lets me select the first one? If I try the other 2 they disappear as you can see at the end of the GIF. Any ideas?
Further to this, if I change the names so they are unique, the autocomplete works. Is there a bug if there are duplicate text despite the Ids being different?
context
Currently we tie the text and value to an item. So there isn't exactly a correlation between the text and value, they are correlated by the item itself. So currently both need to be unique.
As for the issue with validation can you provide a repro please?
Hello @leetheman, thank you for your submission. The issue was labeled "Status: Repro Missing", as you have not provided a way to reproduce the issue quickly. Most problems already solve themselves when isolated, but we would like you to provide us with a reproducible code to make it easier to investigate a possible bug.
Hi there, please see attached:
https://github.com/leetheman/BlazoriseAutocompleteRepro
Validation is sorted now thanks;
in the repro I have two grids with the following source data:
Id Name Display Name 1 Client A "1 - Client A" 2 Client A "2 - Client A" 3 Client B "3 - Client A"
The first one uses the Text field of "Name", which means there are duplicate entries in the dropdown. The buggy behaviour is that if you select the first one, it works fine, but if you select the second or third, it just blanks out.
I appreciate that in real life, this scenario should not happen, but you never know what a client might do and then complain that the system is broken. Ideally if they chose the second option in the list, it would select "2" as the ID, and the third option would be "3".
Hope this helps, happy to spin up more examples if needed.
Hello @leetheman
I guess It does make sense, that it shouldn't break on repeated texts. Autocomplete was developed like this, so we need to take careful look when changing it to make sure we break nothing else.
For now, I can only advise a workaround, to do some pre initialization of the data where you go find the duplicates and make them different texts somehow, something like :
- add blank spaces "Client A", "Client A ", "Client A "
- add numbers like Client A (1), Client A (2), Client A (3)
I'm closing this issue and opening a new one, where the scope will be to investigate Autocomplete & supporting repeated texts.