Unify sheet name validators and apply to properties

Properties panel and properties dialog now share a validator with shared
strings

Fixes https://gitlab.com/kicad/code/kicad/issues/21689
This commit is contained in:
Seth Hillbrand 2025-09-13 02:40:50 -07:00
parent 497afffd48
commit 733a379ca3
3 changed files with 95 additions and 57 deletions

View File

@ -289,13 +289,14 @@ bool FIELD_VALIDATOR::Validate( wxWindow* aParent )
} }
bool FIELD_VALIDATOR::DoValidate( const wxString& aValue, wxWindow* aParent ) wxString GetFieldValidationErrorMessage( FIELD_T aFieldId, const wxString& aValue )
{ {
FIELD_VALIDATOR validator( aFieldId );
wxString msg; wxString msg;
if( HasFlag( wxFILTER_EMPTY ) && aValue.empty() ) if( validator.HasFlag( wxFILTER_EMPTY ) && aValue.empty() )
{ {
switch( m_fieldId ) switch( aFieldId )
{ {
case FIELD_T::SHEET_NAME: msg = _( "A sheet must have a name." ); break; case FIELD_T::SHEET_NAME: msg = _( "A sheet must have a name." ); break;
case FIELD_T::SHEET_FILENAME: msg = _( "A sheet must have a file specified." ); break; case FIELD_T::SHEET_FILENAME: msg = _( "A sheet must have a file specified." ); break;
@ -303,11 +304,11 @@ bool FIELD_VALIDATOR::DoValidate( const wxString& aValue, wxWindow* aParent )
} }
} }
if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( aValue ) ) if( msg.empty() && validator.HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) )
{ {
wxArrayString badCharsFound; wxArrayString badCharsFound;
for( const wxUniCharRef& excludeChar : GetCharExcludes() ) for( const wxUniCharRef& excludeChar : validator.GetCharExcludes() )
{ {
if( aValue.Find( excludeChar ) != wxNOT_FOUND ) if( aValue.Find( excludeChar ) != wxNOT_FOUND )
{ {
@ -324,6 +325,8 @@ bool FIELD_VALIDATOR::DoValidate( const wxString& aValue, wxWindow* aParent )
} }
} }
if( !badCharsFound.IsEmpty() )
{
wxString badChars; wxString badChars;
for( size_t i = 0; i < badCharsFound.GetCount(); i++ ) for( size_t i = 0; i < badCharsFound.GetCount(); i++ )
@ -346,7 +349,7 @@ bool FIELD_VALIDATOR::DoValidate( const wxString& aValue, wxWindow* aParent )
badChars += badCharsFound.Item( i ); badChars += badCharsFound.Item( i );
} }
switch( m_fieldId ) switch( aFieldId )
{ {
case FIELD_T::REFERENCE: case FIELD_T::REFERENCE:
msg.Printf( _( "The reference designator cannot contain %s character(s)." ), badChars ); msg.Printf( _( "The reference designator cannot contain %s character(s)." ), badChars );
@ -377,14 +380,27 @@ bool FIELD_VALIDATOR::DoValidate( const wxString& aValue, wxWindow* aParent )
break; break;
}; };
} }
else if( m_fieldId == FIELD_T::REFERENCE && aValue.Contains( wxT( "${" ) ) ) }
if( msg.empty() )
{
if( aFieldId == FIELD_T::REFERENCE && aValue.Contains( wxT( "${" ) ) )
{ {
msg.Printf( _( "The reference designator cannot contain text variable references" ) ); msg.Printf( _( "The reference designator cannot contain text variable references" ) );
} }
else if( m_fieldId == FIELD_T::REFERENCE && UTIL::GetRefDesPrefix( aValue ).IsEmpty() ) else if( aFieldId == FIELD_T::REFERENCE && UTIL::GetRefDesPrefix( aValue ).IsEmpty() )
{ {
msg.Printf( _( "References must start with a letter." ) ); msg.Printf( _( "References must start with a letter." ) );
} }
}
return msg;
}
bool FIELD_VALIDATOR::DoValidate( const wxString& aValue, wxWindow* aParent )
{
wxString msg = GetFieldValidationErrorMessage( m_fieldId, aValue );
if( !msg.empty() ) if( !msg.empty() )
{ {

View File

@ -46,6 +46,8 @@
#include <settings/color_settings.h> #include <settings/color_settings.h>
#include <settings/settings_manager.h> #include <settings/settings_manager.h>
#include <trace_helpers.h> #include <trace_helpers.h>
#include <validators.h>
#include <properties/property_validators.h>
#include <pgm_base.h> #include <pgm_base.h>
#include <wx/log.h> #include <wx/log.h>
@ -1657,7 +1659,21 @@ static struct SCH_SHEET_DESC
propMgr.InheritsAfter( TYPE_HASH( SCH_SHEET ), TYPE_HASH( SCH_ITEM ) ); propMgr.InheritsAfter( TYPE_HASH( SCH_SHEET ), TYPE_HASH( SCH_ITEM ) );
propMgr.AddProperty( new PROPERTY<SCH_SHEET, wxString>( _HKI( "Sheet Name" ), propMgr.AddProperty( new PROPERTY<SCH_SHEET, wxString>( _HKI( "Sheet Name" ),
&SCH_SHEET::SetName, &SCH_SHEET::GetName ) ); &SCH_SHEET::SetName, &SCH_SHEET::GetName ) )
.SetValidator( []( const wxAny&& aValue, EDA_ITEM* ) -> VALIDATOR_RESULT
{
wxString value;
if( !aValue.GetAs( &value ) )
return {};
wxString msg = GetFieldValidationErrorMessage( FIELD_T::SHEET_NAME, value );
if( msg.empty() )
return {};
return std::make_unique<VALIDATION_ERROR_MSG>( msg );
} );
propMgr.AddProperty( new PROPERTY<SCH_SHEET, int>( _HKI( "Border Width" ), propMgr.AddProperty( new PROPERTY<SCH_SHEET, int>( _HKI( "Border Width" ),
&SCH_SHEET::SetBorderWidth, &SCH_SHEET::GetBorderWidth, &SCH_SHEET::SetBorderWidth, &SCH_SHEET::GetBorderWidth,

View File

@ -162,5 +162,11 @@ private:
FIELD_T m_fieldId; FIELD_T m_fieldId;
}; };
/**
* Return the error message if @a aValue is invalid for @a aFieldId.
* Returns an empty string when the value is valid.
*/
wxString GetFieldValidationErrorMessage( FIELD_T aFieldId, const wxString& aValue );
#endif // #ifndef VALIDATORS_H #endif // #ifndef VALIDATORS_H