Skip to content

Commit

Permalink
Fix file name validation error on ISO creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ericwang401 committed Nov 11, 2023
1 parent 7b9dd75 commit bd915a6
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 17 deletions.
15 changes: 12 additions & 3 deletions app/Http/Requests/Admin/Nodes/Isos/StoreIsoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,28 @@ public function rules(): array
return $rules;
}

public function withValidator(Validator $validator)
public function after(): array
{
$rules = [
function (Validator $validator) {
if (ISO::where('file_name', $this->string('file_name'))->exists()) {
$validator->errors()->add('file_name', __('validation.unique', ['attribute' => 'file name']));
}
}
];

if (!$this->boolean('should_download')) {
$validator->after(function (Validator $validator) {
$rules[] = function (Validator $validator) {
$node = $this->parameter('node', Node::class);

$iso = app(IsoService::class)->getIso($node, $this->input('file_name'));

if (is_null($iso)) {
$validator->errors()->add('file_name', 'This ISO doesn\'t exist.');
}
});
};
}

return $rules;
}
}
13 changes: 0 additions & 13 deletions app/Http/Requests/Admin/Nodes/Isos/UpdateIsoRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,6 @@

class UpdateIsoRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules(): array
{
$rules = ISO::getRulesForUpdate($this->parameter('iso', ISO::class));
Expand Down
2 changes: 1 addition & 1 deletion app/Models/ISO.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ISO extends Model
'node_id' => 'required|integer|exists:nodes,id',
'is_successful' => 'sometimes|boolean',
'name' => 'required|string|min:1|max:40',
'file_name' => 'required|unique:iso_library,file_name|string|ends_with:.iso|max:191',
'file_name' => 'required|string|ends_with:.iso|max:191',
'size' => 'sometimes|numeric|min:0',
'hidden' => 'sometimes|boolean',
'completed_at' => 'nullable|date',
Expand Down

0 comments on commit bd915a6

Please sign in to comment.