Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/Http/Controllers/ChamadoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ public function store(ChamadoRequest $request, Fila $fila)
{
$this->authorize('chamados.create', $fila);

$request->validate(JSONForms::buildTextMaxLengthRules($request->extras, $fila));

# na criação não precisa
#$request->validate(JSONForms::buildRules($request, $fila));

Expand Down Expand Up @@ -303,6 +305,8 @@ public function update(Request $request, Chamado $chamado)
{
$this->authorize('chamados.update', $chamado);

$request->validate(JSONForms::buildTextMaxLengthRules($request->extras, $chamado->fila));

# inicialmente atende a atualização do campo anotacoes via ajax
if ($request->ajax()) {
$chamado->fill($request->all());
Expand Down
9 changes: 9 additions & 0 deletions app/Http/Controllers/FilaController.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,13 @@ public function storeTemplate(Request $request, Fila $fila)
$request->validate([
'template.*.label' => 'required',
'template.*.type' => 'required',
'template.*.maxlength' => 'nullable|integer|min:1',
]);
if (isset($request->campo)) {
$request->validate([
'new.label' => 'required',
'new.type' => 'required',
'new.maxlength' => 'nullable|integer|min:1',
]);
}
$template = [];
Expand All @@ -255,9 +257,16 @@ public function storeTemplate(Request $request, Fila $fila)
if ($atributo['type'] == 'select') {
$template[$campo]['value'] = json_decode($atributo['value'], true);
}

if (($atributo['type'] ?? '') != 'text') {
unset($template[$campo]['maxlength']);
}
}
# adiciona o campo novo
$new = array_filter($request->new, 'strlen');
if (($new['type'] ?? '') != 'text') {
unset($new['maxlength']);
}
if (isset($request->campo)) {
$template[$request->campo] = $new;
}
Expand Down
2 changes: 1 addition & 1 deletion app/Models/Fila.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static function getFields()
*/
public static function getTemplateFields()
{
return ['label', 'type', 'can', 'help', 'value', 'validate'];
return ['label', 'type', 'can', 'help', 'value', 'validate', 'maxlength'];
}

/**
Expand Down
40 changes: 40 additions & 0 deletions app/Utils/JSONForms.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ public static function buildRules($request, $fila)
return $validate;
}

/**
* Monta regras de limite de caracteres para campos extras do tipo texto.
*/
public static function buildTextMaxLengthRules($extras, $fila)
{
$template = json_decode($fila->template);
$validate = [];

if (!$template || !is_array($extras)) {
return $validate;
}

foreach ($template as $key => $json) {
if (($json->type ?? null) != 'text' || !isset($json->maxlength)) {
continue;
}

if (!array_key_exists($key, $extras)) {
continue;
}

$maxlength = filter_var($json->maxlength, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if ($maxlength === false) {
continue;
}

$field = 'extras.' . $key;
$validate[$field] = 'nullable|string|max:' . $maxlength;
}

return $validate;
}

/**
* Renderiza o formulário como array contendo html
*/
Expand Down Expand Up @@ -65,6 +98,13 @@ protected static function JSON2Form($template, $data, $perfil)
default:
$fieldInput = html()->textarea("extras[$key]", $value)->class('form-control')->rows(3);

if ($type == 'text' && isset($json->maxlength)) {
$maxlength = filter_var($json->maxlength, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1]]);
if ($maxlength !== false) {
$fieldInput = $fieldInput->attribute('maxlength', strval($maxlength));
}
}

if (isset($json->validate) && strpos($json->validate, 'required') !== false) {
$fieldInput = $fieldInput ->required();
}
Expand Down
19 changes: 19 additions & 0 deletions public/js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,22 @@ function mudarCampoInputTextarea(campo) {
});
}
}

