Ejemplo: Añadir validación personalizada para nombres de carpetas

Tarea: compruebe los nuevos nombres de carpeta que no existen en el nivel actual.

Solución:

  1. Añada una validación personalizada $Prop["_FolderName"].CustomValidation en la función InitializeWindow, sección FolderWindow:

    function InitializeWindow
    {	
    	…
    	$mWindowName = $dsWindow.Name
    	Switch ( $mWindowName)
    	{
    		"FileWindow"
    		{
    			#rules applying for File
    		}
    		"FolderWindow"
    		{
    			#rules applying for Folder
    			$Prop["_FolderName"].CustomValidation = { FolderNameCustomValidation }
    		}
    		…
    
  2. Añada la función “FolderNameCustomValidation” y la búsqueda de carpetas relacionada:

  3. function FolderNameCustomValidation
    {
    	if(-not $dsWindow.FindName("DSNumSchmsCtrl").NumSchmFieldsEmpty)
    	{
    		return $true
    	}
    	if($Prop["_FolderName"].Value)
    	{
    		$rootFolder = $vault.DocumentService.GetFolderByPath($Prop["_FolderPath"].Value)
    		$mFldExist = mFindFolder $Prop["_FolderName"].Value $rootFolder
    		if($mFldExist)
    		{
    			$Prop["_FolderName"].CustomValidationErrorMessage = "Folder name exists, select a new unique one."
    			return $false
    		}
    		return $true
    	}
    	else
    	{
    		return $false
    	}
    }
    
    function mFindFolder($FolderName, $rootFolder)
    {
    	$FolderPropDefs = $vault.PropertyService.GetPropertyDefinitionsByEntityClassId("FLDR")
        $FolderNamePropDef = $FolderPropDefs | where {$_.SysName -eq "Name"}
        $srchCond = New-Object 'Autodesk.Connectivity.WebServices.SrchCond'
        $srchCond.PropDefId = $FolderNamePropDef.Id
        $srchCond.PropTyp = "SingleProperty"
        $srchCond.SrchOper = 3 #is equal
        $srchCond.SrchRule = "Must"
        $srchCond.SrchTxt = $FolderName
    
        $bookmark = ""
        $status = $null
        $totalResults = @()
        while ($status -eq $null -or $totalResults.Count -lt $status.TotalHits)
        {
            $results = $vault.DocumentService.FindFoldersBySearchConditions(@($srchCond),$null, @($rootFolder.Id), $false, [ref]$bookmark, [ref]$status)
    
            if ($results -ne $null)
            {
                $totalResults += $results
            }
            else {break}
        }
        return $totalResults;
    }
    
  4. Actualice el archivo File.xaml de definición de cuadro de diálogo: desplácese hasta el cuadro de texto "NOMBRE DE ARCHIVO" y actualice el enlace Text="{WPF:ValidatedBinding Name}" a Text="{WPF:ValidatedBinding Prop[_FolderName].Value}". La nueva definición de cuadro de texto tendrá el siguiente aspecto:
<TextBox Text="{WPF:ValidatedBinding Prop[_FolderName].Value}" x:Name="FOLDERNAME" 
                     IsReadOnly="{Binding EditMode}"
                     Visibility="{Binding NumSchmFieldsEmpty, ElementName=DSNumSchmsCtrl, Converter={StaticResource BooleanToVisibilityConverter}}" 
                     Grid.Column="1" Grid.Row="3" />