* Adds a field to the form. * @param {Field|object} fieldOrSpec Field instance or spec object * @throws {Error} If field name is already assigned. * @return {Form} Fluent interface
(fieldOrSpec)
| 75 | * @return {Form} Fluent interface |
| 76 | */ |
| 77 | addField (fieldOrSpec) { |
| 78 | // Retrieve field instance |
| 79 | let field = fieldOrSpec |
| 80 | if (!(fieldOrSpec instanceof Field)) { |
| 81 | // Resolve field spec to field instance |
| 82 | const spec = fieldOrSpec |
| 83 | if (spec.priority === undefined) { |
| 84 | // Use default priority |
| 85 | spec.priority = this._calculateDefaultPriority() |
| 86 | } |
| 87 | // Let the factory create an instance from given spec |
| 88 | field = this.getFieldFactory().create(spec) |
| 89 | } |
| 90 | |
| 91 | // Verify that given field name is not already assigned |
| 92 | if (this.getField(field.getName())) { |
| 93 | throw new Error( |
| 94 | `Name '${field.getName()}' is already assigned to a field.`) |
| 95 | } |
| 96 | |
| 97 | // Add field instance to the form |
| 98 | this._fields.push(field) |
| 99 | field.setDelegate(this) |
| 100 | this._sortFields() |
| 101 | |
| 102 | // Add field subview |
| 103 | if (field.isVisible() && this.hasView()) { |
| 104 | this.getView().addSubview(field.getView()) |
| 105 | } |
| 106 | |
| 107 | return this |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns named field value. |
no test coverage detected