Skip to content
This repository has been archived by the owner on May 9, 2021. It is now read-only.

Display labels instead of field names in error messages #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
Binary file removed .DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion resources/views/array-fields/error-help.blade.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@error($field->key . '.' . $key . '.' . $array_field->name)
<div class="invalid-feedback d-block" role="alert">
<strong>{{ $this->errorMessage($message) }}</strong>
<strong>{{ $message }}</strong>
</div>
@elseif($array_field->help)
<small class="form-text text-muted">{{ $array_field->help }}</small>
Expand Down
32 changes: 25 additions & 7 deletions src/FormComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,13 @@ public function fields()

public function updated($field)
{
$this->validateOnly($field, $this->rules(true));
$this->validateOnly($field, $this->rules(true), [], $this->attributes());
}

public function submit()
{
$this->validate($this->rules());

$this->validate($this->rules(), [], $this->attributes());

$field_names = [];
foreach ($this->fields() as $field) $field_names[] = $field->name;
Expand All @@ -75,11 +76,6 @@ public function submit()
$this->success();
}

public function errorMessage($message)
{
return str_replace('form data.', '', $message);
}

public function success()
{
$this->form_data['password'] = bcrypt($this->form_data['password']);
Expand Down Expand Up @@ -108,4 +104,26 @@ public function saveAndGoBackResponse()
{
return redirect()->route('users.index');
}

public function attributes()
{
$attributes = [];

/** @var Field $field */
foreach ($this->fields() as $field) {

if ($field->type === 'array') {

/** @var Field $arrayField */
foreach ($field->array_fields as $arrayField) {
$attributes[$field->key . '.*.' . $arrayField->name] = $arrayField->placeholder ?? $arrayField->name;
}

} else {
$attributes[$field->key] = $field->label;
}
}

return $attributes;
}
}