= $u_time + 86400) { $modifiedDate = get_the_modified_time('F jS, Y'); echo 'Última modificación: '.$modifiedDate.''; } }?>

Validaciones, terribles validaciones…

Una forma elegante de validar numerosas propiedades:

Queremos validar que ciertos campos del objeto Address estén rellenos:

public class Address : EntityBase
{
	public string City { get; set; }
	public string Country { get; set; }
	public string Name { get; set; }		
	public string PostalCode { get; set; }        
	public string Street { get; set; }
	public string StreetNumber { get; set; }
}

Para ello crearemos una clase AddressValidation con propiedades bool que nos indiquen si esta o no validada la propiedad:

public class AddressValidation
{
        private bool _addressNameIsValid;
        private bool _cityIsValid;
        private bool _countryIsValid;
        private bool _streetNameIsValid;
        private bool _streetNumberIsValid;

        public bool AddressNameIsValid
        {
            get { return _addressNameIsValid; }
            set
            {
                _addressNameIsValid = value;
                RaiseIsValid();
            }
        }

        public bool CityIsValid
        {
            get { return _cityIsValid; }
            set
            {
                _cityIsValid = value;
                RaiseIsValid();
            }
        }

        public bool CountryIsValid
        {
            get { return _countryIsValid; }
            set
            {
                _countryIsValid = value;
                RaiseIsValid();
            }
        }

        public Action<bool> IsValidAddress { get; set; }

        public bool StreetNameIsValid
        {
            get { return _streetNameIsValid; }
            set
            {
                _streetNameIsValid = value;
                RaiseIsValid();
            }
        }

        public bool StreetNumberIsValid
        {
            get { return _streetNumberIsValid; }
            set
            {
                _streetNumberIsValid = value;
                RaiseIsValid();
            }
        }

        private void RaiseIsValid()
        {
            IsValidAddress(StreetNameIsValid && StreetNumberIsValid && CityIsValid && CountryIsValid && PhoneNumberIsValid);
        }
}

 

Creamos un objeto de tipo AddressValidation:

public AddressValidation ShipmentAddressValidation
{
    get { return _shipmentAddressValidation; }
    set
    {
       _shipmentAddressValidation = value;
       RaisePropertyChanged();
    }
}

 

Seguidamente en nuestro constructor declaramos la accion

public MainViewModel()
{
    ShipmentAddressValidation.IsValidAddress = value =>
    {
      _shipmentAddressIsValid = value;
      RaisePropertyChanged(() => AddressesAreValid);
    };
}

 

Leave a Reply

Your email address will not be published. Required fields are marked *