KiCad PCB EDA Suite
SCH_FIELD_VALIDATOR Class Reference

A text control validator used for validating the text allowed in library and schematic symbol fields. More...

#include <sch_validators.h>

Inheritance diagram for SCH_FIELD_VALIDATOR:

Public Member Functions

 SCH_FIELD_VALIDATOR (bool aIsLibEditor, int aFieldId, wxString *aValue=nullptr)
 
 SCH_FIELD_VALIDATOR (const SCH_FIELD_VALIDATOR &aValidator)
 
virtual wxObject * Clone () const override
 
virtual bool Validate (wxWindow *aParent) override
 Override the default Validate() function provided by wxTextValidator to provide better error messages. More...
 

Private Attributes

int m_fieldId
 
bool m_isLibEditor
 

Detailed Description

A text control validator used for validating the text allowed in library and schematic symbol fields.

  • The reference field does not accept spaces.
  • The value field does not accept spaces in the symbol library editor because in symbol libraries, the value field is the symbol name in the library.

Definition at line 51 of file sch_validators.h.

Constructor & Destructor Documentation

◆ SCH_FIELD_VALIDATOR() [1/2]

SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR ( bool  aIsLibEditor,
int  aFieldId,
wxString *  aValue = nullptr 
)

Definition at line 39 of file sch_validators.cpp.

39 :
40 wxTextValidator( wxFILTER_EXCLUDE_CHAR_LIST, aValue )
41{
42 m_fieldId = aFieldId;
43 m_isLibEditor = aIsLibEditor;
44
45 // Fields cannot contain carriage returns, line feeds, or tabs.
46 wxString excludes( wxT( "\r\n\t" ) );
47
48 // The reference and sheet name fields cannot contain spaces.
49 if( aFieldId == REFERENCE_FIELD )
50 {
51 excludes += wxT( " " );
52 }
53 else if( m_fieldId == SHEETNAME_V )
54 {
55 excludes += wxT( "/" );
56 }
57
58 long style = GetStyle();
59
60 // The reference, value sheetname and sheetfilename fields cannot be empty.
61 if( aFieldId == REFERENCE_FIELD
62 || aFieldId == VALUE_FIELD
63 || aFieldId == SHEETNAME_V
64 || aFieldId == SHEETFILENAME_V
65 || aFieldId == FIELD_NAME )
66 {
67 style |= wxFILTER_EMPTY;
68 }
69
70 SetStyle( style );
71 SetCharExcludes( excludes );
72}
#define SHEETFILENAME_V
#define FIELD_NAME
#define SHEETNAME_V
@ VALUE_FIELD
Field Value of part, i.e. "3.3K".
@ REFERENCE_FIELD
Field Reference of part, i.e. "IC21".

References FIELD_NAME, m_fieldId, m_isLibEditor, REFERENCE_FIELD, SHEETFILENAME_V, SHEETNAME_V, and VALUE_FIELD.

Referenced by Clone().

◆ SCH_FIELD_VALIDATOR() [2/2]

SCH_FIELD_VALIDATOR::SCH_FIELD_VALIDATOR ( const SCH_FIELD_VALIDATOR aValidator)

Definition at line 75 of file sch_validators.cpp.

75 :
76 wxTextValidator( aValidator )
77{
78 m_fieldId = aValidator.m_fieldId;
79 m_isLibEditor = aValidator.m_isLibEditor;
80}

References m_fieldId, and m_isLibEditor.

Member Function Documentation

◆ Clone()

virtual wxObject * SCH_FIELD_VALIDATOR::Clone ( ) const
inlineoverridevirtual

Definition at line 58 of file sch_validators.h.

58{ return new SCH_FIELD_VALIDATOR( *this ); }
SCH_FIELD_VALIDATOR(bool aIsLibEditor, int aFieldId, wxString *aValue=nullptr)

References SCH_FIELD_VALIDATOR().

◆ Validate()

bool SCH_FIELD_VALIDATOR::Validate ( wxWindow *  aParent)
overridevirtual

Override the default Validate() function provided by wxTextValidator to provide better error messages.

Parameters
aParentis the parent window of the error message dialog.
Returns
true if the text in the control is valid otherwise false.

Definition at line 83 of file sch_validators.cpp.

84{
85 // If window is disabled, simply return
86 if( !m_validatorWindow->IsEnabled() )
87 return true;
88
89 wxTextEntry* const text = GetTextEntry();
90
91 if( !text )
92 return false;
93
94 wxString val( text->GetValue() );
95
96 // The format of the error message for not allowed chars
97 wxString fieldCharError;
98
99 switch( m_fieldId )
100 {
101 case REFERENCE_FIELD:
102 fieldCharError = _( "The reference designator cannot contain %s character(s)." );
103 break;
104
105 case VALUE_FIELD:
106 fieldCharError = _( "The value field cannot contain %s character(s)." );
107 break;
108
109 case FOOTPRINT_FIELD:
110 fieldCharError = _( "The footprint field cannot contain %s character(s)." );
111 break;
112
113 case DATASHEET_FIELD:
114 fieldCharError = _( "The datasheet field cannot contain %s character(s)." );
115 break;
116
117 case SHEETNAME_V:
118 fieldCharError = _( "The sheet name cannot contain %s character(s)." );
119 break;
120
121 case SHEETFILENAME_V:
122 fieldCharError = _( "The sheet filename cannot contain %s character(s)." );
123 break;
124
125 default:
126 fieldCharError = _( "The field cannot contain %s character(s)." );
127 break;
128 };
129
130 wxString msg;
131
132 // We can only do some kinds of validation once the input is complete, so
133 // check for them here:
134 if( HasFlag( wxFILTER_EMPTY ) && val.empty() )
135 {
136 // Some fields cannot have an empty value, and user fields require a name:
137 if( m_fieldId == FIELD_NAME )
138 msg.Printf( _( "The name of the field cannot be empty." ) );
139 else // the FIELD_VALUE id or REFERENCE_FIELD or VALUE_FIELD
140 msg.Printf( _( "The value of the field cannot be empty." ) );
141 }
142 else if( HasFlag( wxFILTER_EXCLUDE_CHAR_LIST ) && ContainsExcludedCharacters( val ) )
143 {
144 wxArrayString badCharsFound;
145
146 for( const wxString& excludeChar : GetExcludes() )
147 {
148 if( val.Find( excludeChar ) != wxNOT_FOUND )
149 {
150 if( excludeChar == wxT( "\r" ) )
151 badCharsFound.Add( _( "carriage return" ) );
152 else if( excludeChar == wxT( "\n" ) )
153 badCharsFound.Add( _( "line feed" ) );
154 else if( excludeChar == wxT( "\t" ) )
155 badCharsFound.Add( _( "tab" ) );
156 else if( excludeChar == wxT( " " ) )
157 badCharsFound.Add( _( "space" ) );
158 else
159 badCharsFound.Add( wxString::Format( wxT( "'%s'" ), excludeChar ) );
160 }
161 }
162
163 wxString badChars;
164
165 for( size_t i = 0; i < badCharsFound.GetCount(); i++ )
166 {
167 if( !badChars.IsEmpty() )
168 {
169 if( badCharsFound.GetCount() == 2 )
170 {
171 badChars += _( " or " );
172 }
173 else
174 {
175 if( i < badCharsFound.GetCount() - 2 )
176 badChars += _( ", or " );
177 else
178 badChars += wxT( ", " );
179 }
180 }
181
182 badChars += badCharsFound.Item( i );
183 }
184
185 msg.Printf( fieldCharError, badChars );
186 }
187 else if( m_fieldId == REFERENCE_FIELD && val.Contains( wxT( "${" ) ) )
188 {
189 msg.Printf( _( "The reference designator cannot contain text variable references" ) );
190 }
191
192 if ( !msg.empty() )
193 {
194 m_validatorWindow->SetFocus();
195
196 wxMessageBox( msg, _( "Field Validation Error" ), wxOK | wxICON_EXCLAMATION, aParent );
197
198 return false;
199 }
200
201 return true;
202}
#define _(s)
void Format(OUTPUTFORMATTER *out, int aNestLevel, int aCtl, const CPTREE &aTree)
Output a PTREE into s-expression format via an OUTPUTFORMATTER derivative.
Definition: ptree.cpp:200
@ DATASHEET_FIELD
name of datasheet
@ FOOTPRINT_FIELD
Field Name Module PCB, i.e. "16DIP300".

References _, DATASHEET_FIELD, FIELD_NAME, FOOTPRINT_FIELD, Format(), m_fieldId, REFERENCE_FIELD, SHEETFILENAME_V, SHEETNAME_V, text, and VALUE_FIELD.

Member Data Documentation

◆ m_fieldId

int SCH_FIELD_VALIDATOR::m_fieldId
private

Definition at line 70 of file sch_validators.h.

Referenced by SCH_FIELD_VALIDATOR(), and Validate().

◆ m_isLibEditor

bool SCH_FIELD_VALIDATOR::m_isLibEditor
private

Definition at line 71 of file sch_validators.h.

Referenced by SCH_FIELD_VALIDATOR().


The documentation for this class was generated from the following files: