Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Validation for status label creation through the API #15713

Open
wants to merge 4 commits into
base: develop
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
7 changes: 1 addition & 6 deletions app/Http/Controllers/Api/StatuslabelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,6 @@ public function store(Request $request) : JsonResponse
$this->authorize('create', Statuslabel::class);
$request->except('deployable', 'pending', 'archived');

if (! $request->filled('type')) {

return response()->json(Helper::formatStandardApiResponse('error', null, ['type' => ['Status label type is required.']]));
}

$statuslabel = new Statuslabel;
$statuslabel->fill($request->all());

Expand All @@ -111,7 +106,7 @@ public function store(Request $request) : JsonResponse
$statuslabel->default_label = $request->input('default_label', 0);


if ($statuslabel->save()) {
if ($statuslabel->validate() && $statuslabel->save()) {
return response()->json(Helper::formatStandardApiResponse('success', $statuslabel, trans('admin/statuslabels/message.create.success')));
}
return response()->json(Helper::formatStandardApiResponse('error', null, $statuslabel->getErrors()));
Expand Down
16 changes: 16 additions & 0 deletions app/Models/Statuslabel.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use App\Models\Traits\Searchable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Validator;
use Watson\Validating\ValidatingTrait;

class Statuslabel extends SnipeModel
Expand Down Expand Up @@ -166,7 +167,22 @@ public static function getStatuslabelTypesForDB($type)

return $statustype;
}
public function validate()
{
$validator = Validator::make($this->attributes, $this->rules);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we do it this way, we're not actually checking for type though, no? We don't have that constraint in the model $rules:

    protected $rules = [
        'name'  => 'required|string|unique_undeleted',
        'notes'   => 'string|nullable',
        'deployable' => 'required',
        'pending' => 'required',
        'archived' => 'required',
    ];

since type isn't an actual field on the table.


if ($validator->fails()) {
$this->errors = $validator->errors();
return false;
}

return true;
}

public function getErrors()
{
return $this->errors;
}
public function scopeOrderByCreatedBy($query, $order)
{
return $query->leftJoin('users as admin_sort', 'status_labels.created_by', '=', 'admin_sort.id')->select('status_labels.*')->orderBy('admin_sort.first_name', $order)->orderBy('admin_sort.last_name', $order);
Expand Down
Loading