Skip to content

Commit

Permalink
Merge pull request #93 from UC-LeuvenLimburg/task/add-regex-to-form-v…
Browse files Browse the repository at this point in the history
…alidation

task/add-regex-to-form-validation
  • Loading branch information
Kenny-Lacroix authored May 5, 2020
2 parents 1627a55 + 34761d8 commit ba080db
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 10 deletions.
4 changes: 2 additions & 2 deletions app/Http/Requests/StoreLeaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ public function rules()
'rentable_id' => 'required|integer|min:1|exists:App\Models\Rentable,id',
'start_time' => 'required',
'end_time' => 'required',
'phone_nr' => 'required|string|min:8|max:12',
'license_plate' => 'required|string|min:1|max:9'
'phone_nr' => 'required|string|min:8|max:12|regex:/^(\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/', // Regex for phone number
'license_plate' => 'required|string|min:1|max:9|regex:/^[a-zA-Z0-9_.-]*$/' // Regex for license plate
];
}

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

class StoreRentableRequest extends FormRequest
{
/**
* Sanitize before rules()
*/
protected function sanitizeInput()
{
$input = $this->all();
$input['description'] = preg_replace("~[\p{M}]~uis", "", $this->input('description'));
$this->replace($input);
}

/**
* Get the validation rules that apply to the request.
*
Expand All @@ -17,12 +27,12 @@ public function rules()

return [
'user_id' => 'required|integer|min:1|exists:App\Models\User,id',
'adress' => 'required|string|min:3|max:150',
'adress' => 'required|string|min:3|max:150|regex:/^[a-zA-Z0-9_ .-]*$/', // Regex for Adress
'postal_code' => 'required|numeric|digits:4|min:1|max:9999',
'date_of_hire' => 'required|date_format:Y-m-d|after_or_equal:' . $todayDate,
'start_time' => 'required',
'end_time' => 'required',
'price' => 'required|numeric|min:0.01|max:1000',
'price' => 'required|numeric|min:0.01|max:1000|regex:/^[0-9]+(\.[0-9]{1,2})?$/', //Regex for Decimal with 2 decimal places
'bankaccount_nr' => 'required|string|regex:/^[A-Z]{2}(?:[ ]?[0-9]){14,20}$/', // Regex for IBAN numbers
'description' => 'required|string|max:150',
];
Expand All @@ -35,6 +45,8 @@ public function rules()
*/
protected function getValidatorInstance()
{
$this->sanitizeInput();

return parent::getValidatorInstance()->after(function () {
// convert to unix timestamps
$start_time = $this->input('start_time');
Expand Down
14 changes: 13 additions & 1 deletion app/Http/Requests/StoreUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class StoreUserRequest extends FormRequest
{
/**
* Sanitize before rules()
*/
protected function sanitizeInput()
{
$input = $this->all();
$input['name'] = preg_replace("~[\p{M}]~uis", "", $this->input('name'));
$this->replace($input);
}

/**
* Get the validation rules that apply to the request.
*
Expand All @@ -17,7 +27,7 @@ public function rules()
'name' => 'required|string|min:1|max:150',
'email' => 'required|email:rfc,dns',
'password' => 'required|string|min:8|max:128',
'role' => 'required|string|min:1|max:150',
'role' => 'required|string|min:1|max:150|regex:/^[a-zA-Z]+$/', // Regex for ASCII letters
];
}

Expand All @@ -28,6 +38,8 @@ public function rules()
*/
protected function getValidatorInstance()
{
$this->sanitizeInput();

return parent::getValidatorInstance()->after(function () {
// Check if password and confirm password match
if ($this->input('password') != $this->input('confirm_password')) {
Expand Down
4 changes: 2 additions & 2 deletions app/Http/Requests/UpdateLeaseRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ public function rules()
'rentable_id' => 'required|integer|min:1|exists:App\Models\Rentable,id',
'start_time' => 'required',
'end_time' => 'required',
'phone_nr' => 'required|string|min:8|max:12',
'license_plate' => 'required|string|min:1|max:9'
'phone_nr' => 'required|string|min:8|max:12|regex:/^(\+\d{1,2}\s?)?1?\-?\.?\s?\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/',
'license_plate' => 'required|string|min:1|max:9|regex:/^[a-zA-Z0-9_.-]*$/'
];
}

Expand Down
16 changes: 14 additions & 2 deletions app/Http/Requests/UpdateRentableRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ public function __construct(IRentableRepository $rentableRepo)
$this->rentableRepo = $rentableRepo;
}

/**
* Sanitize before rules()
*/
protected function sanitizeInput()
{
$input = $this->all();
$input['description'] = preg_replace("~[\p{M}]~uis", "", $this->input('description'));
$this->replace($input);
}

/**
* Get the validation rules that apply to the request.
*
Expand All @@ -25,12 +35,12 @@ public function rules()

return [
'rentable_id' => 'required|integer|min:1|exists:App\Models\Rentable,id',
'adress' => 'required|string|min:3|max:150',
'adress' => 'required|string|min:3|max:150|regex:/^[a-zA-Z0-9_ .-]*$/', // Regex for Adress
'postal_code' => 'required|numeric|digits:4|min:1|max:9999',
'date_of_hire' => 'required|date_format:Y-m-d|after_or_equal:' . $todayDate,
'start_time' => 'required',
'end_time' => 'required',
'price' => 'required|numeric|min:0.01|max:1000',
'price' => 'required|numeric|min:0.01|max:1000|regex:/^[0-9]+(\.[0-9]{1,2})?$/', //Regex for Decimal with 2 decimal places
'bankaccount_nr' => 'required|string|regex:/^[A-Z]{2}(?:[ ]?[0-9]){14,20}$/', // Regex for IBAN numbers
'description' => 'required|string|max:150',
];
Expand All @@ -43,6 +53,8 @@ public function rules()
*/
protected function getValidatorInstance()
{
$this->sanitizeInput();

return parent::getValidatorInstance()->after(function () {
// Get the current rentable
$rentable = $this->rentableRepo->getRentable($this->input('rentable_id'));
Expand Down
22 changes: 21 additions & 1 deletion app/Http/Requests/UpdateUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@

class UpdateUserRequest extends FormRequest
{
/**
* Sanitize before rules()
*/
protected function sanitizeInput()
{
$input = $this->all();
$input['name'] = preg_replace("~[\p{M}]~uis", "", $this->input('name'));
$this->replace($input);
}

/**
* Get the validation rules that apply to the request.
*
Expand All @@ -17,7 +27,17 @@ public function rules()
'name' => 'required|string|min:1|max:150',
'email' => 'email:rfc,dns',
'password' => 'string|min:8|max:128',
'role' => 'string|min:1|max:150',
'role' => 'string|min:1|max:150|regex:/^[a-zA-Z]+$/', // Regex for ASCII letters
];
}

/**
* Validate request
*
* @return Illuminate\Foundation\Http\FormRequest::getValidatorInstance
*/
protected function getValidatorInstance()
{
$this->sanitizeInput();
}
}

0 comments on commit ba080db

Please sign in to comment.