@@ -1,240 +1,240 @@ |
||
| 1 | 1 | <?php return array( |
| 2 | 2 | |
| 3 | - // Markup |
|
| 4 | - //////////////////////////////////////////////////////////////////// |
|
| 5 | - |
|
| 6 | - // Whether labels should be automatically computed from name |
|
| 7 | - 'automatic_label' => true, |
|
| 8 | - |
|
| 9 | - // The default form type |
|
| 10 | - 'default_form_type' => 'horizontal', |
|
| 11 | - |
|
| 12 | - // Whether Former should escape HTML tags of 'plaintext' fields value |
|
| 13 | - // Enabled by default |
|
| 14 | - // |
|
| 15 | - // Instead of disabled this option, you should use the 'HtmlString' class: |
|
| 16 | - // Former::plaintext('text') |
|
| 17 | - // ->forceValue( |
|
| 18 | - // new Illuminate\Support\HtmlString('<b>your HTML data</b>') |
|
| 19 | - // ) |
|
| 20 | - 'escape_plaintext_value' => true, |
|
| 21 | - |
|
| 22 | - // Validation |
|
| 23 | - //////////////////////////////////////////////////////////////////// |
|
| 24 | - |
|
| 25 | - // Whether Former should fetch errors from Session |
|
| 26 | - 'fetch_errors' => true, |
|
| 27 | - |
|
| 28 | - // Whether Former should try to apply Validator rules as attributes |
|
| 29 | - 'live_validation' => true, |
|
| 30 | - |
|
| 31 | - // Whether Former should automatically fetch error messages and |
|
| 32 | - // display them next to the matching fields |
|
| 33 | - 'error_messages' => true, |
|
| 34 | - |
|
| 35 | - // Checkables |
|
| 36 | - //////////////////////////////////////////////////////////////////// |
|
| 37 | - |
|
| 38 | - // Whether checkboxes should always be present in the POST data, |
|
| 39 | - // no matter if you checked them or not |
|
| 40 | - 'push_checkboxes' => false, |
|
| 41 | - |
|
| 42 | - // The value a checkbox will have in the POST array if unchecked |
|
| 43 | - 'unchecked_value' => 0, |
|
| 44 | - |
|
| 45 | - // Required fields |
|
| 46 | - //////////////////////////////////////////////////////////////////// |
|
| 47 | - |
|
| 48 | - // The class to be added to required fields |
|
| 49 | - 'required_class' => 'required', |
|
| 50 | - |
|
| 51 | - // A facultative text to append to the labels of required fields |
|
| 52 | - 'required_text' => '<sup>*</sup>', |
|
| 53 | - |
|
| 54 | - // Translations |
|
| 55 | - //////////////////////////////////////////////////////////////////// |
|
| 56 | - |
|
| 57 | - // Where Former should look for translations |
|
| 58 | - 'translate_from' => 'validation.attributes', |
|
| 59 | - |
|
| 60 | - // Whether text that comes out of the translated |
|
| 61 | - // should be capitalized (ex: email => Email) automatically |
|
| 62 | - 'capitalize_translations' => true, |
|
| 63 | - |
|
| 64 | - // An array of attributes to automatically translate |
|
| 65 | - 'translatable' => array( |
|
| 66 | - 'help', |
|
| 67 | - 'inlineHelp', |
|
| 68 | - 'blockHelp', |
|
| 69 | - 'placeholder', |
|
| 70 | - 'data_placeholder', |
|
| 71 | - 'label', |
|
| 72 | - ), |
|
| 73 | - |
|
| 74 | - // Framework |
|
| 75 | - //////////////////////////////////////////////////////////////////// |
|
| 76 | - |
|
| 77 | - // The framework to be used by Former |
|
| 78 | - 'framework' => 'TwitterBootstrap3', |
|
| 79 | - |
|
| 80 | - 'TwitterBootstrap5' => array( |
|
| 81 | - |
|
| 82 | - // Map Former-supported viewports to Bootstrap 5 equivalents |
|
| 83 | - 'viewports' => array( |
|
| 84 | - 'large' => 'lg', |
|
| 85 | - 'medium' => 'md', |
|
| 86 | - 'small' => 'sm', |
|
| 87 | - 'mini' => 'xs', |
|
| 88 | - ), |
|
| 89 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 90 | - 'labelWidths' => array( |
|
| 91 | - 'large' => 2, |
|
| 92 | - 'small' => 4, |
|
| 93 | - ), |
|
| 94 | - // HTML markup and classes used by Bootstrap 5 for icons |
|
| 95 | - 'icon' => array( |
|
| 96 | - 'tag' => 'i', |
|
| 97 | - 'set' => 'fa', |
|
| 98 | - 'prefix' => 'fa', |
|
| 99 | - ), |
|
| 100 | - |
|
| 101 | - ), |
|
| 102 | - |
|
| 103 | - 'TwitterBootstrap4' => array( |
|
| 104 | - |
|
| 105 | - // Map Former-supported viewports to Bootstrap 4 equivalents |
|
| 106 | - 'viewports' => array( |
|
| 107 | - 'large' => 'lg', |
|
| 108 | - 'medium' => 'md', |
|
| 109 | - 'small' => 'sm', |
|
| 110 | - 'mini' => 'xs', |
|
| 111 | - ), |
|
| 112 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 113 | - 'labelWidths' => array( |
|
| 114 | - 'large' => 2, |
|
| 115 | - 'small' => 4, |
|
| 116 | - ), |
|
| 117 | - // HTML markup and classes used by Bootstrap 4 for icons |
|
| 118 | - 'icon' => array( |
|
| 119 | - 'tag' => 'i', |
|
| 120 | - 'set' => 'fa', |
|
| 121 | - 'prefix' => 'fa', |
|
| 122 | - ), |
|
| 123 | - |
|
| 124 | - ), |
|
| 125 | - |
|
| 126 | - 'TwitterBootstrap3' => array( |
|
| 127 | - |
|
| 128 | - // Map Former-supported viewports to Bootstrap 3 equivalents |
|
| 129 | - 'viewports' => array( |
|
| 130 | - 'large' => 'lg', |
|
| 131 | - 'medium' => 'md', |
|
| 132 | - 'small' => 'sm', |
|
| 133 | - 'mini' => 'xs', |
|
| 134 | - ), |
|
| 135 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 136 | - 'labelWidths' => array( |
|
| 137 | - 'large' => 2, |
|
| 138 | - 'small' => 4, |
|
| 139 | - ), |
|
| 140 | - // HTML markup and classes used by Bootstrap 3 for icons |
|
| 141 | - 'icon' => array( |
|
| 142 | - 'tag' => 'span', |
|
| 143 | - 'set' => 'glyphicon', |
|
| 144 | - 'prefix' => 'glyphicon', |
|
| 145 | - ), |
|
| 146 | - |
|
| 147 | - ), |
|
| 148 | - |
|
| 149 | - 'Nude' => array( // No-framework markup |
|
| 150 | - 'icon' => array( |
|
| 151 | - 'tag' => 'i', |
|
| 152 | - 'set' => null, |
|
| 153 | - 'prefix' => 'icon', |
|
| 154 | - ), |
|
| 155 | - ), |
|
| 156 | - |
|
| 157 | - 'TwitterBootstrap' => array( // Twitter Bootstrap version 2 |
|
| 158 | - 'icon' => array( |
|
| 159 | - 'tag' => 'i', |
|
| 160 | - 'set' => null, |
|
| 161 | - 'prefix' => 'icon', |
|
| 162 | - ), |
|
| 163 | - ), |
|
| 164 | - |
|
| 165 | - 'ZurbFoundation5' => array( |
|
| 166 | - // Map Former-supported viewports to Foundation 5 equivalents |
|
| 167 | - 'viewports' => array( |
|
| 168 | - 'large' => 'large', |
|
| 169 | - 'medium' => null, |
|
| 170 | - 'small' => 'small', |
|
| 171 | - 'mini' => null, |
|
| 172 | - ), |
|
| 173 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 174 | - 'labelWidths' => array( |
|
| 175 | - 'small' => 3, |
|
| 176 | - ), |
|
| 177 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 178 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 179 | - // HTML markup and classes used by Foundation 5 for icons |
|
| 180 | - 'icon' => array( |
|
| 181 | - 'tag' => 'i', |
|
| 182 | - 'set' => null, |
|
| 183 | - 'prefix' => 'fi', |
|
| 184 | - ), |
|
| 185 | - // CSS for inline validation errors |
|
| 186 | - 'error_classes' => array('class' => 'error'), |
|
| 187 | - ), |
|
| 188 | - |
|
| 189 | - 'ZurbFoundation4' => array( |
|
| 190 | - // Foundation 4 also has an experimental "medium" breakpoint |
|
| 191 | - // explained at http://foundation.zurb.com/docs/components/grid.html |
|
| 192 | - 'viewports' => array( |
|
| 193 | - 'large' => 'large', |
|
| 194 | - 'medium' => null, |
|
| 195 | - 'small' => 'small', |
|
| 196 | - 'mini' => null, |
|
| 197 | - ), |
|
| 198 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 199 | - 'labelWidths' => array( |
|
| 200 | - 'small' => 3, |
|
| 201 | - ), |
|
| 202 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 203 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 204 | - // HTML markup and classes used by Foundation 4 for icons |
|
| 205 | - 'icon' => array( |
|
| 206 | - 'tag' => 'i', |
|
| 207 | - 'set' => 'general', |
|
| 208 | - 'prefix' => 'foundicon', |
|
| 209 | - ), |
|
| 210 | - // CSS for inline validation errors |
|
| 211 | - 'error_classes' => array('class' => 'alert-box radius warning'), |
|
| 212 | - ), |
|
| 213 | - |
|
| 214 | - 'ZurbFoundation' => array( // Foundation 3 |
|
| 215 | - 'viewports' => array( |
|
| 216 | - 'large' => '', |
|
| 217 | - 'medium' => null, |
|
| 218 | - 'small' => 'mobile-', |
|
| 219 | - 'mini' => null, |
|
| 220 | - ), |
|
| 221 | - // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 222 | - 'labelWidths' => array( |
|
| 223 | - 'large' => 2, |
|
| 224 | - 'small' => 4, |
|
| 225 | - ), |
|
| 226 | - // Classes to be applied to wrapped labels in horizontal forms |
|
| 227 | - 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 228 | - // HTML markup and classes used by Foundation 3 for icons |
|
| 229 | - 'icon' => array( |
|
| 230 | - 'tag' => 'i', |
|
| 231 | - 'set' => null, |
|
| 232 | - 'prefix' => 'fi', |
|
| 233 | - ), |
|
| 234 | - // CSS for inline validation errors |
|
| 235 | - // should work for Zurb 2 and 3 |
|
| 236 | - 'error_classes' => array('class' => 'alert-box alert error'), |
|
| 237 | - ), |
|
| 3 | + // Markup |
|
| 4 | + //////////////////////////////////////////////////////////////////// |
|
| 5 | + |
|
| 6 | + // Whether labels should be automatically computed from name |
|
| 7 | + 'automatic_label' => true, |
|
| 8 | + |
|
| 9 | + // The default form type |
|
| 10 | + 'default_form_type' => 'horizontal', |
|
| 11 | + |
|
| 12 | + // Whether Former should escape HTML tags of 'plaintext' fields value |
|
| 13 | + // Enabled by default |
|
| 14 | + // |
|
| 15 | + // Instead of disabled this option, you should use the 'HtmlString' class: |
|
| 16 | + // Former::plaintext('text') |
|
| 17 | + // ->forceValue( |
|
| 18 | + // new Illuminate\Support\HtmlString('<b>your HTML data</b>') |
|
| 19 | + // ) |
|
| 20 | + 'escape_plaintext_value' => true, |
|
| 21 | + |
|
| 22 | + // Validation |
|
| 23 | + //////////////////////////////////////////////////////////////////// |
|
| 24 | + |
|
| 25 | + // Whether Former should fetch errors from Session |
|
| 26 | + 'fetch_errors' => true, |
|
| 27 | + |
|
| 28 | + // Whether Former should try to apply Validator rules as attributes |
|
| 29 | + 'live_validation' => true, |
|
| 30 | + |
|
| 31 | + // Whether Former should automatically fetch error messages and |
|
| 32 | + // display them next to the matching fields |
|
| 33 | + 'error_messages' => true, |
|
| 34 | + |
|
| 35 | + // Checkables |
|
| 36 | + //////////////////////////////////////////////////////////////////// |
|
| 37 | + |
|
| 38 | + // Whether checkboxes should always be present in the POST data, |
|
| 39 | + // no matter if you checked them or not |
|
| 40 | + 'push_checkboxes' => false, |
|
| 41 | + |
|
| 42 | + // The value a checkbox will have in the POST array if unchecked |
|
| 43 | + 'unchecked_value' => 0, |
|
| 44 | + |
|
| 45 | + // Required fields |
|
| 46 | + //////////////////////////////////////////////////////////////////// |
|
| 47 | + |
|
| 48 | + // The class to be added to required fields |
|
| 49 | + 'required_class' => 'required', |
|
| 50 | + |
|
| 51 | + // A facultative text to append to the labels of required fields |
|
| 52 | + 'required_text' => '<sup>*</sup>', |
|
| 53 | + |
|
| 54 | + // Translations |
|
| 55 | + //////////////////////////////////////////////////////////////////// |
|
| 56 | + |
|
| 57 | + // Where Former should look for translations |
|
| 58 | + 'translate_from' => 'validation.attributes', |
|
| 59 | + |
|
| 60 | + // Whether text that comes out of the translated |
|
| 61 | + // should be capitalized (ex: email => Email) automatically |
|
| 62 | + 'capitalize_translations' => true, |
|
| 63 | + |
|
| 64 | + // An array of attributes to automatically translate |
|
| 65 | + 'translatable' => array( |
|
| 66 | + 'help', |
|
| 67 | + 'inlineHelp', |
|
| 68 | + 'blockHelp', |
|
| 69 | + 'placeholder', |
|
| 70 | + 'data_placeholder', |
|
| 71 | + 'label', |
|
| 72 | + ), |
|
| 73 | + |
|
| 74 | + // Framework |
|
| 75 | + //////////////////////////////////////////////////////////////////// |
|
| 76 | + |
|
| 77 | + // The framework to be used by Former |
|
| 78 | + 'framework' => 'TwitterBootstrap3', |
|
| 79 | + |
|
| 80 | + 'TwitterBootstrap5' => array( |
|
| 81 | + |
|
| 82 | + // Map Former-supported viewports to Bootstrap 5 equivalents |
|
| 83 | + 'viewports' => array( |
|
| 84 | + 'large' => 'lg', |
|
| 85 | + 'medium' => 'md', |
|
| 86 | + 'small' => 'sm', |
|
| 87 | + 'mini' => 'xs', |
|
| 88 | + ), |
|
| 89 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 90 | + 'labelWidths' => array( |
|
| 91 | + 'large' => 2, |
|
| 92 | + 'small' => 4, |
|
| 93 | + ), |
|
| 94 | + // HTML markup and classes used by Bootstrap 5 for icons |
|
| 95 | + 'icon' => array( |
|
| 96 | + 'tag' => 'i', |
|
| 97 | + 'set' => 'fa', |
|
| 98 | + 'prefix' => 'fa', |
|
| 99 | + ), |
|
| 100 | + |
|
| 101 | + ), |
|
| 102 | + |
|
| 103 | + 'TwitterBootstrap4' => array( |
|
| 104 | + |
|
| 105 | + // Map Former-supported viewports to Bootstrap 4 equivalents |
|
| 106 | + 'viewports' => array( |
|
| 107 | + 'large' => 'lg', |
|
| 108 | + 'medium' => 'md', |
|
| 109 | + 'small' => 'sm', |
|
| 110 | + 'mini' => 'xs', |
|
| 111 | + ), |
|
| 112 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 113 | + 'labelWidths' => array( |
|
| 114 | + 'large' => 2, |
|
| 115 | + 'small' => 4, |
|
| 116 | + ), |
|
| 117 | + // HTML markup and classes used by Bootstrap 4 for icons |
|
| 118 | + 'icon' => array( |
|
| 119 | + 'tag' => 'i', |
|
| 120 | + 'set' => 'fa', |
|
| 121 | + 'prefix' => 'fa', |
|
| 122 | + ), |
|
| 123 | + |
|
| 124 | + ), |
|
| 125 | + |
|
| 126 | + 'TwitterBootstrap3' => array( |
|
| 127 | + |
|
| 128 | + // Map Former-supported viewports to Bootstrap 3 equivalents |
|
| 129 | + 'viewports' => array( |
|
| 130 | + 'large' => 'lg', |
|
| 131 | + 'medium' => 'md', |
|
| 132 | + 'small' => 'sm', |
|
| 133 | + 'mini' => 'xs', |
|
| 134 | + ), |
|
| 135 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 136 | + 'labelWidths' => array( |
|
| 137 | + 'large' => 2, |
|
| 138 | + 'small' => 4, |
|
| 139 | + ), |
|
| 140 | + // HTML markup and classes used by Bootstrap 3 for icons |
|
| 141 | + 'icon' => array( |
|
| 142 | + 'tag' => 'span', |
|
| 143 | + 'set' => 'glyphicon', |
|
| 144 | + 'prefix' => 'glyphicon', |
|
| 145 | + ), |
|
| 146 | + |
|
| 147 | + ), |
|
| 148 | + |
|
| 149 | + 'Nude' => array( // No-framework markup |
|
| 150 | + 'icon' => array( |
|
| 151 | + 'tag' => 'i', |
|
| 152 | + 'set' => null, |
|
| 153 | + 'prefix' => 'icon', |
|
| 154 | + ), |
|
| 155 | + ), |
|
| 156 | + |
|
| 157 | + 'TwitterBootstrap' => array( // Twitter Bootstrap version 2 |
|
| 158 | + 'icon' => array( |
|
| 159 | + 'tag' => 'i', |
|
| 160 | + 'set' => null, |
|
| 161 | + 'prefix' => 'icon', |
|
| 162 | + ), |
|
| 163 | + ), |
|
| 164 | + |
|
| 165 | + 'ZurbFoundation5' => array( |
|
| 166 | + // Map Former-supported viewports to Foundation 5 equivalents |
|
| 167 | + 'viewports' => array( |
|
| 168 | + 'large' => 'large', |
|
| 169 | + 'medium' => null, |
|
| 170 | + 'small' => 'small', |
|
| 171 | + 'mini' => null, |
|
| 172 | + ), |
|
| 173 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 174 | + 'labelWidths' => array( |
|
| 175 | + 'small' => 3, |
|
| 176 | + ), |
|
| 177 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 178 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 179 | + // HTML markup and classes used by Foundation 5 for icons |
|
| 180 | + 'icon' => array( |
|
| 181 | + 'tag' => 'i', |
|
| 182 | + 'set' => null, |
|
| 183 | + 'prefix' => 'fi', |
|
| 184 | + ), |
|
| 185 | + // CSS for inline validation errors |
|
| 186 | + 'error_classes' => array('class' => 'error'), |
|
| 187 | + ), |
|
| 188 | + |
|
| 189 | + 'ZurbFoundation4' => array( |
|
| 190 | + // Foundation 4 also has an experimental "medium" breakpoint |
|
| 191 | + // explained at http://foundation.zurb.com/docs/components/grid.html |
|
| 192 | + 'viewports' => array( |
|
| 193 | + 'large' => 'large', |
|
| 194 | + 'medium' => null, |
|
| 195 | + 'small' => 'small', |
|
| 196 | + 'mini' => null, |
|
| 197 | + ), |
|
| 198 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 199 | + 'labelWidths' => array( |
|
| 200 | + 'small' => 3, |
|
| 201 | + ), |
|
| 202 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 203 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 204 | + // HTML markup and classes used by Foundation 4 for icons |
|
| 205 | + 'icon' => array( |
|
| 206 | + 'tag' => 'i', |
|
| 207 | + 'set' => 'general', |
|
| 208 | + 'prefix' => 'foundicon', |
|
| 209 | + ), |
|
| 210 | + // CSS for inline validation errors |
|
| 211 | + 'error_classes' => array('class' => 'alert-box radius warning'), |
|
| 212 | + ), |
|
| 213 | + |
|
| 214 | + 'ZurbFoundation' => array( // Foundation 3 |
|
| 215 | + 'viewports' => array( |
|
| 216 | + 'large' => '', |
|
| 217 | + 'medium' => null, |
|
| 218 | + 'small' => 'mobile-', |
|
| 219 | + 'mini' => null, |
|
| 220 | + ), |
|
| 221 | + // Width of labels for horizontal forms expressed as viewport => grid columns |
|
| 222 | + 'labelWidths' => array( |
|
| 223 | + 'large' => 2, |
|
| 224 | + 'small' => 4, |
|
| 225 | + ), |
|
| 226 | + // Classes to be applied to wrapped labels in horizontal forms |
|
| 227 | + 'wrappedLabelClasses' => array('right', 'inline'), |
|
| 228 | + // HTML markup and classes used by Foundation 3 for icons |
|
| 229 | + 'icon' => array( |
|
| 230 | + 'tag' => 'i', |
|
| 231 | + 'set' => null, |
|
| 232 | + 'prefix' => 'fi', |
|
| 233 | + ), |
|
| 234 | + // CSS for inline validation errors |
|
| 235 | + // should work for Zurb 2 and 3 |
|
| 236 | + 'error_classes' => array('class' => 'alert-box alert error'), |
|
| 237 | + ), |
|
| 238 | 238 | |
| 239 | 239 | |
| 240 | 240 | ); |
@@ -14,570 +14,570 @@ |
||
| 14 | 14 | */ |
| 15 | 15 | abstract class Checkable extends Field |
| 16 | 16 | { |
| 17 | - /** |
|
| 18 | - * Renders the checkables as inline |
|
| 19 | - * |
|
| 20 | - * @var boolean |
|
| 21 | - */ |
|
| 22 | - protected $inline = false; |
|
| 23 | - |
|
| 24 | - /** |
|
| 25 | - * Add a text to a single element |
|
| 26 | - * |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - protected $text = null; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * Renders the checkables as grouped |
|
| 33 | - * |
|
| 34 | - * @var boolean |
|
| 35 | - */ |
|
| 36 | - protected $grouped = false; |
|
| 37 | - |
|
| 38 | - /** |
|
| 39 | - * The checkable items currently stored |
|
| 40 | - * |
|
| 41 | - * @var array |
|
| 42 | - */ |
|
| 43 | - protected $items = array(); |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * The type of checkable item |
|
| 47 | - * |
|
| 48 | - * @var string |
|
| 49 | - */ |
|
| 50 | - protected $checkable = null; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * An array of checked items |
|
| 54 | - * |
|
| 55 | - * @var array |
|
| 56 | - */ |
|
| 57 | - protected $checked = array(); |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * The checkable currently being focused on |
|
| 61 | - * |
|
| 62 | - * @var integer |
|
| 63 | - */ |
|
| 64 | - protected $focus = null; |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Whether this particular checkable is to be pushed |
|
| 68 | - * |
|
| 69 | - * @var boolean |
|
| 70 | - */ |
|
| 71 | - protected $isPushed = null; |
|
| 72 | - |
|
| 73 | - //////////////////////////////////////////////////////////////////// |
|
| 74 | - //////////////////////////// CORE METHODS ////////////////////////// |
|
| 75 | - //////////////////////////////////////////////////////////////////// |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Build a new checkable |
|
| 79 | - * |
|
| 80 | - * @param Container $app |
|
| 81 | - * @param string $type |
|
| 82 | - * @param array $name |
|
| 83 | - * @param $label |
|
| 84 | - * @param $value |
|
| 85 | - * @param $attributes |
|
| 86 | - */ |
|
| 87 | - public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 88 | - { |
|
| 89 | - // Unify auto and chained methods of grouping checkboxes |
|
| 90 | - if (Str::endsWith($name, '[]')) { |
|
| 91 | - $name = substr($name, 0, -2); |
|
| 92 | - $this->grouped(); |
|
| 93 | - } |
|
| 94 | - parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 95 | - |
|
| 96 | - if (is_array($this->value)) { |
|
| 97 | - $this->items($this->value); |
|
| 98 | - } |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * Apply methods to focused checkable |
|
| 103 | - * |
|
| 104 | - * @param string $method |
|
| 105 | - * @param array $parameters |
|
| 106 | - * |
|
| 107 | - * @return $this |
|
| 108 | - */ |
|
| 109 | - public function __call($method, $parameters) |
|
| 110 | - { |
|
| 111 | - $focused = $this->setOnFocused('attributes.'.$method, Arr::get($parameters, 0)); |
|
| 112 | - if ($focused) { |
|
| 113 | - return $this; |
|
| 114 | - } |
|
| 115 | - |
|
| 116 | - return parent::__call($method, $parameters); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Prints out the currently stored checkables |
|
| 121 | - */ |
|
| 122 | - public function render() |
|
| 123 | - { |
|
| 124 | - $html = null; |
|
| 125 | - |
|
| 126 | - $this->setFieldClasses(); |
|
| 127 | - |
|
| 128 | - // Multiple items |
|
| 129 | - if ($this->items) { |
|
| 130 | - unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 131 | - foreach ($this->items as $key => $item) { |
|
| 132 | - $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 133 | - $html .= $this->createCheckable($item, $value); |
|
| 134 | - } |
|
| 135 | - |
|
| 136 | - return $html; |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - // Single item |
|
| 140 | - return $this->createCheckable(array( |
|
| 141 | - 'name' => $this->name, |
|
| 142 | - 'label' => $this->text, |
|
| 143 | - 'value' => $this->value, |
|
| 144 | - 'attributes' => $this->attributes, |
|
| 145 | - )); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - //////////////////////////////////////////////////////////////////// |
|
| 149 | - ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 150 | - //////////////////////////////////////////////////////////////////// |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Focus on a particular checkable |
|
| 154 | - * |
|
| 155 | - * @param integer $on The checkable to focus on |
|
| 156 | - * |
|
| 157 | - * @return $this |
|
| 158 | - */ |
|
| 159 | - public function on($on) |
|
| 160 | - { |
|
| 161 | - if (!isset($this->items[$on])) { |
|
| 162 | - return $this; |
|
| 163 | - } else { |
|
| 164 | - $this->focus = $on; |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - return $this; |
|
| 168 | - } |
|
| 169 | - |
|
| 170 | - /** |
|
| 171 | - * Set the checkables as inline |
|
| 172 | - */ |
|
| 173 | - public function inline($isInline = true) |
|
| 174 | - { |
|
| 175 | - $this->inline = $isInline; |
|
| 176 | - |
|
| 177 | - return $this; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Set the checkables as stacked |
|
| 182 | - */ |
|
| 183 | - public function stacked($isStacked = true) |
|
| 184 | - { |
|
| 185 | - $this->inline = !$isStacked; |
|
| 186 | - |
|
| 187 | - return $this; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Set the checkables as grouped |
|
| 192 | - */ |
|
| 193 | - public function grouped($isGrouped = true) |
|
| 194 | - { |
|
| 195 | - $this->grouped = $isGrouped; |
|
| 196 | - |
|
| 197 | - return $this; |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Add text to a single checkable |
|
| 202 | - * |
|
| 203 | - * @param string $text The checkable label |
|
| 204 | - * |
|
| 205 | - * @return $this |
|
| 206 | - */ |
|
| 207 | - public function text($text) |
|
| 208 | - { |
|
| 209 | - // Translate and format |
|
| 210 | - $text = Helpers::translate($text); |
|
| 211 | - |
|
| 212 | - // Apply on focused if any |
|
| 213 | - $focused = $this->setOnFocused('label', $text); |
|
| 214 | - if ($focused) { |
|
| 215 | - return $this; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - $this->text = $text; |
|
| 219 | - |
|
| 220 | - return $this; |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - /** |
|
| 224 | - * Push this particular checkbox |
|
| 225 | - * |
|
| 226 | - * @param boolean $pushed |
|
| 227 | - * |
|
| 228 | - * @return $this |
|
| 229 | - */ |
|
| 230 | - public function push($pushed = true) |
|
| 231 | - { |
|
| 232 | - $this->isPushed = $pushed; |
|
| 233 | - |
|
| 234 | - return $this; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - /** |
|
| 238 | - * Check a specific item |
|
| 239 | - * |
|
| 240 | - * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 241 | - * |
|
| 242 | - * @return $this |
|
| 243 | - */ |
|
| 244 | - public function check($checked = true) |
|
| 245 | - { |
|
| 246 | - // If we're setting all the checked items at once |
|
| 247 | - if (is_array($checked)) { |
|
| 248 | - $this->checked = $checked; |
|
| 249 | - // Checking an item in particular |
|
| 250 | - } elseif (is_string($checked) or is_int($checked)) { |
|
| 251 | - $this->checked[$checked] = true; |
|
| 252 | - // Only setting a single item |
|
| 253 | - } else { |
|
| 254 | - $this->checked[$this->name] = (bool) $checked; |
|
| 255 | - } |
|
| 256 | - |
|
| 257 | - return $this; |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Check if the checkables are inline |
|
| 263 | - * |
|
| 264 | - * @return boolean |
|
| 265 | - */ |
|
| 266 | - public function isInline() |
|
| 267 | - { |
|
| 268 | - return $this->inline; |
|
| 269 | - } |
|
| 270 | - |
|
| 271 | - //////////////////////////////////////////////////////////////////// |
|
| 272 | - ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 273 | - //////////////////////////////////////////////////////////////////// |
|
| 274 | - |
|
| 275 | - /** |
|
| 276 | - * Creates a series of checkable items |
|
| 277 | - * |
|
| 278 | - * @param array $_items Items to create |
|
| 279 | - */ |
|
| 280 | - protected function items($_items) |
|
| 281 | - { |
|
| 282 | - // If passing an array |
|
| 283 | - if (sizeof($_items) == 1 and |
|
| 284 | - isset($_items[0]) and |
|
| 285 | - is_array($_items[0]) |
|
| 286 | - ) { |
|
| 287 | - $_items = $_items[0]; |
|
| 288 | - } |
|
| 289 | - |
|
| 290 | - // Fetch models if that's what we were passed |
|
| 291 | - if (isset($_items[0]) and is_object($_items[0])) { |
|
| 292 | - $_items = Helpers::queryToArray($_items); |
|
| 293 | - $_items = array_flip($_items); |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - // Iterate through items, assign a name and a label to each |
|
| 297 | - $count = 0; |
|
| 298 | - foreach ($_items as $label => $name) { |
|
| 299 | - |
|
| 300 | - // Define a fallback name in case none is found |
|
| 301 | - $fallback = $this->isCheckbox() |
|
| 302 | - ? $this->name.'_'.$count |
|
| 303 | - : $this->name; |
|
| 304 | - |
|
| 305 | - // Grouped fields |
|
| 306 | - if ($this->isGrouped()) { |
|
| 307 | - $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 308 | - $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - // If we haven't any name defined for the checkable, try to compute some |
|
| 312 | - if (!is_string($label) and !is_array($name)) { |
|
| 313 | - $label = $name; |
|
| 314 | - $name = $fallback; |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - // If we gave custom information on the item, add them |
|
| 318 | - if (is_array($name)) { |
|
| 319 | - $attributes = $name; |
|
| 320 | - $name = Arr::get($attributes, 'name', $fallback); |
|
| 321 | - unset($attributes['name']); |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - // Store all informations we have in an array |
|
| 325 | - $item = array( |
|
| 326 | - 'name' => $name, |
|
| 327 | - 'label' => Helpers::translate($label), |
|
| 328 | - 'count' => $count, |
|
| 329 | - ); |
|
| 330 | - if (isset($attributes)) { |
|
| 331 | - $item['attributes'] = $attributes; |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - $this->items[] = $item; |
|
| 335 | - $count++; |
|
| 336 | - } |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - /** |
|
| 340 | - * Renders a checkable |
|
| 341 | - * |
|
| 342 | - * @param string|array $item A checkable item |
|
| 343 | - * @param integer $fallbackValue A fallback value if none is set |
|
| 344 | - * |
|
| 345 | - * @return string |
|
| 346 | - */ |
|
| 347 | - protected function createCheckable($item, $fallbackValue = 1) |
|
| 348 | - { |
|
| 349 | - // Extract informations |
|
| 350 | - extract($item); |
|
| 351 | - |
|
| 352 | - // Set default values |
|
| 353 | - if (!isset($attributes)) { |
|
| 354 | - $attributes = array(); |
|
| 355 | - } |
|
| 356 | - if (isset($attributes['value'])) { |
|
| 357 | - $value = $attributes['value']; |
|
| 358 | - } |
|
| 359 | - if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 360 | - $value = $fallbackValue; |
|
| 361 | - } |
|
| 362 | - |
|
| 363 | - // If inline items, add class |
|
| 364 | - $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 365 | - |
|
| 366 | - // In Bootsrap 3 or 4 or 5, don't append the the checkable type (radio/checkbox) as a class if |
|
| 367 | - // rendering inline. |
|
| 368 | - $class = in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline; |
|
| 369 | - |
|
| 370 | - // Merge custom attributes with global attributes |
|
| 371 | - $attributes = array_merge($this->attributes, $attributes); |
|
| 372 | - if (!isset($attributes['id'])) { |
|
| 373 | - $attributes['id'] = $name.$this->unique($name); |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - // Create field |
|
| 377 | - $field = Input::create($this->checkable, $name, Helpers::encode($value), $attributes); |
|
| 378 | - if ($this->isChecked($item, $value)) { |
|
| 379 | - $field->checked('checked'); |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - // Add hidden checkbox if requested |
|
| 383 | - if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 384 | - if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 385 | - $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 386 | - |
|
| 387 | - // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 388 | - // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 389 | - $this->app->instance('former.field', $this); |
|
| 390 | - } |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - // If no label to wrap, return plain checkable |
|
| 394 | - if (!$label) { |
|
| 395 | - $element = (is_object($field)) ? $field->render() : $field; |
|
| 396 | - } elseif (in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])) { |
|
| 397 | - // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
|
| 398 | - $labelClass = 'form-check-label'; |
|
| 399 | - $element = $field . Element::create('label', $label)->for($attributes['id'])->class($labelClass)->render(); |
|
| 400 | - |
|
| 401 | - $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
|
| 402 | - |
|
| 403 | - $element = Element::create('div', $element)->class($wrapper_class)->render(); |
|
| 404 | - } else { |
|
| 405 | - // Original way is to add the 'input' inside the 'label' |
|
| 406 | - $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 407 | - } |
|
| 408 | - |
|
| 409 | - // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 410 | - if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 411 | - $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 412 | - if ($this->getAttribute('disabled')) { |
|
| 413 | - $wrapper->addClass('disabled'); |
|
| 414 | - } |
|
| 415 | - $element = $wrapper->render(); |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - // Return the field |
|
| 419 | - return $element; |
|
| 420 | - } |
|
| 421 | - |
|
| 422 | - //////////////////////////////////////////////////////////////////// |
|
| 423 | - ///////////////////////////// HELPERS ////////////////////////////// |
|
| 424 | - //////////////////////////////////////////////////////////////////// |
|
| 425 | - |
|
| 426 | - /** |
|
| 427 | - * Generate an unique ID for a field |
|
| 428 | - * |
|
| 429 | - * @param string $name The field's name |
|
| 430 | - * |
|
| 431 | - * @return string A field number to use |
|
| 432 | - */ |
|
| 433 | - protected function unique($name) |
|
| 434 | - { |
|
| 435 | - $this->app['former']->labels[] = $name; |
|
| 436 | - |
|
| 437 | - // Count number of fields with the same ID |
|
| 438 | - $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 439 | - return $label == $name; |
|
| 440 | - }); |
|
| 441 | - $unique = sizeof($where); |
|
| 442 | - |
|
| 443 | - // In case the field doesn't need to be numbered |
|
| 444 | - if ($unique < 2 or empty($this->items)) { |
|
| 445 | - return false; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - return $unique; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - /** |
|
| 452 | - * Set something on the currently focused checkable |
|
| 453 | - * |
|
| 454 | - * @param string $attribute The key to set |
|
| 455 | - * @param string $value Its value |
|
| 456 | - * |
|
| 457 | - * @return $this|bool |
|
| 458 | - */ |
|
| 459 | - protected function setOnFocused($attribute, $value) |
|
| 460 | - { |
|
| 461 | - if (is_null($this->focus)) { |
|
| 462 | - return false; |
|
| 463 | - } |
|
| 464 | - |
|
| 465 | - $this->items[$this->focus] = Arr::set($this->items[$this->focus], $attribute, $value); |
|
| 466 | - |
|
| 467 | - return $this; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Check if a checkable is checked |
|
| 472 | - * |
|
| 473 | - * @return boolean Checked or not |
|
| 474 | - */ |
|
| 475 | - protected function isChecked($item = null, $value = null) |
|
| 476 | - { |
|
| 477 | - if (isset($item['name'])) { |
|
| 478 | - $name = $item['name']; |
|
| 479 | - } |
|
| 480 | - if (empty($name)) { |
|
| 481 | - $name = $this->name; |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 485 | - // Or if it's a single radio, simply see if we called check |
|
| 486 | - if ($this->isCheckbox() or |
|
| 487 | - !$this->isCheckbox() and !$this->items |
|
| 488 | - ) { |
|
| 489 | - $checked = Arr::get($this->checked, $name, false); |
|
| 490 | - |
|
| 491 | - // If there are multiple, search for the value |
|
| 492 | - // as the name are the same between radios |
|
| 493 | - } else { |
|
| 494 | - $checked = Arr::get($this->checked, $value, false); |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - // Check the values and POST array |
|
| 498 | - if ($this->isGrouped()) { |
|
| 499 | - // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 500 | - $groupIndex = self::getGroupIndexFromItem($item); |
|
| 501 | - |
|
| 502 | - // Search using the bare name, not the individual item name |
|
| 503 | - $post = $this->app['former']->getPost($this->name); |
|
| 504 | - $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 505 | - |
|
| 506 | - if (isset($post[$groupIndex])) { |
|
| 507 | - $post = $post[$groupIndex]; |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - /** |
|
| 511 | - * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 512 | - * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 513 | - * |
|
| 514 | - * array( |
|
| 515 | - * 'name' = 'roles[foo]', |
|
| 516 | - * 'value' => 'foo', |
|
| 517 | - * ) |
|
| 518 | - */ |
|
| 519 | - if ($static instanceof Collection) { |
|
| 520 | - // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 521 | - foreach ($static as $staticItem) { |
|
| 522 | - $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 523 | - if ($staticItemValue == $groupIndex) { |
|
| 524 | - $static = $staticItemValue; |
|
| 525 | - break; |
|
| 526 | - } |
|
| 527 | - } |
|
| 528 | - } else if (isset($static[$groupIndex])) { |
|
| 529 | - $static = $static[$groupIndex]; |
|
| 530 | - } |
|
| 531 | - } else { |
|
| 532 | - $post = $this->app['former']->getPost($name); |
|
| 533 | - $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 537 | - $isChecked = ($post == $value); |
|
| 538 | - } elseif (!is_null($static)) { |
|
| 539 | - $isChecked = ($static == $value); |
|
| 540 | - } else { |
|
| 541 | - $isChecked = $checked; |
|
| 542 | - } |
|
| 543 | - |
|
| 544 | - return $isChecked ? true : false; |
|
| 545 | - } |
|
| 546 | - |
|
| 547 | - /** |
|
| 548 | - * Check if the current element is a checkbox |
|
| 549 | - * |
|
| 550 | - * @return boolean Checkbox or radio |
|
| 551 | - */ |
|
| 552 | - protected function isCheckbox() |
|
| 553 | - { |
|
| 554 | - return $this->checkable == 'checkbox'; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Check if the checkables are grouped or not |
|
| 559 | - * |
|
| 560 | - * @return boolean |
|
| 561 | - */ |
|
| 562 | - protected function isGrouped() |
|
| 563 | - { |
|
| 564 | - return |
|
| 565 | - $this->grouped == true or |
|
| 566 | - strpos($this->name, '[]') !== false; |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - /** |
|
| 570 | - * @param array $item The item array, containing at least name and count keys. |
|
| 571 | - * |
|
| 572 | - * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 573 | - */ |
|
| 574 | - public static function getGroupIndexFromItem($item) |
|
| 575 | - { |
|
| 576 | - $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 577 | - if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 578 | - return $item['count']; |
|
| 579 | - } |
|
| 580 | - |
|
| 581 | - return $groupIndex; |
|
| 582 | - } |
|
| 17 | + /** |
|
| 18 | + * Renders the checkables as inline |
|
| 19 | + * |
|
| 20 | + * @var boolean |
|
| 21 | + */ |
|
| 22 | + protected $inline = false; |
|
| 23 | + |
|
| 24 | + /** |
|
| 25 | + * Add a text to a single element |
|
| 26 | + * |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + protected $text = null; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * Renders the checkables as grouped |
|
| 33 | + * |
|
| 34 | + * @var boolean |
|
| 35 | + */ |
|
| 36 | + protected $grouped = false; |
|
| 37 | + |
|
| 38 | + /** |
|
| 39 | + * The checkable items currently stored |
|
| 40 | + * |
|
| 41 | + * @var array |
|
| 42 | + */ |
|
| 43 | + protected $items = array(); |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * The type of checkable item |
|
| 47 | + * |
|
| 48 | + * @var string |
|
| 49 | + */ |
|
| 50 | + protected $checkable = null; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * An array of checked items |
|
| 54 | + * |
|
| 55 | + * @var array |
|
| 56 | + */ |
|
| 57 | + protected $checked = array(); |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * The checkable currently being focused on |
|
| 61 | + * |
|
| 62 | + * @var integer |
|
| 63 | + */ |
|
| 64 | + protected $focus = null; |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Whether this particular checkable is to be pushed |
|
| 68 | + * |
|
| 69 | + * @var boolean |
|
| 70 | + */ |
|
| 71 | + protected $isPushed = null; |
|
| 72 | + |
|
| 73 | + //////////////////////////////////////////////////////////////////// |
|
| 74 | + //////////////////////////// CORE METHODS ////////////////////////// |
|
| 75 | + //////////////////////////////////////////////////////////////////// |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Build a new checkable |
|
| 79 | + * |
|
| 80 | + * @param Container $app |
|
| 81 | + * @param string $type |
|
| 82 | + * @param array $name |
|
| 83 | + * @param $label |
|
| 84 | + * @param $value |
|
| 85 | + * @param $attributes |
|
| 86 | + */ |
|
| 87 | + public function __construct(Container $app, $type, $name, $label, $value, $attributes) |
|
| 88 | + { |
|
| 89 | + // Unify auto and chained methods of grouping checkboxes |
|
| 90 | + if (Str::endsWith($name, '[]')) { |
|
| 91 | + $name = substr($name, 0, -2); |
|
| 92 | + $this->grouped(); |
|
| 93 | + } |
|
| 94 | + parent::__construct($app, $type, $name, $label, $value, $attributes); |
|
| 95 | + |
|
| 96 | + if (is_array($this->value)) { |
|
| 97 | + $this->items($this->value); |
|
| 98 | + } |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * Apply methods to focused checkable |
|
| 103 | + * |
|
| 104 | + * @param string $method |
|
| 105 | + * @param array $parameters |
|
| 106 | + * |
|
| 107 | + * @return $this |
|
| 108 | + */ |
|
| 109 | + public function __call($method, $parameters) |
|
| 110 | + { |
|
| 111 | + $focused = $this->setOnFocused('attributes.'.$method, Arr::get($parameters, 0)); |
|
| 112 | + if ($focused) { |
|
| 113 | + return $this; |
|
| 114 | + } |
|
| 115 | + |
|
| 116 | + return parent::__call($method, $parameters); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Prints out the currently stored checkables |
|
| 121 | + */ |
|
| 122 | + public function render() |
|
| 123 | + { |
|
| 124 | + $html = null; |
|
| 125 | + |
|
| 126 | + $this->setFieldClasses(); |
|
| 127 | + |
|
| 128 | + // Multiple items |
|
| 129 | + if ($this->items) { |
|
| 130 | + unset($this->app['former']->labels[array_search($this->name, $this->app['former']->labels)]); |
|
| 131 | + foreach ($this->items as $key => $item) { |
|
| 132 | + $value = $this->isCheckbox() && !$this->isGrouped() ? 1 : $key; |
|
| 133 | + $html .= $this->createCheckable($item, $value); |
|
| 134 | + } |
|
| 135 | + |
|
| 136 | + return $html; |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + // Single item |
|
| 140 | + return $this->createCheckable(array( |
|
| 141 | + 'name' => $this->name, |
|
| 142 | + 'label' => $this->text, |
|
| 143 | + 'value' => $this->value, |
|
| 144 | + 'attributes' => $this->attributes, |
|
| 145 | + )); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + //////////////////////////////////////////////////////////////////// |
|
| 149 | + ////////////////////////// FIELD METHODS /////////////////////////// |
|
| 150 | + //////////////////////////////////////////////////////////////////// |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Focus on a particular checkable |
|
| 154 | + * |
|
| 155 | + * @param integer $on The checkable to focus on |
|
| 156 | + * |
|
| 157 | + * @return $this |
|
| 158 | + */ |
|
| 159 | + public function on($on) |
|
| 160 | + { |
|
| 161 | + if (!isset($this->items[$on])) { |
|
| 162 | + return $this; |
|
| 163 | + } else { |
|
| 164 | + $this->focus = $on; |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + return $this; |
|
| 168 | + } |
|
| 169 | + |
|
| 170 | + /** |
|
| 171 | + * Set the checkables as inline |
|
| 172 | + */ |
|
| 173 | + public function inline($isInline = true) |
|
| 174 | + { |
|
| 175 | + $this->inline = $isInline; |
|
| 176 | + |
|
| 177 | + return $this; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Set the checkables as stacked |
|
| 182 | + */ |
|
| 183 | + public function stacked($isStacked = true) |
|
| 184 | + { |
|
| 185 | + $this->inline = !$isStacked; |
|
| 186 | + |
|
| 187 | + return $this; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Set the checkables as grouped |
|
| 192 | + */ |
|
| 193 | + public function grouped($isGrouped = true) |
|
| 194 | + { |
|
| 195 | + $this->grouped = $isGrouped; |
|
| 196 | + |
|
| 197 | + return $this; |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Add text to a single checkable |
|
| 202 | + * |
|
| 203 | + * @param string $text The checkable label |
|
| 204 | + * |
|
| 205 | + * @return $this |
|
| 206 | + */ |
|
| 207 | + public function text($text) |
|
| 208 | + { |
|
| 209 | + // Translate and format |
|
| 210 | + $text = Helpers::translate($text); |
|
| 211 | + |
|
| 212 | + // Apply on focused if any |
|
| 213 | + $focused = $this->setOnFocused('label', $text); |
|
| 214 | + if ($focused) { |
|
| 215 | + return $this; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + $this->text = $text; |
|
| 219 | + |
|
| 220 | + return $this; |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + /** |
|
| 224 | + * Push this particular checkbox |
|
| 225 | + * |
|
| 226 | + * @param boolean $pushed |
|
| 227 | + * |
|
| 228 | + * @return $this |
|
| 229 | + */ |
|
| 230 | + public function push($pushed = true) |
|
| 231 | + { |
|
| 232 | + $this->isPushed = $pushed; |
|
| 233 | + |
|
| 234 | + return $this; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + /** |
|
| 238 | + * Check a specific item |
|
| 239 | + * |
|
| 240 | + * @param bool|string $checked The checkable to check, or an array of checked items |
|
| 241 | + * |
|
| 242 | + * @return $this |
|
| 243 | + */ |
|
| 244 | + public function check($checked = true) |
|
| 245 | + { |
|
| 246 | + // If we're setting all the checked items at once |
|
| 247 | + if (is_array($checked)) { |
|
| 248 | + $this->checked = $checked; |
|
| 249 | + // Checking an item in particular |
|
| 250 | + } elseif (is_string($checked) or is_int($checked)) { |
|
| 251 | + $this->checked[$checked] = true; |
|
| 252 | + // Only setting a single item |
|
| 253 | + } else { |
|
| 254 | + $this->checked[$this->name] = (bool) $checked; |
|
| 255 | + } |
|
| 256 | + |
|
| 257 | + return $this; |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Check if the checkables are inline |
|
| 263 | + * |
|
| 264 | + * @return boolean |
|
| 265 | + */ |
|
| 266 | + public function isInline() |
|
| 267 | + { |
|
| 268 | + return $this->inline; |
|
| 269 | + } |
|
| 270 | + |
|
| 271 | + //////////////////////////////////////////////////////////////////// |
|
| 272 | + ////////////////////////// INTERNAL METHODS //////////////////////// |
|
| 273 | + //////////////////////////////////////////////////////////////////// |
|
| 274 | + |
|
| 275 | + /** |
|
| 276 | + * Creates a series of checkable items |
|
| 277 | + * |
|
| 278 | + * @param array $_items Items to create |
|
| 279 | + */ |
|
| 280 | + protected function items($_items) |
|
| 281 | + { |
|
| 282 | + // If passing an array |
|
| 283 | + if (sizeof($_items) == 1 and |
|
| 284 | + isset($_items[0]) and |
|
| 285 | + is_array($_items[0]) |
|
| 286 | + ) { |
|
| 287 | + $_items = $_items[0]; |
|
| 288 | + } |
|
| 289 | + |
|
| 290 | + // Fetch models if that's what we were passed |
|
| 291 | + if (isset($_items[0]) and is_object($_items[0])) { |
|
| 292 | + $_items = Helpers::queryToArray($_items); |
|
| 293 | + $_items = array_flip($_items); |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + // Iterate through items, assign a name and a label to each |
|
| 297 | + $count = 0; |
|
| 298 | + foreach ($_items as $label => $name) { |
|
| 299 | + |
|
| 300 | + // Define a fallback name in case none is found |
|
| 301 | + $fallback = $this->isCheckbox() |
|
| 302 | + ? $this->name.'_'.$count |
|
| 303 | + : $this->name; |
|
| 304 | + |
|
| 305 | + // Grouped fields |
|
| 306 | + if ($this->isGrouped()) { |
|
| 307 | + $attributes['id'] = str_replace('[]', null, $fallback); |
|
| 308 | + $fallback = str_replace('[]', null, $this->name).'[]'; |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + // If we haven't any name defined for the checkable, try to compute some |
|
| 312 | + if (!is_string($label) and !is_array($name)) { |
|
| 313 | + $label = $name; |
|
| 314 | + $name = $fallback; |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + // If we gave custom information on the item, add them |
|
| 318 | + if (is_array($name)) { |
|
| 319 | + $attributes = $name; |
|
| 320 | + $name = Arr::get($attributes, 'name', $fallback); |
|
| 321 | + unset($attributes['name']); |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + // Store all informations we have in an array |
|
| 325 | + $item = array( |
|
| 326 | + 'name' => $name, |
|
| 327 | + 'label' => Helpers::translate($label), |
|
| 328 | + 'count' => $count, |
|
| 329 | + ); |
|
| 330 | + if (isset($attributes)) { |
|
| 331 | + $item['attributes'] = $attributes; |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + $this->items[] = $item; |
|
| 335 | + $count++; |
|
| 336 | + } |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + /** |
|
| 340 | + * Renders a checkable |
|
| 341 | + * |
|
| 342 | + * @param string|array $item A checkable item |
|
| 343 | + * @param integer $fallbackValue A fallback value if none is set |
|
| 344 | + * |
|
| 345 | + * @return string |
|
| 346 | + */ |
|
| 347 | + protected function createCheckable($item, $fallbackValue = 1) |
|
| 348 | + { |
|
| 349 | + // Extract informations |
|
| 350 | + extract($item); |
|
| 351 | + |
|
| 352 | + // Set default values |
|
| 353 | + if (!isset($attributes)) { |
|
| 354 | + $attributes = array(); |
|
| 355 | + } |
|
| 356 | + if (isset($attributes['value'])) { |
|
| 357 | + $value = $attributes['value']; |
|
| 358 | + } |
|
| 359 | + if (!isset($value) or $value === $this->app['former']->getOption('unchecked_value')) { |
|
| 360 | + $value = $fallbackValue; |
|
| 361 | + } |
|
| 362 | + |
|
| 363 | + // If inline items, add class |
|
| 364 | + $isInline = $this->inline ? ' '.$this->app['former.framework']->getInlineLabelClass($this) : null; |
|
| 365 | + |
|
| 366 | + // In Bootsrap 3 or 4 or 5, don't append the the checkable type (radio/checkbox) as a class if |
|
| 367 | + // rendering inline. |
|
| 368 | + $class = in_array($this->app['former']->framework(), ['TwitterBootstrap3', 'TwitterBootstrap4', 'TwitterBootstrap5']) ? trim($isInline) : $this->checkable.$isInline; |
|
| 369 | + |
|
| 370 | + // Merge custom attributes with global attributes |
|
| 371 | + $attributes = array_merge($this->attributes, $attributes); |
|
| 372 | + if (!isset($attributes['id'])) { |
|
| 373 | + $attributes['id'] = $name.$this->unique($name); |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + // Create field |
|
| 377 | + $field = Input::create($this->checkable, $name, Helpers::encode($value), $attributes); |
|
| 378 | + if ($this->isChecked($item, $value)) { |
|
| 379 | + $field->checked('checked'); |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + // Add hidden checkbox if requested |
|
| 383 | + if ($this->isOfType('checkbox', 'checkboxes')) { |
|
| 384 | + if ($this->isPushed or ($this->app['former']->getOption('push_checkboxes') and $this->isPushed !== false)) { |
|
| 385 | + $field = $this->app['former']->hidden($name)->forceValue($this->app['former']->getOption('unchecked_value')).$field->render(); |
|
| 386 | + |
|
| 387 | + // app['former.field'] was overwritten by Former::hidden() call in the line above, so here |
|
| 388 | + // we reset it to $this to enable $this->app['former']->getErrors() to retrieve the correct object |
|
| 389 | + $this->app->instance('former.field', $this); |
|
| 390 | + } |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + // If no label to wrap, return plain checkable |
|
| 394 | + if (!$label) { |
|
| 395 | + $element = (is_object($field)) ? $field->render() : $field; |
|
| 396 | + } elseif (in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5'])) { |
|
| 397 | + // Revised for Bootstrap 4, move the 'input' outside of the 'label' |
|
| 398 | + $labelClass = 'form-check-label'; |
|
| 399 | + $element = $field . Element::create('label', $label)->for($attributes['id'])->class($labelClass)->render(); |
|
| 400 | + |
|
| 401 | + $wrapper_class = $this->inline ? 'form-check form-check-inline' : 'form-check'; |
|
| 402 | + |
|
| 403 | + $element = Element::create('div', $element)->class($wrapper_class)->render(); |
|
| 404 | + } else { |
|
| 405 | + // Original way is to add the 'input' inside the 'label' |
|
| 406 | + $element = Element::create('label', $field.$label)->for($attributes['id'])->class($class)->render(); |
|
| 407 | + } |
|
| 408 | + |
|
| 409 | + // If BS3, if checkables are stacked, wrap them in a div with the checkable type |
|
| 410 | + if (!$isInline && $this->app['former']->framework() == 'TwitterBootstrap3') { |
|
| 411 | + $wrapper = Element::create('div', $element)->class($this->checkable); |
|
| 412 | + if ($this->getAttribute('disabled')) { |
|
| 413 | + $wrapper->addClass('disabled'); |
|
| 414 | + } |
|
| 415 | + $element = $wrapper->render(); |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + // Return the field |
|
| 419 | + return $element; |
|
| 420 | + } |
|
| 421 | + |
|
| 422 | + //////////////////////////////////////////////////////////////////// |
|
| 423 | + ///////////////////////////// HELPERS ////////////////////////////// |
|
| 424 | + //////////////////////////////////////////////////////////////////// |
|
| 425 | + |
|
| 426 | + /** |
|
| 427 | + * Generate an unique ID for a field |
|
| 428 | + * |
|
| 429 | + * @param string $name The field's name |
|
| 430 | + * |
|
| 431 | + * @return string A field number to use |
|
| 432 | + */ |
|
| 433 | + protected function unique($name) |
|
| 434 | + { |
|
| 435 | + $this->app['former']->labels[] = $name; |
|
| 436 | + |
|
| 437 | + // Count number of fields with the same ID |
|
| 438 | + $where = array_filter($this->app['former']->labels, function ($label) use ($name) { |
|
| 439 | + return $label == $name; |
|
| 440 | + }); |
|
| 441 | + $unique = sizeof($where); |
|
| 442 | + |
|
| 443 | + // In case the field doesn't need to be numbered |
|
| 444 | + if ($unique < 2 or empty($this->items)) { |
|
| 445 | + return false; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + return $unique; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + /** |
|
| 452 | + * Set something on the currently focused checkable |
|
| 453 | + * |
|
| 454 | + * @param string $attribute The key to set |
|
| 455 | + * @param string $value Its value |
|
| 456 | + * |
|
| 457 | + * @return $this|bool |
|
| 458 | + */ |
|
| 459 | + protected function setOnFocused($attribute, $value) |
|
| 460 | + { |
|
| 461 | + if (is_null($this->focus)) { |
|
| 462 | + return false; |
|
| 463 | + } |
|
| 464 | + |
|
| 465 | + $this->items[$this->focus] = Arr::set($this->items[$this->focus], $attribute, $value); |
|
| 466 | + |
|
| 467 | + return $this; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Check if a checkable is checked |
|
| 472 | + * |
|
| 473 | + * @return boolean Checked or not |
|
| 474 | + */ |
|
| 475 | + protected function isChecked($item = null, $value = null) |
|
| 476 | + { |
|
| 477 | + if (isset($item['name'])) { |
|
| 478 | + $name = $item['name']; |
|
| 479 | + } |
|
| 480 | + if (empty($name)) { |
|
| 481 | + $name = $this->name; |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + // If it's a checkbox, see if we marqued that one as checked in the array |
|
| 485 | + // Or if it's a single radio, simply see if we called check |
|
| 486 | + if ($this->isCheckbox() or |
|
| 487 | + !$this->isCheckbox() and !$this->items |
|
| 488 | + ) { |
|
| 489 | + $checked = Arr::get($this->checked, $name, false); |
|
| 490 | + |
|
| 491 | + // If there are multiple, search for the value |
|
| 492 | + // as the name are the same between radios |
|
| 493 | + } else { |
|
| 494 | + $checked = Arr::get($this->checked, $value, false); |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + // Check the values and POST array |
|
| 498 | + if ($this->isGrouped()) { |
|
| 499 | + // The group index. (e.g. 'bar' if the item name is foo[bar], or the item index for foo[]) |
|
| 500 | + $groupIndex = self::getGroupIndexFromItem($item); |
|
| 501 | + |
|
| 502 | + // Search using the bare name, not the individual item name |
|
| 503 | + $post = $this->app['former']->getPost($this->name); |
|
| 504 | + $static = $this->app['former']->getValue($this->bind ?: $this->name); |
|
| 505 | + |
|
| 506 | + if (isset($post[$groupIndex])) { |
|
| 507 | + $post = $post[$groupIndex]; |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + /** |
|
| 511 | + * Support for Laravel Collection repopulating for grouped checkboxes. Note that the groupIndex must |
|
| 512 | + * match the value in order for the checkbox to be considered checked, e.g.: |
|
| 513 | + * |
|
| 514 | + * array( |
|
| 515 | + * 'name' = 'roles[foo]', |
|
| 516 | + * 'value' => 'foo', |
|
| 517 | + * ) |
|
| 518 | + */ |
|
| 519 | + if ($static instanceof Collection) { |
|
| 520 | + // If the repopulate value is a collection, search for an item matching the $groupIndex |
|
| 521 | + foreach ($static as $staticItem) { |
|
| 522 | + $staticItemValue = method_exists($staticItem, 'getKey') ? $staticItem->getKey() : $staticItem; |
|
| 523 | + if ($staticItemValue == $groupIndex) { |
|
| 524 | + $static = $staticItemValue; |
|
| 525 | + break; |
|
| 526 | + } |
|
| 527 | + } |
|
| 528 | + } else if (isset($static[$groupIndex])) { |
|
| 529 | + $static = $static[$groupIndex]; |
|
| 530 | + } |
|
| 531 | + } else { |
|
| 532 | + $post = $this->app['former']->getPost($name); |
|
| 533 | + $static = $this->app['former']->getValue($this->bind ?: $name); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + if (!is_null($post) and $post !== $this->app['former']->getOption('unchecked_value')) { |
|
| 537 | + $isChecked = ($post == $value); |
|
| 538 | + } elseif (!is_null($static)) { |
|
| 539 | + $isChecked = ($static == $value); |
|
| 540 | + } else { |
|
| 541 | + $isChecked = $checked; |
|
| 542 | + } |
|
| 543 | + |
|
| 544 | + return $isChecked ? true : false; |
|
| 545 | + } |
|
| 546 | + |
|
| 547 | + /** |
|
| 548 | + * Check if the current element is a checkbox |
|
| 549 | + * |
|
| 550 | + * @return boolean Checkbox or radio |
|
| 551 | + */ |
|
| 552 | + protected function isCheckbox() |
|
| 553 | + { |
|
| 554 | + return $this->checkable == 'checkbox'; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Check if the checkables are grouped or not |
|
| 559 | + * |
|
| 560 | + * @return boolean |
|
| 561 | + */ |
|
| 562 | + protected function isGrouped() |
|
| 563 | + { |
|
| 564 | + return |
|
| 565 | + $this->grouped == true or |
|
| 566 | + strpos($this->name, '[]') !== false; |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + /** |
|
| 570 | + * @param array $item The item array, containing at least name and count keys. |
|
| 571 | + * |
|
| 572 | + * @return mixed The group index. (e.g. returns bar if the item name is foo[bar], or the item count for foo[]) |
|
| 573 | + */ |
|
| 574 | + public static function getGroupIndexFromItem($item) |
|
| 575 | + { |
|
| 576 | + $groupIndex = preg_replace('/^.*?\[(.*)\]$/', '$1', $item['name']); |
|
| 577 | + if (empty($groupIndex) or $groupIndex == $item['name']) { |
|
| 578 | + return $item['count']; |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + return $groupIndex; |
|
| 582 | + } |
|
| 583 | 583 | } |
@@ -13,500 +13,500 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class Group extends Tag |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * The Container |
|
| 18 | - * |
|
| 19 | - * @var Container |
|
| 20 | - */ |
|
| 21 | - protected $app; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The current state of the group |
|
| 25 | - * |
|
| 26 | - * @var string |
|
| 27 | - */ |
|
| 28 | - protected $state = null; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * Whether the field should be displayed raw or not |
|
| 32 | - * |
|
| 33 | - * @var boolean |
|
| 34 | - */ |
|
| 35 | - protected $raw = false; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * The group label |
|
| 39 | - * |
|
| 40 | - * @var Element |
|
| 41 | - */ |
|
| 42 | - protected $label; |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The group help |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - protected $help = array(); |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * An array of elements to preprend the field |
|
| 53 | - * |
|
| 54 | - * @var array |
|
| 55 | - */ |
|
| 56 | - protected $prepend = array(); |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * An array of elements to append the field |
|
| 60 | - * |
|
| 61 | - * @var array |
|
| 62 | - */ |
|
| 63 | - protected $append = array(); |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * The field validations to be checked for errors |
|
| 67 | - * |
|
| 68 | - * @var array |
|
| 69 | - */ |
|
| 70 | - protected $validations = array(); |
|
| 71 | - |
|
| 72 | - /** |
|
| 73 | - * The group's element |
|
| 74 | - * |
|
| 75 | - * @var string |
|
| 76 | - */ |
|
| 77 | - protected $element = 'div'; |
|
| 78 | - |
|
| 79 | - /** |
|
| 80 | - * Whether a custom group is opened or not |
|
| 81 | - * |
|
| 82 | - * @var boolean |
|
| 83 | - */ |
|
| 84 | - public static $opened = false; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * The custom group that is open |
|
| 88 | - * |
|
| 89 | - * @var Former\Form\Group |
|
| 90 | - */ |
|
| 91 | - public static $openGroup = null; |
|
| 92 | - |
|
| 93 | - //////////////////////////////////////////////////////////////////// |
|
| 94 | - /////////////////////////// CORE METHODS /////////////////////////// |
|
| 95 | - //////////////////////////////////////////////////////////////////// |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Creates a group |
|
| 99 | - * |
|
| 100 | - * @param string $label Its label |
|
| 101 | - */ |
|
| 102 | - public function __construct(Container $app, $label, $validations = null) |
|
| 103 | - { |
|
| 104 | - // Get special classes |
|
| 105 | - $this->app = $app; |
|
| 106 | - $this->addClass($this->app['former.framework']->getGroupClasses()); |
|
| 107 | - |
|
| 108 | - // Invisible if Nude |
|
| 109 | - if ($this->app['former.framework']->is('Nude')) { |
|
| 110 | - $this->element = ''; |
|
| 111 | - } |
|
| 112 | - |
|
| 113 | - // Set group label |
|
| 114 | - if ($label) { |
|
| 115 | - $this->setLabel($label); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - // Set validations used to override groups own conclusions |
|
| 119 | - $this->validations = (array) $validations; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Prints out the opening of the Control Group |
|
| 124 | - * |
|
| 125 | - * @return string A control group opening tag |
|
| 126 | - */ |
|
| 127 | - public function __toString() |
|
| 128 | - { |
|
| 129 | - return $this->open().$this->getFormattedLabel(); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Opens a group |
|
| 134 | - * |
|
| 135 | - * @return string Opening tag |
|
| 136 | - */ |
|
| 137 | - public function open() |
|
| 138 | - { |
|
| 139 | - if ($this->getErrors()) { |
|
| 140 | - $this->state($this->app['former.framework']->errorState()); |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - // Retrieve state and append it to classes |
|
| 144 | - if ($this->state) { |
|
| 145 | - $this->addClass($this->state); |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - // Required state |
|
| 149 | - if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) { |
|
| 150 | - $this->addClass($this->app['former']->getOption('required_class')); |
|
| 151 | - } |
|
| 152 | - |
|
| 153 | - return parent::open(); |
|
| 154 | - } |
|
| 155 | - |
|
| 156 | - /** |
|
| 157 | - * Set the contents of the current group |
|
| 158 | - * |
|
| 159 | - * @param string $contents The group contents |
|
| 160 | - * |
|
| 161 | - * @return string A group |
|
| 162 | - */ |
|
| 163 | - public function contents($contents) |
|
| 164 | - { |
|
| 165 | - return $this->wrap($contents, $this->getFormattedLabel()); |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - /** |
|
| 169 | - * Wrap a Field with the current group |
|
| 170 | - * |
|
| 171 | - * @param \Former\Traits\Field $field A Field instance |
|
| 172 | - * |
|
| 173 | - * @return string A group |
|
| 174 | - */ |
|
| 175 | - public function wrapField($field) |
|
| 176 | - { |
|
| 177 | - $label = $this->getLabel($field); |
|
| 178 | - $help = $this->getHelp(); |
|
| 179 | - if ($field->isCheckable() && |
|
| 180 | - in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5']) |
|
| 181 | - ) { |
|
| 182 | - $wrapperClass = $field->isInline() ? 'form-check form-check-inline' : 'form-check'; |
|
| 183 | - if ($this->app['former']->getErrors($field->getName())) { |
|
| 184 | - $hiddenInput = Element::create('input', null, ['type' => 'hidden'])->class('form-check-input is-invalid'); |
|
| 185 | - $help = $hiddenInput.$help; |
|
| 186 | - } |
|
| 187 | - $help = Element::create('div', $help)->class($wrapperClass); |
|
| 188 | - } |
|
| 189 | - $field = $this->prependAppend($field); |
|
| 190 | - $field .= $help; |
|
| 191 | - |
|
| 192 | - return $this->wrap($field, $label); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - //////////////////////////////////////////////////////////////////// |
|
| 196 | - //////////////////////////// FIELD METHODS ///////////////////////// |
|
| 197 | - //////////////////////////////////////////////////////////////////// |
|
| 198 | - |
|
| 199 | - /** |
|
| 200 | - * Set the state of the group |
|
| 201 | - * |
|
| 202 | - * @param string $state A Bootstrap state class |
|
| 203 | - */ |
|
| 204 | - public function state($state) |
|
| 205 | - { |
|
| 206 | - // Filter state |
|
| 207 | - $state = $this->app['former.framework']->filterState($state); |
|
| 208 | - |
|
| 209 | - $this->state = $state; |
|
| 210 | - } |
|
| 211 | - |
|
| 212 | - /** |
|
| 213 | - * Set a class on the Group |
|
| 214 | - * |
|
| 215 | - * @param string $class The class to add |
|
| 216 | - */ |
|
| 217 | - public function addGroupClass($class) |
|
| 218 | - { |
|
| 219 | - $this->addClass($class); |
|
| 220 | - } |
|
| 221 | - |
|
| 222 | - /** |
|
| 223 | - * Set a class on the Label |
|
| 224 | - * |
|
| 225 | - * @param string $class The class to add on the Label |
|
| 226 | - */ |
|
| 227 | - public function addLabelClass($class) |
|
| 228 | - { |
|
| 229 | - // Don't add a label class if it isn't an Element instance |
|
| 230 | - if (!$this->label instanceof Element) { |
|
| 231 | - return $this; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - $this->label->addClass($class); |
|
| 235 | - |
|
| 236 | - return $this; |
|
| 237 | - } |
|
| 238 | - |
|
| 239 | - /** |
|
| 240 | - * Adds a label to the group |
|
| 241 | - * |
|
| 242 | - * @param string $label A label |
|
| 243 | - */ |
|
| 244 | - public function setLabel($label) |
|
| 245 | - { |
|
| 246 | - if (!$label instanceof Element) { |
|
| 247 | - $label = Helpers::translate($label); |
|
| 248 | - $label = Element::create('label', $label)->for($label); |
|
| 249 | - } |
|
| 250 | - |
|
| 251 | - $this->label = $label; |
|
| 252 | - } |
|
| 253 | - |
|
| 254 | - /** |
|
| 255 | - * Get the formatted group label |
|
| 256 | - * |
|
| 257 | - * @return string|null |
|
| 258 | - */ |
|
| 259 | - public function getFormattedLabel() |
|
| 260 | - { |
|
| 261 | - if (!$this->label) { |
|
| 262 | - return false; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - /** |
|
| 269 | - * Disables the control group for the current field |
|
| 270 | - */ |
|
| 271 | - public function raw() |
|
| 272 | - { |
|
| 273 | - $this->raw = true; |
|
| 274 | - } |
|
| 275 | - |
|
| 276 | - /** |
|
| 277 | - * Check if the current group is to be displayed or not |
|
| 278 | - * |
|
| 279 | - * @return boolean |
|
| 280 | - */ |
|
| 281 | - public function isRaw() |
|
| 282 | - { |
|
| 283 | - return (bool) $this->raw; |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - //////////////////////////////////////////////////////////////////// |
|
| 287 | - ///////////////////////////// HELP BLOCKS ////////////////////////// |
|
| 288 | - //////////////////////////////////////////////////////////////////// |
|
| 289 | - |
|
| 290 | - /** |
|
| 291 | - * Alias for inlineHelp |
|
| 292 | - * |
|
| 293 | - * @param string $help The help text |
|
| 294 | - * @param array $attributes Facultative attributes |
|
| 295 | - */ |
|
| 296 | - public function help($help, $attributes = array()) |
|
| 297 | - { |
|
| 298 | - return $this->inlineHelp($help, $attributes); |
|
| 299 | - } |
|
| 300 | - |
|
| 301 | - /** |
|
| 302 | - * Add an inline help |
|
| 303 | - * |
|
| 304 | - * @param string $help The help text |
|
| 305 | - * @param array $attributes Facultative attributes |
|
| 306 | - */ |
|
| 307 | - public function inlineHelp($help, $attributes = array()) |
|
| 308 | - { |
|
| 309 | - // If no help text, do nothing |
|
| 310 | - if (!$help) { |
|
| 311 | - return false; |
|
| 312 | - } |
|
| 313 | - |
|
| 314 | - $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes); |
|
| 315 | - } |
|
| 316 | - |
|
| 317 | - /** |
|
| 318 | - * Add an block help |
|
| 319 | - * |
|
| 320 | - * @param string $help The help text |
|
| 321 | - * @param array $attributes Facultative attributes |
|
| 322 | - */ |
|
| 323 | - public function blockHelp($help, $attributes = array()) |
|
| 324 | - { |
|
| 325 | - // Reserved method |
|
| 326 | - if ($this->app['former.framework']->isnt('TwitterBootstrap') && |
|
| 327 | - $this->app['former.framework']->isnt('TwitterBootstrap3') && |
|
| 328 | - $this->app['former.framework']->isnt('TwitterBootstrap4') && |
|
| 329 | - $this->app['former.framework']->isnt('TwitterBootstrap5') |
|
| 330 | - ) { |
|
| 331 | - throw new BadMethodCallException('This method is only available on the Bootstrap framework'); |
|
| 332 | - } |
|
| 333 | - |
|
| 334 | - // If no help text, do nothing |
|
| 335 | - if (!$help) { |
|
| 336 | - return false; |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - //////////////////////////////////////////////////////////////////// |
|
| 343 | - ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
|
| 344 | - //////////////////////////////////////////////////////////////////// |
|
| 345 | - |
|
| 346 | - /** |
|
| 347 | - * Prepend elements to the field |
|
| 348 | - */ |
|
| 349 | - public function prepend() |
|
| 350 | - { |
|
| 351 | - $this->placeAround(func_get_args(), 'prepend'); |
|
| 352 | - } |
|
| 353 | - |
|
| 354 | - /** |
|
| 355 | - * Append elements to the field |
|
| 356 | - */ |
|
| 357 | - public function append() |
|
| 358 | - { |
|
| 359 | - $this->placeAround(func_get_args(), 'append'); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Prepends an icon to a field |
|
| 364 | - * |
|
| 365 | - * @param string $icon The icon to prepend |
|
| 366 | - * @param array $attributes Its attributes |
|
| 367 | - */ |
|
| 368 | - public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 369 | - { |
|
| 370 | - $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 371 | - |
|
| 372 | - $this->prepend($icon); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Append an icon to a field |
|
| 377 | - * |
|
| 378 | - * @param string $icon The icon to prepend |
|
| 379 | - * @param array $attributes Its attributes |
|
| 380 | - */ |
|
| 381 | - public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 382 | - { |
|
| 383 | - $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 384 | - |
|
| 385 | - $this->append($icon); |
|
| 386 | - } |
|
| 387 | - |
|
| 388 | - //////////////////////////////////////////////////////////////////// |
|
| 389 | - //////////////////////////////// HELPERS /////////////////////////// |
|
| 390 | - //////////////////////////////////////////////////////////////////// |
|
| 391 | - |
|
| 392 | - /** |
|
| 393 | - * Get the errors for the group |
|
| 394 | - * |
|
| 395 | - * @return string |
|
| 396 | - */ |
|
| 397 | - public function getErrors() |
|
| 398 | - { |
|
| 399 | - $errors = ''; |
|
| 400 | - |
|
| 401 | - if (!self::$opened) { |
|
| 402 | - |
|
| 403 | - // for non-custom groups, normal error handling applies |
|
| 404 | - $errors = $this->app['former']->getErrors(); |
|
| 405 | - } elseif (!empty($this->validations)) { |
|
| 406 | - |
|
| 407 | - // error handling only when validations specified for custom groups |
|
| 408 | - foreach ($this->validations as $validation) { |
|
| 409 | - $errors .= $this->app['former']->getErrors($validation); |
|
| 410 | - } |
|
| 411 | - } |
|
| 412 | - |
|
| 413 | - return $errors; |
|
| 414 | - } |
|
| 415 | - |
|
| 416 | - /** |
|
| 417 | - * Wraps content in a group |
|
| 418 | - * |
|
| 419 | - * @param string $contents The content |
|
| 420 | - * @param string $label The label to add |
|
| 421 | - * |
|
| 422 | - * @return string A group |
|
| 423 | - */ |
|
| 424 | - public function wrap($contents, $label = null) |
|
| 425 | - { |
|
| 426 | - $group = $this->open(); |
|
| 427 | - $group .= $label; |
|
| 428 | - $group .= $this->app['former.framework']->wrapField($contents); |
|
| 429 | - $group .= $this->close(); |
|
| 430 | - |
|
| 431 | - return $group; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Prints out the current label |
|
| 436 | - * |
|
| 437 | - * @param string $field The field to create a label for |
|
| 438 | - * |
|
| 439 | - * @return string A <label> tag |
|
| 440 | - */ |
|
| 441 | - protected function getLabel($field = null) |
|
| 442 | - { |
|
| 443 | - // Don't create a label if none exist |
|
| 444 | - if (!$field or !$this->label) { |
|
| 445 | - return null; |
|
| 446 | - } |
|
| 447 | - |
|
| 448 | - // Wrap label in framework classes |
|
| 449 | - $labelClasses = $this->app['former.framework']->getLabelClasses(); |
|
| 450 | - if ($field->isCheckable() && |
|
| 451 | - in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5']) && |
|
| 452 | - $this->app['former.form']->isOfType('horizontal') |
|
| 453 | - ) { |
|
| 454 | - $labelClasses = array_merge($labelClasses, array('pt-0')); |
|
| 455 | - } |
|
| 456 | - $this->label->addClass($labelClasses); |
|
| 457 | - $this->label = $this->app['former.framework']->createLabelOf($field, $this->label); |
|
| 458 | - $this->label = $this->app['former.framework']->wrapLabel($this->label); |
|
| 459 | - |
|
| 460 | - return $this->label; |
|
| 461 | - } |
|
| 462 | - |
|
| 463 | - /** |
|
| 464 | - * Prints out the current help |
|
| 465 | - * |
|
| 466 | - * @return string A .help-block or .help-inline |
|
| 467 | - */ |
|
| 468 | - protected function getHelp() |
|
| 469 | - { |
|
| 470 | - $inline = Arr::get($this->help, 'inline'); |
|
| 471 | - $block = Arr::get($this->help, 'block'); |
|
| 472 | - |
|
| 473 | - // Replace help text with error if any found |
|
| 474 | - $errors = $this->app['former']->getErrors(); |
|
| 475 | - if ($errors and $this->app['former']->getOption('error_messages')) { |
|
| 476 | - $inline = $this->app['former.framework']->createValidationError($errors); |
|
| 477 | - } |
|
| 478 | - |
|
| 479 | - return join(null, array($inline, $block)); |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - /** |
|
| 483 | - * Format the field with prepended/appended elements |
|
| 484 | - * |
|
| 485 | - * @param Field $field The field to format |
|
| 486 | - * |
|
| 487 | - * @return string Field plus supplementary elements |
|
| 488 | - */ |
|
| 489 | - protected function prependAppend($field) |
|
| 490 | - { |
|
| 491 | - if (!$this->prepend and !$this->append) { |
|
| 492 | - return $field->render(); |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append); |
|
| 496 | - } |
|
| 497 | - |
|
| 498 | - /** |
|
| 499 | - * Place elements around the field |
|
| 500 | - * |
|
| 501 | - * @param array $items An array of items to place |
|
| 502 | - * @param string $place Where they should end up (prepend|append) |
|
| 503 | - */ |
|
| 504 | - protected function placeAround($items, $place) |
|
| 505 | - { |
|
| 506 | - // Iterate over the items and place them where they should |
|
| 507 | - foreach ((array) $items as $item) { |
|
| 508 | - $item = $this->app['former.framework']->placeAround($item, $place); |
|
| 509 | - $this->{$place}[] = $item; |
|
| 510 | - } |
|
| 511 | - } |
|
| 16 | + /** |
|
| 17 | + * The Container |
|
| 18 | + * |
|
| 19 | + * @var Container |
|
| 20 | + */ |
|
| 21 | + protected $app; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The current state of the group |
|
| 25 | + * |
|
| 26 | + * @var string |
|
| 27 | + */ |
|
| 28 | + protected $state = null; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * Whether the field should be displayed raw or not |
|
| 32 | + * |
|
| 33 | + * @var boolean |
|
| 34 | + */ |
|
| 35 | + protected $raw = false; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * The group label |
|
| 39 | + * |
|
| 40 | + * @var Element |
|
| 41 | + */ |
|
| 42 | + protected $label; |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The group help |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + protected $help = array(); |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * An array of elements to preprend the field |
|
| 53 | + * |
|
| 54 | + * @var array |
|
| 55 | + */ |
|
| 56 | + protected $prepend = array(); |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * An array of elements to append the field |
|
| 60 | + * |
|
| 61 | + * @var array |
|
| 62 | + */ |
|
| 63 | + protected $append = array(); |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * The field validations to be checked for errors |
|
| 67 | + * |
|
| 68 | + * @var array |
|
| 69 | + */ |
|
| 70 | + protected $validations = array(); |
|
| 71 | + |
|
| 72 | + /** |
|
| 73 | + * The group's element |
|
| 74 | + * |
|
| 75 | + * @var string |
|
| 76 | + */ |
|
| 77 | + protected $element = 'div'; |
|
| 78 | + |
|
| 79 | + /** |
|
| 80 | + * Whether a custom group is opened or not |
|
| 81 | + * |
|
| 82 | + * @var boolean |
|
| 83 | + */ |
|
| 84 | + public static $opened = false; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * The custom group that is open |
|
| 88 | + * |
|
| 89 | + * @var Former\Form\Group |
|
| 90 | + */ |
|
| 91 | + public static $openGroup = null; |
|
| 92 | + |
|
| 93 | + //////////////////////////////////////////////////////////////////// |
|
| 94 | + /////////////////////////// CORE METHODS /////////////////////////// |
|
| 95 | + //////////////////////////////////////////////////////////////////// |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Creates a group |
|
| 99 | + * |
|
| 100 | + * @param string $label Its label |
|
| 101 | + */ |
|
| 102 | + public function __construct(Container $app, $label, $validations = null) |
|
| 103 | + { |
|
| 104 | + // Get special classes |
|
| 105 | + $this->app = $app; |
|
| 106 | + $this->addClass($this->app['former.framework']->getGroupClasses()); |
|
| 107 | + |
|
| 108 | + // Invisible if Nude |
|
| 109 | + if ($this->app['former.framework']->is('Nude')) { |
|
| 110 | + $this->element = ''; |
|
| 111 | + } |
|
| 112 | + |
|
| 113 | + // Set group label |
|
| 114 | + if ($label) { |
|
| 115 | + $this->setLabel($label); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + // Set validations used to override groups own conclusions |
|
| 119 | + $this->validations = (array) $validations; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Prints out the opening of the Control Group |
|
| 124 | + * |
|
| 125 | + * @return string A control group opening tag |
|
| 126 | + */ |
|
| 127 | + public function __toString() |
|
| 128 | + { |
|
| 129 | + return $this->open().$this->getFormattedLabel(); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Opens a group |
|
| 134 | + * |
|
| 135 | + * @return string Opening tag |
|
| 136 | + */ |
|
| 137 | + public function open() |
|
| 138 | + { |
|
| 139 | + if ($this->getErrors()) { |
|
| 140 | + $this->state($this->app['former.framework']->errorState()); |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + // Retrieve state and append it to classes |
|
| 144 | + if ($this->state) { |
|
| 145 | + $this->addClass($this->state); |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + // Required state |
|
| 149 | + if ($this->app->bound('former.field') and $this->app['former.field']->isRequired()) { |
|
| 150 | + $this->addClass($this->app['former']->getOption('required_class')); |
|
| 151 | + } |
|
| 152 | + |
|
| 153 | + return parent::open(); |
|
| 154 | + } |
|
| 155 | + |
|
| 156 | + /** |
|
| 157 | + * Set the contents of the current group |
|
| 158 | + * |
|
| 159 | + * @param string $contents The group contents |
|
| 160 | + * |
|
| 161 | + * @return string A group |
|
| 162 | + */ |
|
| 163 | + public function contents($contents) |
|
| 164 | + { |
|
| 165 | + return $this->wrap($contents, $this->getFormattedLabel()); |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + /** |
|
| 169 | + * Wrap a Field with the current group |
|
| 170 | + * |
|
| 171 | + * @param \Former\Traits\Field $field A Field instance |
|
| 172 | + * |
|
| 173 | + * @return string A group |
|
| 174 | + */ |
|
| 175 | + public function wrapField($field) |
|
| 176 | + { |
|
| 177 | + $label = $this->getLabel($field); |
|
| 178 | + $help = $this->getHelp(); |
|
| 179 | + if ($field->isCheckable() && |
|
| 180 | + in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5']) |
|
| 181 | + ) { |
|
| 182 | + $wrapperClass = $field->isInline() ? 'form-check form-check-inline' : 'form-check'; |
|
| 183 | + if ($this->app['former']->getErrors($field->getName())) { |
|
| 184 | + $hiddenInput = Element::create('input', null, ['type' => 'hidden'])->class('form-check-input is-invalid'); |
|
| 185 | + $help = $hiddenInput.$help; |
|
| 186 | + } |
|
| 187 | + $help = Element::create('div', $help)->class($wrapperClass); |
|
| 188 | + } |
|
| 189 | + $field = $this->prependAppend($field); |
|
| 190 | + $field .= $help; |
|
| 191 | + |
|
| 192 | + return $this->wrap($field, $label); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + //////////////////////////////////////////////////////////////////// |
|
| 196 | + //////////////////////////// FIELD METHODS ///////////////////////// |
|
| 197 | + //////////////////////////////////////////////////////////////////// |
|
| 198 | + |
|
| 199 | + /** |
|
| 200 | + * Set the state of the group |
|
| 201 | + * |
|
| 202 | + * @param string $state A Bootstrap state class |
|
| 203 | + */ |
|
| 204 | + public function state($state) |
|
| 205 | + { |
|
| 206 | + // Filter state |
|
| 207 | + $state = $this->app['former.framework']->filterState($state); |
|
| 208 | + |
|
| 209 | + $this->state = $state; |
|
| 210 | + } |
|
| 211 | + |
|
| 212 | + /** |
|
| 213 | + * Set a class on the Group |
|
| 214 | + * |
|
| 215 | + * @param string $class The class to add |
|
| 216 | + */ |
|
| 217 | + public function addGroupClass($class) |
|
| 218 | + { |
|
| 219 | + $this->addClass($class); |
|
| 220 | + } |
|
| 221 | + |
|
| 222 | + /** |
|
| 223 | + * Set a class on the Label |
|
| 224 | + * |
|
| 225 | + * @param string $class The class to add on the Label |
|
| 226 | + */ |
|
| 227 | + public function addLabelClass($class) |
|
| 228 | + { |
|
| 229 | + // Don't add a label class if it isn't an Element instance |
|
| 230 | + if (!$this->label instanceof Element) { |
|
| 231 | + return $this; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + $this->label->addClass($class); |
|
| 235 | + |
|
| 236 | + return $this; |
|
| 237 | + } |
|
| 238 | + |
|
| 239 | + /** |
|
| 240 | + * Adds a label to the group |
|
| 241 | + * |
|
| 242 | + * @param string $label A label |
|
| 243 | + */ |
|
| 244 | + public function setLabel($label) |
|
| 245 | + { |
|
| 246 | + if (!$label instanceof Element) { |
|
| 247 | + $label = Helpers::translate($label); |
|
| 248 | + $label = Element::create('label', $label)->for($label); |
|
| 249 | + } |
|
| 250 | + |
|
| 251 | + $this->label = $label; |
|
| 252 | + } |
|
| 253 | + |
|
| 254 | + /** |
|
| 255 | + * Get the formatted group label |
|
| 256 | + * |
|
| 257 | + * @return string|null |
|
| 258 | + */ |
|
| 259 | + public function getFormattedLabel() |
|
| 260 | + { |
|
| 261 | + if (!$this->label) { |
|
| 262 | + return false; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return $this->label->addClass($this->app['former.framework']->getLabelClasses()); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + /** |
|
| 269 | + * Disables the control group for the current field |
|
| 270 | + */ |
|
| 271 | + public function raw() |
|
| 272 | + { |
|
| 273 | + $this->raw = true; |
|
| 274 | + } |
|
| 275 | + |
|
| 276 | + /** |
|
| 277 | + * Check if the current group is to be displayed or not |
|
| 278 | + * |
|
| 279 | + * @return boolean |
|
| 280 | + */ |
|
| 281 | + public function isRaw() |
|
| 282 | + { |
|
| 283 | + return (bool) $this->raw; |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + //////////////////////////////////////////////////////////////////// |
|
| 287 | + ///////////////////////////// HELP BLOCKS ////////////////////////// |
|
| 288 | + //////////////////////////////////////////////////////////////////// |
|
| 289 | + |
|
| 290 | + /** |
|
| 291 | + * Alias for inlineHelp |
|
| 292 | + * |
|
| 293 | + * @param string $help The help text |
|
| 294 | + * @param array $attributes Facultative attributes |
|
| 295 | + */ |
|
| 296 | + public function help($help, $attributes = array()) |
|
| 297 | + { |
|
| 298 | + return $this->inlineHelp($help, $attributes); |
|
| 299 | + } |
|
| 300 | + |
|
| 301 | + /** |
|
| 302 | + * Add an inline help |
|
| 303 | + * |
|
| 304 | + * @param string $help The help text |
|
| 305 | + * @param array $attributes Facultative attributes |
|
| 306 | + */ |
|
| 307 | + public function inlineHelp($help, $attributes = array()) |
|
| 308 | + { |
|
| 309 | + // If no help text, do nothing |
|
| 310 | + if (!$help) { |
|
| 311 | + return false; |
|
| 312 | + } |
|
| 313 | + |
|
| 314 | + $this->help['inline'] = $this->app['former.framework']->createHelp($help, $attributes); |
|
| 315 | + } |
|
| 316 | + |
|
| 317 | + /** |
|
| 318 | + * Add an block help |
|
| 319 | + * |
|
| 320 | + * @param string $help The help text |
|
| 321 | + * @param array $attributes Facultative attributes |
|
| 322 | + */ |
|
| 323 | + public function blockHelp($help, $attributes = array()) |
|
| 324 | + { |
|
| 325 | + // Reserved method |
|
| 326 | + if ($this->app['former.framework']->isnt('TwitterBootstrap') && |
|
| 327 | + $this->app['former.framework']->isnt('TwitterBootstrap3') && |
|
| 328 | + $this->app['former.framework']->isnt('TwitterBootstrap4') && |
|
| 329 | + $this->app['former.framework']->isnt('TwitterBootstrap5') |
|
| 330 | + ) { |
|
| 331 | + throw new BadMethodCallException('This method is only available on the Bootstrap framework'); |
|
| 332 | + } |
|
| 333 | + |
|
| 334 | + // If no help text, do nothing |
|
| 335 | + if (!$help) { |
|
| 336 | + return false; |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + $this->help['block'] = $this->app['former.framework']->createBlockHelp($help, $attributes); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + //////////////////////////////////////////////////////////////////// |
|
| 343 | + ///////////////////////// PREPEND/APPEND METHODS /////////////////// |
|
| 344 | + //////////////////////////////////////////////////////////////////// |
|
| 345 | + |
|
| 346 | + /** |
|
| 347 | + * Prepend elements to the field |
|
| 348 | + */ |
|
| 349 | + public function prepend() |
|
| 350 | + { |
|
| 351 | + $this->placeAround(func_get_args(), 'prepend'); |
|
| 352 | + } |
|
| 353 | + |
|
| 354 | + /** |
|
| 355 | + * Append elements to the field |
|
| 356 | + */ |
|
| 357 | + public function append() |
|
| 358 | + { |
|
| 359 | + $this->placeAround(func_get_args(), 'append'); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Prepends an icon to a field |
|
| 364 | + * |
|
| 365 | + * @param string $icon The icon to prepend |
|
| 366 | + * @param array $attributes Its attributes |
|
| 367 | + */ |
|
| 368 | + public function prependIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 369 | + { |
|
| 370 | + $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 371 | + |
|
| 372 | + $this->prepend($icon); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Append an icon to a field |
|
| 377 | + * |
|
| 378 | + * @param string $icon The icon to prepend |
|
| 379 | + * @param array $attributes Its attributes |
|
| 380 | + */ |
|
| 381 | + public function appendIcon($icon, $attributes = array(), $iconSettings = array()) |
|
| 382 | + { |
|
| 383 | + $icon = $this->app['former.framework']->createIcon($icon, $attributes, $iconSettings); |
|
| 384 | + |
|
| 385 | + $this->append($icon); |
|
| 386 | + } |
|
| 387 | + |
|
| 388 | + //////////////////////////////////////////////////////////////////// |
|
| 389 | + //////////////////////////////// HELPERS /////////////////////////// |
|
| 390 | + //////////////////////////////////////////////////////////////////// |
|
| 391 | + |
|
| 392 | + /** |
|
| 393 | + * Get the errors for the group |
|
| 394 | + * |
|
| 395 | + * @return string |
|
| 396 | + */ |
|
| 397 | + public function getErrors() |
|
| 398 | + { |
|
| 399 | + $errors = ''; |
|
| 400 | + |
|
| 401 | + if (!self::$opened) { |
|
| 402 | + |
|
| 403 | + // for non-custom groups, normal error handling applies |
|
| 404 | + $errors = $this->app['former']->getErrors(); |
|
| 405 | + } elseif (!empty($this->validations)) { |
|
| 406 | + |
|
| 407 | + // error handling only when validations specified for custom groups |
|
| 408 | + foreach ($this->validations as $validation) { |
|
| 409 | + $errors .= $this->app['former']->getErrors($validation); |
|
| 410 | + } |
|
| 411 | + } |
|
| 412 | + |
|
| 413 | + return $errors; |
|
| 414 | + } |
|
| 415 | + |
|
| 416 | + /** |
|
| 417 | + * Wraps content in a group |
|
| 418 | + * |
|
| 419 | + * @param string $contents The content |
|
| 420 | + * @param string $label The label to add |
|
| 421 | + * |
|
| 422 | + * @return string A group |
|
| 423 | + */ |
|
| 424 | + public function wrap($contents, $label = null) |
|
| 425 | + { |
|
| 426 | + $group = $this->open(); |
|
| 427 | + $group .= $label; |
|
| 428 | + $group .= $this->app['former.framework']->wrapField($contents); |
|
| 429 | + $group .= $this->close(); |
|
| 430 | + |
|
| 431 | + return $group; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Prints out the current label |
|
| 436 | + * |
|
| 437 | + * @param string $field The field to create a label for |
|
| 438 | + * |
|
| 439 | + * @return string A <label> tag |
|
| 440 | + */ |
|
| 441 | + protected function getLabel($field = null) |
|
| 442 | + { |
|
| 443 | + // Don't create a label if none exist |
|
| 444 | + if (!$field or !$this->label) { |
|
| 445 | + return null; |
|
| 446 | + } |
|
| 447 | + |
|
| 448 | + // Wrap label in framework classes |
|
| 449 | + $labelClasses = $this->app['former.framework']->getLabelClasses(); |
|
| 450 | + if ($field->isCheckable() && |
|
| 451 | + in_array($this->app['former']->framework(), ['TwitterBootstrap4', 'TwitterBootstrap5']) && |
|
| 452 | + $this->app['former.form']->isOfType('horizontal') |
|
| 453 | + ) { |
|
| 454 | + $labelClasses = array_merge($labelClasses, array('pt-0')); |
|
| 455 | + } |
|
| 456 | + $this->label->addClass($labelClasses); |
|
| 457 | + $this->label = $this->app['former.framework']->createLabelOf($field, $this->label); |
|
| 458 | + $this->label = $this->app['former.framework']->wrapLabel($this->label); |
|
| 459 | + |
|
| 460 | + return $this->label; |
|
| 461 | + } |
|
| 462 | + |
|
| 463 | + /** |
|
| 464 | + * Prints out the current help |
|
| 465 | + * |
|
| 466 | + * @return string A .help-block or .help-inline |
|
| 467 | + */ |
|
| 468 | + protected function getHelp() |
|
| 469 | + { |
|
| 470 | + $inline = Arr::get($this->help, 'inline'); |
|
| 471 | + $block = Arr::get($this->help, 'block'); |
|
| 472 | + |
|
| 473 | + // Replace help text with error if any found |
|
| 474 | + $errors = $this->app['former']->getErrors(); |
|
| 475 | + if ($errors and $this->app['former']->getOption('error_messages')) { |
|
| 476 | + $inline = $this->app['former.framework']->createValidationError($errors); |
|
| 477 | + } |
|
| 478 | + |
|
| 479 | + return join(null, array($inline, $block)); |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + /** |
|
| 483 | + * Format the field with prepended/appended elements |
|
| 484 | + * |
|
| 485 | + * @param Field $field The field to format |
|
| 486 | + * |
|
| 487 | + * @return string Field plus supplementary elements |
|
| 488 | + */ |
|
| 489 | + protected function prependAppend($field) |
|
| 490 | + { |
|
| 491 | + if (!$this->prepend and !$this->append) { |
|
| 492 | + return $field->render(); |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + return $this->app['former.framework']->prependAppend($field, $this->prepend, $this->append); |
|
| 496 | + } |
|
| 497 | + |
|
| 498 | + /** |
|
| 499 | + * Place elements around the field |
|
| 500 | + * |
|
| 501 | + * @param array $items An array of items to place |
|
| 502 | + * @param string $place Where they should end up (prepend|append) |
|
| 503 | + */ |
|
| 504 | + protected function placeAround($items, $place) |
|
| 505 | + { |
|
| 506 | + // Iterate over the items and place them where they should |
|
| 507 | + foreach ((array) $items as $item) { |
|
| 508 | + $item = $this->app['former.framework']->placeAround($item, $place); |
|
| 509 | + $this->{$place}[] = $item; |
|
| 510 | + } |
|
| 511 | + } |
|
| 512 | 512 | } |
@@ -13,474 +13,474 @@ |
||
| 13 | 13 | */ |
| 14 | 14 | class TwitterBootstrap5 extends Framework implements FrameworkInterface |
| 15 | 15 | { |
| 16 | - /** |
|
| 17 | - * Form types that trigger special styling for this Framework |
|
| 18 | - * |
|
| 19 | - * @var array |
|
| 20 | - */ |
|
| 21 | - protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * The button types available |
|
| 25 | - * |
|
| 26 | - * @var array |
|
| 27 | - */ |
|
| 28 | - private $buttons = array( |
|
| 29 | - 'lg', |
|
| 30 | - 'sm', |
|
| 31 | - 'xs', |
|
| 32 | - 'block', |
|
| 33 | - 'link', |
|
| 34 | - 'primary', |
|
| 35 | - 'secondary', |
|
| 36 | - 'warning', |
|
| 37 | - 'danger', |
|
| 38 | - 'success', |
|
| 39 | - 'info', |
|
| 40 | - 'light', |
|
| 41 | - 'dark', |
|
| 42 | - ); |
|
| 43 | - |
|
| 44 | - /** |
|
| 45 | - * The field sizes available |
|
| 46 | - * |
|
| 47 | - * @var array |
|
| 48 | - */ |
|
| 49 | - private $fields = array( |
|
| 50 | - 'lg', |
|
| 51 | - 'sm', |
|
| 52 | - // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 53 | - // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 54 | - // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 55 | - // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 56 | - // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 57 | - // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 58 | - // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 59 | - // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 60 | - ); |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * The field states available |
|
| 64 | - * |
|
| 65 | - * @var array |
|
| 66 | - */ |
|
| 67 | - protected $states = array( |
|
| 68 | - 'is-invalid', |
|
| 69 | - ); |
|
| 70 | - |
|
| 71 | - /** |
|
| 72 | - * The default HTML tag used for icons |
|
| 73 | - * |
|
| 74 | - * @var string |
|
| 75 | - */ |
|
| 76 | - protected $iconTag = 'i'; |
|
| 77 | - |
|
| 78 | - /** |
|
| 79 | - * The default set for icon fonts |
|
| 80 | - * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
|
| 81 | - * |
|
| 82 | - * @var string |
|
| 83 | - */ |
|
| 84 | - protected $iconSet = 'fa'; |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * The default prefix icon names |
|
| 88 | - * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
|
| 89 | - * |
|
| 90 | - * @var string |
|
| 91 | - */ |
|
| 92 | - protected $iconPrefix = 'fa'; |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * Create a new TwitterBootstrap instance |
|
| 96 | - * |
|
| 97 | - * @param \Illuminate\Container\Container $app |
|
| 98 | - */ |
|
| 99 | - public function __construct(Container $app) |
|
| 100 | - { |
|
| 101 | - $this->app = $app; |
|
| 102 | - $this->setFrameworkDefaults(); |
|
| 103 | - } |
|
| 104 | - |
|
| 105 | - //////////////////////////////////////////////////////////////////// |
|
| 106 | - /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 107 | - //////////////////////////////////////////////////////////////////// |
|
| 108 | - |
|
| 109 | - /** |
|
| 110 | - * Filter buttons classes |
|
| 111 | - * |
|
| 112 | - * @param array $classes An array of classes |
|
| 113 | - * |
|
| 114 | - * @return string[] A filtered array |
|
| 115 | - */ |
|
| 116 | - public function filterButtonClasses($classes) |
|
| 117 | - { |
|
| 118 | - // Filter classes |
|
| 119 | - // $classes = array_intersect($classes, $this->buttons); |
|
| 120 | - |
|
| 121 | - // Prepend button type |
|
| 122 | - $classes = $this->prependWith($classes, 'btn-'); |
|
| 123 | - $classes[] = 'btn'; |
|
| 124 | - |
|
| 125 | - return $classes; |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Filter field classes |
|
| 130 | - * |
|
| 131 | - * @param array $classes An array of classes |
|
| 132 | - * |
|
| 133 | - * @return array A filtered array |
|
| 134 | - */ |
|
| 135 | - public function filterFieldClasses($classes) |
|
| 136 | - { |
|
| 137 | - // Filter classes |
|
| 138 | - $classes = array_intersect($classes, $this->fields); |
|
| 139 | - |
|
| 140 | - // Prepend field type |
|
| 141 | - $classes = array_map(function ($class) { |
|
| 142 | - return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 143 | - }, $classes); |
|
| 144 | - |
|
| 145 | - return $classes; |
|
| 146 | - } |
|
| 147 | - |
|
| 148 | - //////////////////////////////////////////////////////////////////// |
|
| 149 | - ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 150 | - //////////////////////////////////////////////////////////////////// |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * Framework error state |
|
| 154 | - * |
|
| 155 | - * @return string |
|
| 156 | - */ |
|
| 157 | - public function errorState() |
|
| 158 | - { |
|
| 159 | - return 'is-invalid'; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - /** |
|
| 163 | - * Returns corresponding inline class of a field |
|
| 164 | - * |
|
| 165 | - * @param Field $field |
|
| 166 | - * |
|
| 167 | - * @return string |
|
| 168 | - */ |
|
| 169 | - public function getInlineLabelClass($field) |
|
| 170 | - { |
|
| 171 | - $inlineClass = parent::getInlineLabelClass($field); |
|
| 172 | - if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) { |
|
| 173 | - $inlineClass = 'form-check-label'; |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - return $inlineClass; |
|
| 177 | - } |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * Set the fields width from a label width |
|
| 181 | - * |
|
| 182 | - * @param array $labelWidths |
|
| 183 | - */ |
|
| 184 | - protected function setFieldWidths($labelWidths) |
|
| 185 | - { |
|
| 186 | - $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 187 | - |
|
| 188 | - $viewports = $this->getFrameworkOption('viewports'); |
|
| 189 | - foreach ($labelWidths as $viewport => $columns) { |
|
| 190 | - if ($viewport) { |
|
| 191 | - $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 192 | - $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 193 | - $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns"; |
|
| 194 | - } |
|
| 195 | - } |
|
| 196 | - |
|
| 197 | - $this->labelWidth = ltrim($labelWidthClass); |
|
| 198 | - $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 199 | - $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - //////////////////////////////////////////////////////////////////// |
|
| 203 | - ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 204 | - //////////////////////////////////////////////////////////////////// |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Add classes to a field |
|
| 208 | - * |
|
| 209 | - * @param Field $field |
|
| 210 | - * @param array $classes The possible classes to add |
|
| 211 | - * |
|
| 212 | - * @return Field |
|
| 213 | - */ |
|
| 214 | - public function getFieldClasses(Field $field, $classes) |
|
| 215 | - { |
|
| 216 | - // Add inline class for checkables |
|
| 217 | - if ($field->isCheckable()) { |
|
| 218 | - // Adds correct checkbox input class when is a checkbox (or radio) |
|
| 219 | - $field->addClass('form-check-input'); |
|
| 220 | - $classes[] = 'form-check'; |
|
| 221 | - |
|
| 222 | - if (in_array('inline', $classes)) { |
|
| 223 | - $field->inline(); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - // Filter classes according to field type |
|
| 228 | - if ($field->isButton()) { |
|
| 229 | - $classes = $this->filterButtonClasses($classes); |
|
| 230 | - } else { |
|
| 231 | - $classes = $this->filterFieldClasses($classes); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - // Add form-control class for text-type, textarea and file fields |
|
| 235 | - // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 236 | - if (!$field->isCheckable() && !$field->isButton() && !in_array($field->getType(), [ |
|
| 237 | - 'plaintext', |
|
| 238 | - 'select', |
|
| 239 | - ]) && !in_array('form-control', $classes) |
|
| 240 | - ) { |
|
| 241 | - $classes[] = 'form-control'; |
|
| 242 | - } |
|
| 243 | - |
|
| 244 | - // Add form-select class for select fields |
|
| 245 | - if ($field->getType() === 'select' && !in_array('form-select', $classes)) { |
|
| 246 | - $classes[] = 'form-select'; |
|
| 247 | - } |
|
| 248 | - |
|
| 249 | - if ($this->app['former']->getErrors($field->getName())) { |
|
| 250 | - $classes[] = $this->errorState(); |
|
| 251 | - } |
|
| 252 | - |
|
| 253 | - return $this->addClassesToField($field, $classes); |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Add group classes |
|
| 258 | - * |
|
| 259 | - * @return string A list of group classes |
|
| 260 | - */ |
|
| 261 | - public function getGroupClasses() |
|
| 262 | - { |
|
| 263 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 264 | - return 'mb-3 row'; |
|
| 265 | - } else { |
|
| 266 | - return 'mb-3'; |
|
| 267 | - } |
|
| 268 | - } |
|
| 269 | - |
|
| 270 | - /** |
|
| 271 | - * Add label classes |
|
| 272 | - * |
|
| 273 | - * @return string[] An array of attributes with the label class |
|
| 274 | - */ |
|
| 275 | - public function getLabelClasses() |
|
| 276 | - { |
|
| 277 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 278 | - return array('col-form-label', $this->labelWidth); |
|
| 279 | - } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 280 | - return array('visually-hidden'); |
|
| 281 | - } else { |
|
| 282 | - return array('form-label'); |
|
| 283 | - } |
|
| 284 | - } |
|
| 285 | - |
|
| 286 | - /** |
|
| 287 | - * Add uneditable field classes |
|
| 288 | - * |
|
| 289 | - * @return string An array of attributes with the uneditable class |
|
| 290 | - */ |
|
| 291 | - public function getUneditableClasses() |
|
| 292 | - { |
|
| 293 | - return ''; |
|
| 294 | - } |
|
| 295 | - |
|
| 296 | - /** |
|
| 297 | - * Add plain text field classes |
|
| 298 | - * |
|
| 299 | - * @return string An array of attributes with the plain text class |
|
| 300 | - */ |
|
| 301 | - public function getPlainTextClasses() |
|
| 302 | - { |
|
| 303 | - return 'form-control-plaintext'; |
|
| 304 | - } |
|
| 305 | - |
|
| 306 | - /** |
|
| 307 | - * Add form class |
|
| 308 | - * |
|
| 309 | - * @param string $type The type of form to add |
|
| 310 | - * |
|
| 311 | - * @return string|null |
|
| 312 | - */ |
|
| 313 | - public function getFormClasses($type) |
|
| 314 | - { |
|
| 315 | - return $type ? 'form-'.$type : null; |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - /** |
|
| 319 | - * Add actions block class |
|
| 320 | - * |
|
| 321 | - * @return string|null |
|
| 322 | - */ |
|
| 323 | - public function getActionClasses() |
|
| 324 | - { |
|
| 325 | - if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 326 | - return 'mb-3 row'; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - return null; |
|
| 330 | - } |
|
| 331 | - |
|
| 332 | - //////////////////////////////////////////////////////////////////// |
|
| 333 | - //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 334 | - //////////////////////////////////////////////////////////////////// |
|
| 335 | - |
|
| 336 | - /** |
|
| 337 | - * Render an help text |
|
| 338 | - * |
|
| 339 | - * @param string $text |
|
| 340 | - * @param array $attributes |
|
| 341 | - * |
|
| 342 | - * @return Element |
|
| 343 | - */ |
|
| 344 | - public function createHelp($text, $attributes = array()) |
|
| 345 | - { |
|
| 346 | - return Element::create('small', $text, $attributes)->addClass('text-muted'); |
|
| 347 | - } |
|
| 348 | - |
|
| 349 | - /** |
|
| 350 | - * Render an validation error text |
|
| 351 | - * |
|
| 352 | - * @param string $text |
|
| 353 | - * @param array $attributes |
|
| 354 | - * |
|
| 355 | - * @return string |
|
| 356 | - */ |
|
| 357 | - public function createValidationError($text, $attributes = array()) |
|
| 358 | - { |
|
| 359 | - return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - /** |
|
| 363 | - * Render an help text |
|
| 364 | - * |
|
| 365 | - * @param string $text |
|
| 366 | - * @param array $attributes |
|
| 367 | - * |
|
| 368 | - * @return Element |
|
| 369 | - */ |
|
| 370 | - public function createBlockHelp($text, $attributes = array()) |
|
| 371 | - { |
|
| 372 | - return Element::create('small', $text, $attributes)->addClass('form-text text-muted'); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * Render a disabled field |
|
| 377 | - * |
|
| 378 | - * @param Field $field |
|
| 379 | - * |
|
| 380 | - * @return Element |
|
| 381 | - */ |
|
| 382 | - public function createDisabledField(Field $field) |
|
| 383 | - { |
|
| 384 | - return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 385 | - } |
|
| 386 | - |
|
| 387 | - /** |
|
| 388 | - * Render a plain text field |
|
| 389 | - * |
|
| 390 | - * @param Field $field |
|
| 391 | - * |
|
| 392 | - * @return Element |
|
| 393 | - */ |
|
| 394 | - public function createPlainTextField(Field $field) |
|
| 395 | - { |
|
| 396 | - $label = $field->getLabel(); |
|
| 397 | - if ($label) { |
|
| 398 | - $label->for(''); |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 402 | - } |
|
| 403 | - |
|
| 404 | - //////////////////////////////////////////////////////////////////// |
|
| 405 | - //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 406 | - //////////////////////////////////////////////////////////////////// |
|
| 407 | - |
|
| 408 | - /** |
|
| 409 | - * Wrap an item to be prepended or appended to the current field |
|
| 410 | - * |
|
| 411 | - * @return Element A wrapped item |
|
| 412 | - */ |
|
| 413 | - public function placeAround($item, $place = null) |
|
| 414 | - { |
|
| 415 | - // Render object |
|
| 416 | - if (is_object($item) and method_exists($item, '__toString')) { |
|
| 417 | - $item = $item->__toString(); |
|
| 418 | - } |
|
| 419 | - |
|
| 420 | - $items = (array) $item; |
|
| 421 | - $element = ''; |
|
| 422 | - foreach ($items as $item) { |
|
| 423 | - $hasButtonTag = strpos(ltrim($item), '<button') === 0; |
|
| 424 | - |
|
| 425 | - // Get class to use |
|
| 426 | - $class = $hasButtonTag ? '' : 'input-group-text'; |
|
| 427 | - |
|
| 428 | - $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class); |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - return $element; |
|
| 432 | - } |
|
| 433 | - |
|
| 434 | - /** |
|
| 435 | - * Wrap a field with prepended and appended items |
|
| 436 | - * |
|
| 437 | - * @param Field $field |
|
| 438 | - * @param array $prepend |
|
| 439 | - * @param array $append |
|
| 440 | - * |
|
| 441 | - * @return string A field concatented with prepended and/or appended items |
|
| 442 | - */ |
|
| 443 | - public function prependAppend($field, $prepend, $append) |
|
| 444 | - { |
|
| 445 | - $return = '<div class="input-group">'; |
|
| 446 | - $return .= join(null, $prepend); |
|
| 447 | - $return .= $field->render(); |
|
| 448 | - $return .= join(null, $append); |
|
| 449 | - $return .= '</div>'; |
|
| 450 | - |
|
| 451 | - return $return; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Wrap a field with potential additional tags |
|
| 456 | - * |
|
| 457 | - * @param Field $field |
|
| 458 | - * |
|
| 459 | - * @return Element A wrapped field |
|
| 460 | - */ |
|
| 461 | - public function wrapField($field) |
|
| 462 | - { |
|
| 463 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 464 | - return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 465 | - } |
|
| 466 | - |
|
| 467 | - return $field; |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Wrap actions block with potential additional tags |
|
| 472 | - * |
|
| 473 | - * @param Actions $actions |
|
| 474 | - * |
|
| 475 | - * @return string A wrapped actions block |
|
| 476 | - */ |
|
| 477 | - public function wrapActions($actions) |
|
| 478 | - { |
|
| 479 | - // For horizontal forms, we wrap the actions in a div |
|
| 480 | - if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 481 | - return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 482 | - } |
|
| 483 | - |
|
| 484 | - return $actions; |
|
| 485 | - } |
|
| 16 | + /** |
|
| 17 | + * Form types that trigger special styling for this Framework |
|
| 18 | + * |
|
| 19 | + * @var array |
|
| 20 | + */ |
|
| 21 | + protected $availableTypes = array('horizontal', 'vertical', 'inline'); |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * The button types available |
|
| 25 | + * |
|
| 26 | + * @var array |
|
| 27 | + */ |
|
| 28 | + private $buttons = array( |
|
| 29 | + 'lg', |
|
| 30 | + 'sm', |
|
| 31 | + 'xs', |
|
| 32 | + 'block', |
|
| 33 | + 'link', |
|
| 34 | + 'primary', |
|
| 35 | + 'secondary', |
|
| 36 | + 'warning', |
|
| 37 | + 'danger', |
|
| 38 | + 'success', |
|
| 39 | + 'info', |
|
| 40 | + 'light', |
|
| 41 | + 'dark', |
|
| 42 | + ); |
|
| 43 | + |
|
| 44 | + /** |
|
| 45 | + * The field sizes available |
|
| 46 | + * |
|
| 47 | + * @var array |
|
| 48 | + */ |
|
| 49 | + private $fields = array( |
|
| 50 | + 'lg', |
|
| 51 | + 'sm', |
|
| 52 | + // 'col-xs-1', 'col-xs-2', 'col-xs-3', 'col-xs-4', 'col-xs-5', 'col-xs-6', |
|
| 53 | + // 'col-xs-7', 'col-xs-8', 'col-xs-9', 'col-xs-10', 'col-xs-11', 'col-xs-12', |
|
| 54 | + // 'col-sm-1', 'col-sm-2', 'col-sm-3', 'col-sm-4', 'col-sm-5', 'col-sm-6', |
|
| 55 | + // 'col-sm-7', 'col-sm-8', 'col-sm-9', 'col-sm-10', 'col-sm-11', 'col-sm-12', |
|
| 56 | + // 'col-md-1', 'col-md-2', 'col-md-3', 'col-md-4', 'col-md-5', 'col-md-6', |
|
| 57 | + // 'col-md-7', 'col-md-8', 'col-md-9', 'col-md-10', 'col-md-11', 'col-md-12', |
|
| 58 | + // 'col-lg-1', 'col-lg-2', 'col-lg-3', 'col-lg-4', 'col-lg-5', 'col-lg-6', |
|
| 59 | + // 'col-lg-7', 'col-lg-8', 'col-lg-9', 'col-lg-10', 'col-lg-11', 'col-lg-12', |
|
| 60 | + ); |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * The field states available |
|
| 64 | + * |
|
| 65 | + * @var array |
|
| 66 | + */ |
|
| 67 | + protected $states = array( |
|
| 68 | + 'is-invalid', |
|
| 69 | + ); |
|
| 70 | + |
|
| 71 | + /** |
|
| 72 | + * The default HTML tag used for icons |
|
| 73 | + * |
|
| 74 | + * @var string |
|
| 75 | + */ |
|
| 76 | + protected $iconTag = 'i'; |
|
| 77 | + |
|
| 78 | + /** |
|
| 79 | + * The default set for icon fonts |
|
| 80 | + * By default Bootstrap 4 offers no fonts, but we'll add Font Awesome |
|
| 81 | + * |
|
| 82 | + * @var string |
|
| 83 | + */ |
|
| 84 | + protected $iconSet = 'fa'; |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * The default prefix icon names |
|
| 88 | + * Using Font Awesome 5, this can be 'fa' or 'fas' for solid, 'far' for regular |
|
| 89 | + * |
|
| 90 | + * @var string |
|
| 91 | + */ |
|
| 92 | + protected $iconPrefix = 'fa'; |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * Create a new TwitterBootstrap instance |
|
| 96 | + * |
|
| 97 | + * @param \Illuminate\Container\Container $app |
|
| 98 | + */ |
|
| 99 | + public function __construct(Container $app) |
|
| 100 | + { |
|
| 101 | + $this->app = $app; |
|
| 102 | + $this->setFrameworkDefaults(); |
|
| 103 | + } |
|
| 104 | + |
|
| 105 | + //////////////////////////////////////////////////////////////////// |
|
| 106 | + /////////////////////////// FILTER ARRAYS ////////////////////////// |
|
| 107 | + //////////////////////////////////////////////////////////////////// |
|
| 108 | + |
|
| 109 | + /** |
|
| 110 | + * Filter buttons classes |
|
| 111 | + * |
|
| 112 | + * @param array $classes An array of classes |
|
| 113 | + * |
|
| 114 | + * @return string[] A filtered array |
|
| 115 | + */ |
|
| 116 | + public function filterButtonClasses($classes) |
|
| 117 | + { |
|
| 118 | + // Filter classes |
|
| 119 | + // $classes = array_intersect($classes, $this->buttons); |
|
| 120 | + |
|
| 121 | + // Prepend button type |
|
| 122 | + $classes = $this->prependWith($classes, 'btn-'); |
|
| 123 | + $classes[] = 'btn'; |
|
| 124 | + |
|
| 125 | + return $classes; |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Filter field classes |
|
| 130 | + * |
|
| 131 | + * @param array $classes An array of classes |
|
| 132 | + * |
|
| 133 | + * @return array A filtered array |
|
| 134 | + */ |
|
| 135 | + public function filterFieldClasses($classes) |
|
| 136 | + { |
|
| 137 | + // Filter classes |
|
| 138 | + $classes = array_intersect($classes, $this->fields); |
|
| 139 | + |
|
| 140 | + // Prepend field type |
|
| 141 | + $classes = array_map(function ($class) { |
|
| 142 | + return Str::startsWith($class, 'col') ? $class : 'input-'.$class; |
|
| 143 | + }, $classes); |
|
| 144 | + |
|
| 145 | + return $classes; |
|
| 146 | + } |
|
| 147 | + |
|
| 148 | + //////////////////////////////////////////////////////////////////// |
|
| 149 | + ///////////////////// EXPOSE FRAMEWORK SPECIFICS /////////////////// |
|
| 150 | + //////////////////////////////////////////////////////////////////// |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * Framework error state |
|
| 154 | + * |
|
| 155 | + * @return string |
|
| 156 | + */ |
|
| 157 | + public function errorState() |
|
| 158 | + { |
|
| 159 | + return 'is-invalid'; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + /** |
|
| 163 | + * Returns corresponding inline class of a field |
|
| 164 | + * |
|
| 165 | + * @param Field $field |
|
| 166 | + * |
|
| 167 | + * @return string |
|
| 168 | + */ |
|
| 169 | + public function getInlineLabelClass($field) |
|
| 170 | + { |
|
| 171 | + $inlineClass = parent::getInlineLabelClass($field); |
|
| 172 | + if ($field->isOfType('checkbox', 'checkboxes', 'radio', 'radios')) { |
|
| 173 | + $inlineClass = 'form-check-label'; |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + return $inlineClass; |
|
| 177 | + } |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * Set the fields width from a label width |
|
| 181 | + * |
|
| 182 | + * @param array $labelWidths |
|
| 183 | + */ |
|
| 184 | + protected function setFieldWidths($labelWidths) |
|
| 185 | + { |
|
| 186 | + $labelWidthClass = $fieldWidthClass = $fieldOffsetClass = ''; |
|
| 187 | + |
|
| 188 | + $viewports = $this->getFrameworkOption('viewports'); |
|
| 189 | + foreach ($labelWidths as $viewport => $columns) { |
|
| 190 | + if ($viewport) { |
|
| 191 | + $labelWidthClass .= " col-$viewports[$viewport]-$columns"; |
|
| 192 | + $fieldWidthClass .= " col-$viewports[$viewport]-".(12 - $columns); |
|
| 193 | + $fieldOffsetClass .= " col-$viewports[$viewport]-offset-$columns"; |
|
| 194 | + } |
|
| 195 | + } |
|
| 196 | + |
|
| 197 | + $this->labelWidth = ltrim($labelWidthClass); |
|
| 198 | + $this->fieldWidth = ltrim($fieldWidthClass); |
|
| 199 | + $this->fieldOffset = ltrim($fieldOffsetClass); |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + //////////////////////////////////////////////////////////////////// |
|
| 203 | + ///////////////////////////// ADD CLASSES ////////////////////////// |
|
| 204 | + //////////////////////////////////////////////////////////////////// |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Add classes to a field |
|
| 208 | + * |
|
| 209 | + * @param Field $field |
|
| 210 | + * @param array $classes The possible classes to add |
|
| 211 | + * |
|
| 212 | + * @return Field |
|
| 213 | + */ |
|
| 214 | + public function getFieldClasses(Field $field, $classes) |
|
| 215 | + { |
|
| 216 | + // Add inline class for checkables |
|
| 217 | + if ($field->isCheckable()) { |
|
| 218 | + // Adds correct checkbox input class when is a checkbox (or radio) |
|
| 219 | + $field->addClass('form-check-input'); |
|
| 220 | + $classes[] = 'form-check'; |
|
| 221 | + |
|
| 222 | + if (in_array('inline', $classes)) { |
|
| 223 | + $field->inline(); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + // Filter classes according to field type |
|
| 228 | + if ($field->isButton()) { |
|
| 229 | + $classes = $this->filterButtonClasses($classes); |
|
| 230 | + } else { |
|
| 231 | + $classes = $this->filterFieldClasses($classes); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + // Add form-control class for text-type, textarea and file fields |
|
| 235 | + // As text-type is open-ended we instead exclude those that shouldn't receive the class |
|
| 236 | + if (!$field->isCheckable() && !$field->isButton() && !in_array($field->getType(), [ |
|
| 237 | + 'plaintext', |
|
| 238 | + 'select', |
|
| 239 | + ]) && !in_array('form-control', $classes) |
|
| 240 | + ) { |
|
| 241 | + $classes[] = 'form-control'; |
|
| 242 | + } |
|
| 243 | + |
|
| 244 | + // Add form-select class for select fields |
|
| 245 | + if ($field->getType() === 'select' && !in_array('form-select', $classes)) { |
|
| 246 | + $classes[] = 'form-select'; |
|
| 247 | + } |
|
| 248 | + |
|
| 249 | + if ($this->app['former']->getErrors($field->getName())) { |
|
| 250 | + $classes[] = $this->errorState(); |
|
| 251 | + } |
|
| 252 | + |
|
| 253 | + return $this->addClassesToField($field, $classes); |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Add group classes |
|
| 258 | + * |
|
| 259 | + * @return string A list of group classes |
|
| 260 | + */ |
|
| 261 | + public function getGroupClasses() |
|
| 262 | + { |
|
| 263 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 264 | + return 'mb-3 row'; |
|
| 265 | + } else { |
|
| 266 | + return 'mb-3'; |
|
| 267 | + } |
|
| 268 | + } |
|
| 269 | + |
|
| 270 | + /** |
|
| 271 | + * Add label classes |
|
| 272 | + * |
|
| 273 | + * @return string[] An array of attributes with the label class |
|
| 274 | + */ |
|
| 275 | + public function getLabelClasses() |
|
| 276 | + { |
|
| 277 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 278 | + return array('col-form-label', $this->labelWidth); |
|
| 279 | + } elseif ($this->app['former.form']->isOfType('inline')) { |
|
| 280 | + return array('visually-hidden'); |
|
| 281 | + } else { |
|
| 282 | + return array('form-label'); |
|
| 283 | + } |
|
| 284 | + } |
|
| 285 | + |
|
| 286 | + /** |
|
| 287 | + * Add uneditable field classes |
|
| 288 | + * |
|
| 289 | + * @return string An array of attributes with the uneditable class |
|
| 290 | + */ |
|
| 291 | + public function getUneditableClasses() |
|
| 292 | + { |
|
| 293 | + return ''; |
|
| 294 | + } |
|
| 295 | + |
|
| 296 | + /** |
|
| 297 | + * Add plain text field classes |
|
| 298 | + * |
|
| 299 | + * @return string An array of attributes with the plain text class |
|
| 300 | + */ |
|
| 301 | + public function getPlainTextClasses() |
|
| 302 | + { |
|
| 303 | + return 'form-control-plaintext'; |
|
| 304 | + } |
|
| 305 | + |
|
| 306 | + /** |
|
| 307 | + * Add form class |
|
| 308 | + * |
|
| 309 | + * @param string $type The type of form to add |
|
| 310 | + * |
|
| 311 | + * @return string|null |
|
| 312 | + */ |
|
| 313 | + public function getFormClasses($type) |
|
| 314 | + { |
|
| 315 | + return $type ? 'form-'.$type : null; |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + /** |
|
| 319 | + * Add actions block class |
|
| 320 | + * |
|
| 321 | + * @return string|null |
|
| 322 | + */ |
|
| 323 | + public function getActionClasses() |
|
| 324 | + { |
|
| 325 | + if ($this->app['former.form']->isOfType('horizontal') || $this->app['former.form']->isOfType('inline')) { |
|
| 326 | + return 'mb-3 row'; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + return null; |
|
| 330 | + } |
|
| 331 | + |
|
| 332 | + //////////////////////////////////////////////////////////////////// |
|
| 333 | + //////////////////////////// RENDER BLOCKS ///////////////////////// |
|
| 334 | + //////////////////////////////////////////////////////////////////// |
|
| 335 | + |
|
| 336 | + /** |
|
| 337 | + * Render an help text |
|
| 338 | + * |
|
| 339 | + * @param string $text |
|
| 340 | + * @param array $attributes |
|
| 341 | + * |
|
| 342 | + * @return Element |
|
| 343 | + */ |
|
| 344 | + public function createHelp($text, $attributes = array()) |
|
| 345 | + { |
|
| 346 | + return Element::create('small', $text, $attributes)->addClass('text-muted'); |
|
| 347 | + } |
|
| 348 | + |
|
| 349 | + /** |
|
| 350 | + * Render an validation error text |
|
| 351 | + * |
|
| 352 | + * @param string $text |
|
| 353 | + * @param array $attributes |
|
| 354 | + * |
|
| 355 | + * @return string |
|
| 356 | + */ |
|
| 357 | + public function createValidationError($text, $attributes = array()) |
|
| 358 | + { |
|
| 359 | + return Element::create('div', $text, $attributes)->addClass('invalid-feedback'); |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + /** |
|
| 363 | + * Render an help text |
|
| 364 | + * |
|
| 365 | + * @param string $text |
|
| 366 | + * @param array $attributes |
|
| 367 | + * |
|
| 368 | + * @return Element |
|
| 369 | + */ |
|
| 370 | + public function createBlockHelp($text, $attributes = array()) |
|
| 371 | + { |
|
| 372 | + return Element::create('small', $text, $attributes)->addClass('form-text text-muted'); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * Render a disabled field |
|
| 377 | + * |
|
| 378 | + * @param Field $field |
|
| 379 | + * |
|
| 380 | + * @return Element |
|
| 381 | + */ |
|
| 382 | + public function createDisabledField(Field $field) |
|
| 383 | + { |
|
| 384 | + return Element::create('span', $field->getValue(), $field->getAttributes()); |
|
| 385 | + } |
|
| 386 | + |
|
| 387 | + /** |
|
| 388 | + * Render a plain text field |
|
| 389 | + * |
|
| 390 | + * @param Field $field |
|
| 391 | + * |
|
| 392 | + * @return Element |
|
| 393 | + */ |
|
| 394 | + public function createPlainTextField(Field $field) |
|
| 395 | + { |
|
| 396 | + $label = $field->getLabel(); |
|
| 397 | + if ($label) { |
|
| 398 | + $label->for(''); |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + return Element::create('div', $field->getValue(), $field->getAttributes()); |
|
| 402 | + } |
|
| 403 | + |
|
| 404 | + //////////////////////////////////////////////////////////////////// |
|
| 405 | + //////////////////////////// WRAP BLOCKS /////////////////////////// |
|
| 406 | + //////////////////////////////////////////////////////////////////// |
|
| 407 | + |
|
| 408 | + /** |
|
| 409 | + * Wrap an item to be prepended or appended to the current field |
|
| 410 | + * |
|
| 411 | + * @return Element A wrapped item |
|
| 412 | + */ |
|
| 413 | + public function placeAround($item, $place = null) |
|
| 414 | + { |
|
| 415 | + // Render object |
|
| 416 | + if (is_object($item) and method_exists($item, '__toString')) { |
|
| 417 | + $item = $item->__toString(); |
|
| 418 | + } |
|
| 419 | + |
|
| 420 | + $items = (array) $item; |
|
| 421 | + $element = ''; |
|
| 422 | + foreach ($items as $item) { |
|
| 423 | + $hasButtonTag = strpos(ltrim($item), '<button') === 0; |
|
| 424 | + |
|
| 425 | + // Get class to use |
|
| 426 | + $class = $hasButtonTag ? '' : 'input-group-text'; |
|
| 427 | + |
|
| 428 | + $element .= $hasButtonTag ? $item : Element::create('span', $item)->addClass($class); |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + return $element; |
|
| 432 | + } |
|
| 433 | + |
|
| 434 | + /** |
|
| 435 | + * Wrap a field with prepended and appended items |
|
| 436 | + * |
|
| 437 | + * @param Field $field |
|
| 438 | + * @param array $prepend |
|
| 439 | + * @param array $append |
|
| 440 | + * |
|
| 441 | + * @return string A field concatented with prepended and/or appended items |
|
| 442 | + */ |
|
| 443 | + public function prependAppend($field, $prepend, $append) |
|
| 444 | + { |
|
| 445 | + $return = '<div class="input-group">'; |
|
| 446 | + $return .= join(null, $prepend); |
|
| 447 | + $return .= $field->render(); |
|
| 448 | + $return .= join(null, $append); |
|
| 449 | + $return .= '</div>'; |
|
| 450 | + |
|
| 451 | + return $return; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Wrap a field with potential additional tags |
|
| 456 | + * |
|
| 457 | + * @param Field $field |
|
| 458 | + * |
|
| 459 | + * @return Element A wrapped field |
|
| 460 | + */ |
|
| 461 | + public function wrapField($field) |
|
| 462 | + { |
|
| 463 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 464 | + return Element::create('div', $field)->addClass($this->fieldWidth); |
|
| 465 | + } |
|
| 466 | + |
|
| 467 | + return $field; |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Wrap actions block with potential additional tags |
|
| 472 | + * |
|
| 473 | + * @param Actions $actions |
|
| 474 | + * |
|
| 475 | + * @return string A wrapped actions block |
|
| 476 | + */ |
|
| 477 | + public function wrapActions($actions) |
|
| 478 | + { |
|
| 479 | + // For horizontal forms, we wrap the actions in a div |
|
| 480 | + if ($this->app['former.form']->isOfType('horizontal')) { |
|
| 481 | + return Element::create('div', $actions)->addClass(array($this->fieldOffset, $this->fieldWidth)); |
|
| 482 | + } |
|
| 483 | + |
|
| 484 | + return $actions; |
|
| 485 | + } |
|
| 486 | 486 | } |