Salvador Ruiz Guevara
Salvador Ruiz Guevara

Reputation: 184

Validation on viewmodel + Catel

Im trying to implement validation on a viewModel in Catel.

I read the documentation and it looks really easy but somehow i'm probably missing something for this particular case. I have a user control that has two user controls nested in it. One is a user control that I will use as a custom command bar and the other is a detail view for a model.

<catel:UserControl x:Class="SICUAP.Views.CatProducto_CategoriasView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:catel="http://catel.codeplex.com"
               xmlns:Views="clr-namespace:SICUAP.Views">

<!-- Resources -->
<UserControl.Resources>
</UserControl.Resources>

<!-- Content -->
<catel:StackGrid>
    <catel:StackGrid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </catel:StackGrid.RowDefinitions>
    <Views:cmdGlobalesBDView></Views:cmdGlobalesBDView>
    <Label Content="Catalogo de Categorias de Producto" Style="{StaticResource estiloTituloControl}">
    </Label>
    <Views:dataProducto_CategoriasView />

</catel:StackGrid>

The command bar has only one button with a bind to a global command.

<Button Width="50" Height="50" VerticalAlignment="Center" HorizontalAlignment="Center" ToolTip="Salvar" Command="{catel:CommandManagerBinding Salvar}"
                    Style="{StaticResource MetroCircleButtonStyle}">
        <Rectangle Width="20" Height="20" Fill="{Binding Path=Foreground, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Button}}}" >
            <Rectangle.OpacityMask>
                <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_save}" />
            </Rectangle.OpacityMask>
        </Rectangle>
    </Button>

And this is the detail view

<catel:UserControl x:Class="SICUAP.Views.dataProducto_CategoriasView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:catel="http://catel.codeplex.com">
<UserControl.Resources>
</UserControl.Resources>
<catel:StackGrid>
    <catel:StackGrid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </catel:StackGrid.RowDefinitions>
    <GroupBox Header="Datos de la Categoria del Producto">
        <catel:StackGrid>
            <catel:StackGrid.RowDefinitions>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
                <RowDefinition Height="Auto"></RowDefinition>
            </catel:StackGrid.RowDefinitions>
            <catel:StackGrid.ColumnDefinitions>
                <ColumnDefinition Width="300"></ColumnDefinition>
                <ColumnDefinition Width="100"></ColumnDefinition>
                <ColumnDefinition Width="200"></ColumnDefinition>
            </catel:StackGrid.ColumnDefinitions>
            <Label Content="Nombre:"></Label>
            <Label Content="Activo:"></Label>
            <catel:EmptyCell></catel:EmptyCell>
            <TextBox Margin="5" Text="{Binding Nombre, ValidatesOnDataErrors=True, NotifyOnValidationError=True}"></TextBox>
            <CheckBox Margin="5" IsChecked="{Binding Activo}"></CheckBox>
            <catel:EmptyCell></catel:EmptyCell>
            <Label Content="Descripcion:"></Label>
            <catel:EmptyRow></catel:EmptyRow>
            <TextBox Margin="5" TextWrapping="Wrap" AcceptsReturn="True" Grid.ColumnSpan="3" Height="200" Text="{Binding Descr,ValidatesOnDataErrors=True, NotifyOnValidationError=True}"></TextBox>
        </catel:StackGrid>
    </GroupBox>
</catel:StackGrid>

I create the command and bind it to the validation summary on the constructor of the detail view model.

public dataProducto_CategoriasViewModel(ICommandManager commandManager, IMessageService messageService )
    {
        Catel.Argument.IsNotNull(() => commandManager);
        Catel.Argument.IsNotNull(() => messageService);
        _commandManager = commandManager;
        _messageService = messageService;            
        Salvar = CommandHelper.CreateCommand(OnSalvarExecute,()=>CategoriaProductoValidationSummary);
        _commandManager.RegisterCommand(Comandos.Catalogos.Salvar, Salvar, this);
    }

And this is my property of the validation summary and the override for the ValidateFields.

[ValidationToViewModel(Tag = "ValidacionCategoriaProducto")]
    public IValidationSummary CategoriaProductoValidationSummary { get; set; }

    protected override void ValidateFields(List<IFieldValidationResult> validationResults)
    {
        if (string.IsNullOrEmpty(Nombre))
            validationResults.Add(FieldValidationResult.CreateErrorWithTag(NombrePropiedad, "Nombre es requerido","ValidacionCategoriaProducto"));
        if (string.IsNullOrEmpty(Descr))
            validationResults.Add(FieldValidationResult.CreateErrorWithTag(DescrPropiedad, "Descripcion es requerido", "ValidacionCategoriaProducto"));           
    }

The ValidateFields never triggers. And the button is always enabled. Am I missing something like a service register or something else?

Upvotes: 1

Views: 560

Answers (1)

Salvador Ruiz Guevara
Salvador Ruiz Guevara

Reputation: 184

Already found my mistake. When creating a project with the Orchestra template, it adds this lines at the start of the App.xaml.cs

Catel.Windows.Controls.UserControl.DefaultCreateWarningAndErrorValidatorForViewModelValue = false;
Catel.Windows.Controls.UserControl.DefaultSkipSearchingForInfoBarMessageControlValue = true;
Catel.Data.ModelBase.SuspendValidationForAllModels = true;

They disable the validation features so deleting them should be more than enough.

Upvotes: 2

Related Questions