Skip to content

Commit e8bfa1c

Browse files
authored
Merge pull request #27 from Lucalopezz/new_form_attributes
Novos atributos para os campos
2 parents bdb79ec + ac8c7f1 commit e8bfa1c

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

readme.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,36 @@ OBS.: Os arquivos são armazenados em storage/app/formsubmissions/<ano>/id
280280
* local-usp: campo tipo select que faz busca no replicado e retorna um local da usp já formatado. nome do campo recomendado: codlocusp;
281281
* data: data simples no formato dd/mm/aaaa;
282282
* text: texto simples (linha única);
283+
* pode passar `"maxlength": 100` para limitar a quantidade de caracteres;
284+
* number: campo numérico;
285+
* pode passar `"min"`, `"max"` e `"step"` para definir limites e incremento;
283286
* email: valida campos email;
284287
* select: precisa passar `options`;
285288
* textarea: parágrafos;
286289
* file: pode passar `"accept" : ".pdf, image/*"`;
287290

291+
### Atributos adicionais por campo
292+
293+
#### width para todos os campos
294+
295+
O atributo `width` define a largura do campo no grid do Bootstrap (`col-1` até `col-12`).
296+
297+
```json
298+
{
299+
"type": "text",
300+
"name": "nome",
301+
"width": 6
302+
}
303+
```
304+
305+
HTML gerado:
306+
307+
```html
308+
<div class="col-6">
309+
<input type="text" name="nome">
310+
</div>
311+
```
312+
288313
## Contribuindo
289314

290315
Contribuições são bem-vindas! Siga estes passos para contribuir:

resources/views/partials/default.blade.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
<label for="{{ $field['id'] }}">{{ $field['label'] }} {!! $field['requiredLabel'] !!}</label>
44

55
<input id="{{ $field['id'] }}" type="{{ $field['type'] }}" name="{{ $field['name'] }}" class="{{ $field['controlClass'] }}"
6-
value="{{ $field['value'] ?? $field['old'] }}" @required($field['required'])>
6+
value="{{ $field['value'] ?? $field['old'] }}"
7+
@if($field['type'] === 'text' && isset($field['maxlength'])) maxlength="{{ $field['maxlength'] }}" @endif
8+
@if($field['type'] === 'number' && isset($field['min'])) min="{{ $field['min'] }}" @endif
9+
@if($field['type'] === 'number' && isset($field['max'])) max="{{ $field['max'] }}" @endif
10+
@if($field['type'] === 'number' && isset($field['step'])) step="{{ $field['step'] }}" @endif
11+
@required($field['required'])>
712

813
</div>

src/Form.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ protected static function getFieldValidationRule($field)
239239
*/
240240
public function generateHtml(?string $formName = null, $formSubmission = null)
241241
{
242+
// pega pela definição
242243
if (!($this->definition = $this->getDefinition($formName ?? $this->name)) && !($this->definition = $formSubmission->formDefinition)) {
243244
return null;
244245
}
@@ -249,11 +250,27 @@ public function generateHtml(?string $formName = null, $formSubmission = null)
249250
// agrupando campos na mesma linha: igual para bs4 e bs5
250251
$fields .= '<div class="row">';
251252
foreach ($field as $f) {
252-
$fields .= '<div class="col">' . $this->generateField($f, $formSubmission) . '</div>';
253+
$colClass = 'col';
254+
if (isset($f['width']) && is_numeric($f['width'])) {
255+
$width = (int) $f['width'];
256+
if ($width >= 1 && $width <= 12) {
257+
$colClass = 'col-' . $width;
258+
}
259+
}
260+
261+
$fields .= '<div class="' . $colClass . '">' . $this->generateField($f, $formSubmission) . '</div>';
253262
}
254263
$fields .= '</div>';
255264
} else {
256265
// a linha possui um campo somente
266+
if (isset($field['width']) && is_numeric($field['width'])) {
267+
$width = (int) $field['width'];
268+
if ($width >= 1 && $width <= 12) {
269+
$fields .= '<div class="col-' . $width . '">' . $this->generateField($field, $formSubmission) . '</div>';
270+
continue;
271+
}
272+
}
273+
257274
$fields .= $this->generateField($field, $formSubmission);
258275
}
259276
}

0 commit comments

Comments
 (0)