/* @autor uspdev/alecosta 10/04/2026
* Habilita o campo de limite de caracteres apenas quando tipo é texto
*/
function toggleCampoMaxlength(campo) {
var fieldTypeSelect = $('select[name="' + campo + '"]').find(':selected').val();
var maxlengthField = $('input[name="' + campo.replace('][type]', '][maxlength]') + '"]');

maxlengthField.each(function () {
if (fieldTypeSelect == 'text') {
$(this).prop('disabled', false);
$(this).attr('placeholder', 'Opcional');
} else {
$(this).val('');
$(this).prop('disabled', true);
$(this).attr('placeholder', 'Apenas para Texto');
}
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
</select>
@break
@default
<input class="form-control col-9" name="new[{{ $field }}]">
<input class="form-control col-9"
name="new[{{ $field }}]"
@if ($field === 'maxlength') type="number" min="1" placeholder="Apenas para Texto" @endif
@if ($field === 'maxlength') disabled @endif>
@endswitch
</div>
@endforeach
Expand All @@ -70,6 +73,12 @@
json_modal_form = function() {
jsonForm.modal();
}

toggleCampoMaxlength('new[type]');

$('select[name="new[type]"]').on('change', function() {
toggleCampoMaxlength(this.name);
});
});

</script>
Expand Down
17 changes: 14 additions & 3 deletions resources/views/filas/template.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@isset($tvalue[$field])
@switch($field)
@case('type')
<select class="form-control" name="template[{{ $tkey }}][{{ $field }}]" onchange="javascript: mudarCampoInputTextarea(this.name);">
<select class="form-control" name="template[{{ $tkey }}][{{ $field }}]" onchange="javascript: mudarCampoInputTextarea(this.name); toggleCampoMaxlength(this.name);">
<option value='text' {{ $tvalue[$field] == 'text' ? 'selected' : '' }}>Texto</option>
<option value='select' {{ $tvalue[$field] == 'select' ? 'selected' : '' }}>Caixa de Seleção</option>
<option value='date' {{ $tvalue[$field] == 'date' ? 'selected' : '' }}>Data</option>
Expand All @@ -57,7 +57,14 @@
</select>
@break
@default
<input class="form-control" name="template[{{ $tkey }}][{{ $field }}]" value="{{ is_array($tvalue[$field]) ? json_encode($tvalue[$field], JSON_UNESCAPED_UNICODE) : $tvalue[$field] ?? '' }}">
@php
$maxLengthDisabled = $field === 'maxlength' && (($tvalue['type'] ?? '') !== 'text');
@endphp
<input class="form-control"
name="template[{{ $tkey }}][{{ $field }}]"
value="{{ is_array($tvalue[$field]) ? json_encode($tvalue[$field], JSON_UNESCAPED_UNICODE) : $tvalue[$field] ?? '' }}"
@if ($field === 'maxlength') type="number" min="1" placeholder="Apenas para Texto" @endif
@if ($maxLengthDisabled) disabled @endif>
@endswitch
@endisset
@empty($tvalue[$field])
Expand All @@ -76,7 +83,10 @@
</select>
@break
@default
<input class="form-control" name="template[{{ $tkey }}][{{ $field }}]" value="">
<input class="form-control"
name="template[{{ $tkey }}][{{ $field }}]"
value=""
@if ($field === 'maxlength') type="number" min="1" placeholder="Apenas para Texto" @endif>
@endswitch
@endempty
</div>
Expand Down Expand Up @@ -146,6 +156,7 @@ function move(r, up) {
var nameField = $(this).prop('name');
// muda o campo de input para caixa de texto
$(mudarCampoInputTextarea(nameField));
$(toggleCampoMaxlength(nameField));
});
});

Expand Down
20 changes: 20 additions & 0 deletions tests/Unit/JSONFormsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@ public function testGenerateFormHelp()
$this->assertStringContainsString("Local de atendimento", $form[0][2]->toHtml());
}

public function testGenerateFormTextMaxlength()
{
$tmp = $this->template;
$tmp["predio"]["maxlength"] = 10;
$template = json_encode($tmp);
$fila = Fila::factory()->make(['template' => $template]);
$form = JSONForms::generateForm($fila);
$this->assertStringContainsString('maxlength="10"', $form[0][1]->toHtml());
}

public function testBuildRules()
{
$tmp = $this->template;
Expand All @@ -149,4 +159,14 @@ public function testBuildRules()
$validate = JSONForms::buildRules($request, $fila);
$this->assertEquals($validate, ["extras.predio" => "required"]);
}

public function testBuildTextMaxLengthRules()
{
$tmp = $this->template;
$tmp["predio"]["maxlength"] = 10;
$template = json_encode($tmp);
$fila = Fila::factory()->make(['template' => $template]);
$validate = JSONForms::buildTextMaxLengthRules(["predio" => "Administracao"], $fila);
$this->assertEquals($validate, ["extras.predio" => "nullable|string|max:10"]);
}
}