help with Validation in MVVM

e.gromero@alumnos.upm.es 21 Reputation points
2020-11-24T17:05:03.98+00:00

Hello, everyone, I need help with Xamarin

I now that Xamarin forums are moving here

I am making basic validation forms with an email, and password, but it doesn't work correctly. If I enter a name and I enter a valid email, it says "name cannot be empty"

namespace ValidateFormularies.ViewModels {
    class ValidatorVM : ViewModelBase {


        private string _name;
        public string name {
            get { return _name; }
            set {
                if (_name != value) {
                    _name = value;
                    Validate(name);
                    RaisePropertyChanged();
                }
            }
        }


        private string _email;
        public string email {
            get { return _email; }
            set {
                if (_email != value) {
                    _email = value;
                    Validate(email);
                    RaisePropertyChanged();
                }
            }
        }



        private bool _isVisible;
        public bool isVisible {
            get { return _isVisible; }
            set {
                if (_isVisible != value) {
                    _isVisible = value;
                    RaisePropertyChanged();
                }
            }
        }


        private string _msg;
        public string msg {
            get { return _msg; }
            set {
                if (_msg != value) {
                    _msg = value;
                    RaisePropertyChanged();
                }
            }
        }

        private void Validate(string prop) {

            if (prop.Equals(email)) {
                if (!EmailValidator.IsValidEmail(email)) {
                    isVisible = true;
                    msg = "Email needs to be valid";
                } else if (string.IsNullOrEmpty(email)) {
                    msg = "Email cannot be empty";
                } else {
                    isVisible = false;
                }
            } else if (prop.Equals(name)) {
                if (string.IsNullOrEmpty(name)) {
                    isVisible = true;
                    msg = "name cannot be empty";
                } else {
                    isVisible = false;
                }
            }
        }
    }
}

Actually, this works, but not quite as expected

For example, If I enter my name in a random email, it will tell name cannot be empty

Also, can you explain why would you use nameOF?

my view

<StackLayout>
        <Entry Placeholder="Name" Text="{Binding name, Mode=TwoWay}" />
        <Label Text="{Binding msg, Mode=TwoWay}"  
               IsVisible="{Binding isVisible, Mode=TwoWay}"
               TextColor="Red" />
        <Entry Placeholder="Email" Text="{Binding email, Mode=TwoWay}" />
        <Label Text="{Binding msg, Mode=TwoWay}"
               TextColor="Red"
               IsVisible="{Binding isVisible, Mode=TwoWay}" />
    </StackLayout>

I updated my code a bit

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

Accepted answer
  1. John Hardman 161 Reputation points
    2020-11-24T19:40:04.22+00:00

    And for IsVisible too, those should also be OneWay.

    And having just looked at the XAML, it will be apparent why I said in my first answer "That might not be how you want your final validation to operate". You only have on msg property, which is being bound to in two different places. If you are going to show two messages, you will want to have two different properties for the validation messages, which would then mean you can simplify other bits.


2 additional answers

Sort by: Most helpful
  1. John Hardman 161 Reputation points
    2020-11-24T17:36:09.053+00:00

    In
    private void Validate(string prop)

    what do you want "prop" to be? Is it the value of the property, or the name of the property?

    I'm guessing that you intend it to be the name of the property.
    In that case, change:

    Validate(name);
    to
    Validate(nameof(name));

    Validate(email);
    to
    Validate(nameof(email));

    if (prop.Equals(email))
    to
    if (prop.Equals(nameof(email)))

    if (prop.Equals(prop))
    to
    if (prop.Equals(nameof(name)))

    if (!EmailValidator.IsValidEmail(prop) && string.IsNullOrEmpty(prop))
    to
    if (string.IsNullOrEmpty(email) | | !EmailValidator.IsValidEmail(email))

    (the last) string.IsNullOrEmpty(prop)
    to
    string.IsNullOrEmpty(name)

    That might not be how you want your final validation to operate, but it should get you started


  2. John Hardman 161 Reputation points
    2020-11-24T19:37:15.677+00:00

    BTW, you need to tweak your XAML too

    Your BindingModes when binding to msg should be OneWay, not TwoWay

    0 comments No comments