[go: nahoru, domu]

Skip to content

Commit

Permalink
added new validations
Browse files Browse the repository at this point in the history
  • Loading branch information
dynamic11 committed Jul 31, 2019
1 parent b5f3462 commit a1172e4
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/Controllers/Http/RoomController.js
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ class RoomController {
async searchRooms ({ request, view }) {
const options = request.all();

const duration = Number(options.hour) * 60;
const duration = Number(options.duration) * 60;
const difference = Math.abs(moment.duration(moment(options.from, 'HH:mm').diff(moment(options.to, 'HH:mm'))).asMinutes());

if (duration === difference) {
Expand Down
65 changes: 65 additions & 0 deletions app/Validators/SearchFixed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/**
* File Name: SearchRoom.js
* Description: Validator used to validate input fields Search Room Page (Employee)
* Instructions: Use this validator by adding ".validator('SearchRoom')" to your route in /routes.js
**/
'use strict';

class SearchFixed {
// Validate and return all fields
get validateAll () {
return true;
}

// Validation rules
get rules () {
// getting the current date subract 1 day
let minDate = new Date();
minDate.setDate(minDate.getDate() - 1);
// getting the current date plus 3 months
let maxDate = new Date();
maxDate.setDate(maxDate.getDate() + 1);
maxDate.setMonth(maxDate.getMonth() + 3);

// Validation rules
return {
/**
* Date validation rules
*
* after: must be current date or after
* before: cannot be more than 3 months ahead of the current date
* date: checks if the input field is a valid date
* required: require field, cannot search without a date entered
*/
date: `required|date|after:${minDate}|before:${maxDate}`,
/**
* From and To validation rules
*
* isAfterToday: 'from' and 'to' fields must occur after the current time if the date field is the current date
* required: require field, cannot search without a date entered
*/
from: 'required|isAfterToday:date',
duration: 'required|range:0,24'

};
}

// Custom error messages
get messages () {
return {
'date.after': 'Please enter a time in the future',
'date.before': 'You can only book rooms up to 3 months ahead of time',
'date.date': 'Please enter a valid date',
'from.isAfterToday': 'This field must occur after the current time',
'required': 'This field is required'
};
}

async fails (error) {
console.log(error);
this.ctx.session.withErrors(error).flashAll();
return this.ctx.response.redirect('back');
}
}

module.exports = SearchFixed;
68 changes: 68 additions & 0 deletions app/Validators/SearchFlexible.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* File Name: SearchRoom.js
* Description: Validator used to validate input fields Search Room Page (Employee)
* Instructions: Use this validator by adding ".validator('SearchRoom')" to your route in /routes.js
**/
'use strict';

class SearchFlexible {
// Validate and return all fields
get validateAll () {
return true;
}

// Validation rules
get rules () {
// getting the current date subract 1 day
let afterDate = new Date();
afterDate.setDate(afterDate.getDate() - 1);
// getting the current date plus 3 months
let beforeDate = new Date();
beforeDate.setDate(beforeDate.getDate() + 1);
beforeDate.setMonth(beforeDate.getMonth() + 3);

// Validation rules
return {
/**
* Date validation rules
*
* after: must be current date or after
* before: cannot be more than 3 months ahead of the current date
* date: checks if the input field is a valid date
* required: require field, cannot search without a date entered
*/
date: `required|date|after:${afterDate}|before:${beforeDate}`,
/**
* From and To validation rules
*
* isAfter: the 'to' field must occur after the 'from' field (custom validator see CustomValidationProvidor.js for more)
* isAfterToday: 'from' and 'to' fields must occur after the current time if the date field is the current date
* isWithinRange: 'to' field cannot be more than X hours after 'from'
* required: require field, cannot search without a date entered
*/
from: 'required|isAfterToday:date',
to: 'required|isAfter:from|isAfterToday:date',
duration: 'required|range:0,24'

};
}

// Custom error messages
get messages () {
return {
'date.after': 'Please enter a time in the future',
'date.before': 'You can only book rooms up to 3 months ahead of time',
'date.date': 'Please enter a valid date',
'from.isAfterToday': 'This field must occur after the current time',
'to.isAfterToday': 'This field must occur after the current time',
'required': 'This field is required'
};
}

async fails (error) {
this.ctx.session.withErrors(error).flashAll();
return this.ctx.response.redirect('back');
}
}

module.exports = SearchFlexible;
11 changes: 7 additions & 4 deletions resources/views/partials/searchFixedForm.edge
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,14 @@
<label for='duration'>{{ antl.formatMessage('searchForm.duration') }} </label> <strong class='text-danger'>*</strong>
<div class='form-group'>
<div class='input-group mb-3'>
<input class='form-control duration-input' name='hour' type='number' min=0.5 max=10 step=0.5 value='{{ old('hour', '0.5') }}'>
<div class="input-group-append">
<span class="input-group-text duration-append">hr</span>
</div>
<input class='form-control duration-input' name='duration' type='number' min=0.5 max=10 step=0.5 value='{{ old('duration', '0.5') }}'>
<div class="input-group-append">
<span class="input-group-text duration-append">hr</span>
</div>
</div>
@if(hasErrorFor('from'))
<div id='from-error' class='invalid-feedback show-invalid'>{{ getErrorFor('duration')}}</div>
@endif
</div>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion start/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ Route.get('/userDash', 'HomeController.userDashboard').as('userDash').middleware
// Rendering Results
Route.get('/results', 'RoomController.findSpecific').as('results').middleware(['auth']).validator('SearchRoom').middleware(['isUser']);
Route.get('/recurringResults', 'RoomController.searchRecurring2').as('recurringResults');
Route.get('/search/fixed', 'RoomController.searchRooms').as('searchFixed').validator('SearchRoomFixed');
Route.get('/search/fixed', 'RoomController.searchRooms').as('searchFixed').validator('SearchFixed');
Route.get('/search/flexible', 'RoomController.searchRooms').as('searchFlexible').validator('SearchRoomFlexible');
Route.get('/search/recurring', 'RoomController.searchRooms').as('searchRecurring').validator('SearchRoomFlexible'); // TODO

Expand Down

0 comments on commit a1172e4

Please sign in to comment.