Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MultiplePickerField does not show validation errors #628

Open
nickl-martin opened this issue Mar 20, 2024 · 2 comments
Open

MultiplePickerField does not show validation errors #628

nickl-martin opened this issue Mar 20, 2024 · 2 comments
Labels
bug Something isn't working control-multiplepickerfield good first issue Good for newcomers

Comments

@nickl-martin
Copy link

MultiplePickerField does not have any error text or icon when validations are applied.

Consider the following layout:

<input:FormView>
	
	<mtrl:PickerField
		Title="My Single Picker"
		ItemsSource="{Binding Items}">
		<mtrl:PickerField.Validations>
			<v:RequiredValidation/>
		</mtrl:PickerField.Validations>
	</mtrl:PickerField>
	
	<mtrl:MultiplePickerField
		Title="My Multi Picker"
		ItemsSource="{Binding Items}">
		<mtrl:MultiplePickerField.Validations>
			<v:RequiredValidation/>
		</mtrl:MultiplePickerField.Validations>
	</mtrl:MultiplePickerField>
	
	<Button
		Text="Submit"
		input:FormView.IsSubmitButton="True"/>
	
</input:FormView>

When the submit button is pressed without filling out either field it only shows the error for the single picker field.

@enisn
Copy link
Owner

enisn commented Mar 20, 2024

Probably the items colletion is not null, it's an empty collection. You probably need to create a validation something like that:

public class CollectionNotEmptyValidation : IValidation
{
    public string Message { get; set; }

    public bool Validate(object value)
    {
        if (value is ICollection<object> collection)
        {
            return collection.Count > 0;
        }
        return false;
    }
}

And use it like this:

<material:MultiplePickerField Title="Multiple Picker">
    <material:MultiplePickerField.Validations>
        <root:CollectionNotEmptyValidation Message="At least one option must be selected." />
    </material:MultiplePickerField.Validations>
</material:MultiplePickerField>

I'll consider handling collections in RequiredValidation in the library in the next version

@enisn enisn added control-multiplepickerfield question❔ Further information is requested labels Mar 20, 2024
@nickl-martin
Copy link
Author

I see. Having RequiredValidation check for empty collections would be useful, however that is only part of the problem.

I dug a little deeper and found the real issue. MultiplePickerField does not override the GetValueForValidator() virtual method defined on InputField. Because of this it uses the implementation in InputField which just returns new object() (see below).

This could be resolved with the following code added to MultiplePickerField.

protected override object GetValueForValidator()
{
    return SelectedItems;
}

@enisn enisn added good first issue Good for newcomers bug Something isn't working and removed question❔ Further information is requested labels Mar 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working control-multiplepickerfield good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants