SetIfFieldExists sets the provided key-value data pair into the current Record model ONLY if key is existing Collection field name/modifier. This method does nothing if key is not a known Collection field name/modifier. On success returns the matched Field, otherwise - nil. To set any key-value,
(key string, value any)
| 865 | // |
| 866 | // To set any key-value, including custom/unknown fields, use the [Record.Set] method. |
| 867 | func (m *Record) SetIfFieldExists(key string, value any) Field { |
| 868 | for _, field := range m.Collection().Fields { |
| 869 | ff, ok := field.(SetterFinder) |
| 870 | if ok { |
| 871 | setter := ff.FindSetter(key) |
| 872 | if setter != nil { |
| 873 | setter(m, value) |
| 874 | return field |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | // fallback to the default field PrepareValue method for direct match |
| 879 | if key == field.GetName() { |
| 880 | value, _ = field.PrepareValue(m, value) |
| 881 | m.SetRaw(key, value) |
| 882 | return field |
| 883 | } |
| 884 | } |
| 885 | |
| 886 | return nil |
| 887 | } |
| 888 | |
| 889 | // Set sets the provided key-value data pair into the current Record model. |
| 890 | // |
no test coverage detected