Skip to content

Commit

Permalink
Change validation rules to string
Browse files Browse the repository at this point in the history
  • Loading branch information
burakcakirel committed Apr 29, 2021
1 parent 09f83c2 commit bee4056
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 37 deletions.
17 changes: 7 additions & 10 deletions app/Http/Requests/Auth/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@

namespace App\Http\Requests\Auth;

use App\Traits\Contacts;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class User extends FormRequest
{
use Contacts;

/**
* Determine if the user is authorized to make this request.
Expand All @@ -33,7 +30,7 @@ public function rules()
$picture = 'mimes:' . config('filesystems.mimes') . '|between:0,' . config('filesystems.max_size') * 1024;
}

$email = ['required', 'email'];
$email = 'required|email';

if ($this->getMethod() == 'PATCH') {
// Updating user
Expand All @@ -43,11 +40,11 @@ public function rules()
$roles = $this->user->can('read-auth-roles') ? 'required' : '';

if ($this->user->contact) {
$email[] = Rule::unique('contacts')
->ignore($this->user->contact->id)
->where('company_id', company_id())
->where('type', $this->getCustomerTypes())
->where('deleted_at');
$email .= '|unique:contacts,NULL,'
. $this->user->contact->id . ',id'
. ',company_id,' . company_id()
. ',type,customer'
. ',deleted_at,NULL';
}
} else {
// Creating user
Expand All @@ -57,7 +54,7 @@ public function rules()
$roles = 'required';
}

$email[] = Rule::unique('users')->ignore($id)->where('deleted_at');
$email .= '|unique:users,email,' . $id . ',id,deleted_at,NULL';

return [
'name' => 'required|string',
Expand Down
18 changes: 7 additions & 11 deletions app/Http/Requests/Common/Contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace App\Http\Requests\Common;

use App\Abstracts\Http\FormRequest;
use Illuminate\Validation\Rule;

class Contact extends FormRequest
{
Expand All @@ -24,7 +23,7 @@ public function authorize()
*/
public function rules()
{
$email = [];
$email = '';
$required = '';

$type = $this->request->get('type', 'customer');
Expand All @@ -44,17 +43,14 @@ public function rules()
}

if (!empty($this->request->get('email'))) {
$email[] = 'email';
$email[] = Rule::unique('contacts')
->ignore($id)
->where('company_id', $company_id)
->where('type', $type)
->where('deleted_at');
$email .= 'email|unique:contacts,NULL,'
. $id . ',id'
. ',company_id,' . $company_id
. ',type,' . $type
. ',deleted_at,NULL';

if (isset($model) && $this->$model->user_id) {
$email[] = Rule::unique('users')
->ignore($this->$model->user_id)
->where('deleted_at');
$email .= '|unique:users,NULL,' . $this->$model->user_id . ',id,deleted_at,NULL';
}
}

Expand Down
22 changes: 6 additions & 16 deletions app/Http/Requests/Portal/Profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@

namespace App\Http\Requests\Portal;

use App\Traits\Contacts;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class Profile extends FormRequest
{
use Contacts;

/**
* Determine if the user is authorized to make this request.
*
Expand All @@ -35,20 +31,14 @@ public function rules()
$picture = 'mimes:' . config('filesystems.mimes') . '|between:0,' . config('filesystems.max_size') * 1024;
}

$email = [
'required',
'email',
Rule::unique('users')
->ignore($id)
->where('deleted_at'),
];
$email = 'required|email|unique:users,email,' . $id . ',id,deleted_at,NULL';

if (user()->contact) {
$email[] = Rule::unique('contacts')
->ignore(user()->contact->id)
->where('company_id', company_id())
->where('type', $this->getCustomerTypes())
->where('deleted_at');
$email .= '|unique:contacts,NULL,'
. user()->contact->id . ',id'
. ',company_id,' . company_id()
. ',type,customer'
. ',deleted_at,NULL';
}

return [
Expand Down

0 comments on commit bee4056

Please sign in to comment.