Improve date validation

This commit is contained in:
Sebastian Schmidt 2018-10-04 13:42:46 +10:00
parent 7d1c1a1e6a
commit cda77d562a
No known key found for this signature in database
GPG Key ID: 80B9687901B6587C
7 changed files with 53 additions and 32 deletions

View File

@ -28,6 +28,7 @@ class MyBaseController extends Controller
'is_confirmed' => Auth::user()->is_confirmed,
],
'DateTimeFormat' => config('attendize.default_date_picker_format'),
'DateSeparator' => config('attendize.default_date_picker_seperator'),
'GenericErrorMessage' => trans("Controllers.whoops"),
]);
/*

View File

@ -16,18 +16,22 @@ class Event extends MyBaseModel
/**
* The validation rules.
*
* @var array $rules
* @return array $rules
*/
protected $rules = [
'title' => ['required'],
'description' => ['required'],
'location_venue_name' => ['required_without:venue_name_full'],
'venue_name_full' => ['required_without:location_venue_name'],
'start_date' => ['required|date'],
'end_date' => ['required|date'],
'organiser_name' => ['required_without:organiser_id'],
'event_image' => ['mimes:jpeg,jpg,png', 'max:3000'],
];
public function rules()
{
$format = config('attendize.default_datetime_format');
return [
'title' => 'required',
'description' => 'required',
'location_venue_name' => 'required_without:venue_name_full',
'venue_name_full' => 'required_without:location_venue_name',
'start_date' => 'required|date_format:"'.$format.'"',
'end_date' => 'required|date_format:"'.$format.'"',
'organiser_name' => 'required_without:organiser_id',
'event_image' => 'mimes:jpeg,jpg,png|max:3000',
];
}
/**
* The validation error messages.
@ -200,7 +204,8 @@ class Event extends MyBaseModel
*/
public function setStartDateAttribute($date)
{
$this->attributes['start_date'] = Carbon::parse($date);
$format = config('attendize.default_datetime_format');
$this->attributes['start_date'] = Carbon::createFromFormat($format, $date);
}
/**
@ -219,7 +224,8 @@ class Event extends MyBaseModel
*/
public function setEndDateAttribute($date)
{
$this->attributes['end_date'] = Carbon::parse($date);
$format = config('attendize.default_datetime_format');
$this->attributes['end_date'] = Carbon::createFromFormat($format, $date);
}
/**

View File

@ -86,7 +86,8 @@ class MyBaseModel extends \Illuminate\Database\Eloquent\Model
*/
public function validate($data)
{
$v = Validator::make($data, $this->rules, $this->messages, $this->attributes);
$rules = $this->rules ? is_array($rules = $this->rules) : $rules = $this->rules();
$v = Validator::make($data, $rules, $this->messages, $this->attributes);
if ($v->fails()) {
$this->errors = $v->messages();

View File

@ -15,16 +15,21 @@ class Ticket extends MyBaseModel
/**
* The rules to validate the model.
*
* @var array $rules
* @return array $rules
*/
public $rules = [
'title' => ['required'],
'price' => ['required', 'numeric', 'min:0'],
'description' => [],
'start_sale_date' => ['date'],
'end_sale_date' => ['date', 'after:start_sale_date'],
'quantity_available' => ['integer', 'min:0'],
];
public function rules()
{
$format = config('attendize.default_datetime_format');
return [
'title' => 'required',
'price' => 'required|numeric|min:0',
'description' => '',
'start_sale_date' => 'date_format:"'.$format.'"',
'end_sale_date' => 'date_format:"'.$format.'"|after:start_sale_date',
'quantity_available' => 'integer|min:0',
];
}
/**
* The validation error messages.
*
@ -84,7 +89,10 @@ class Ticket extends MyBaseModel
if (!$date) {
$this->attributes['start_sale_date'] = Carbon::now();
} else {
$this->attributes['start_sale_date'] = Carbon::parse($date);
$this->attributes['start_sale_date'] = Carbon::createFromFormat(
config('attendize.default_datetime_format'),
$date
);
}
}
@ -98,7 +106,10 @@ class Ticket extends MyBaseModel
if (!$date) {
$this->attributes['end_sale_date'] = null;
} else {
$this->attributes['end_sale_date'] = Carbon::parse($date);
$this->attributes['end_sale_date'] = Carbon::createFromFormat(
config('attendize.default_datetime_format'),
$date
);
}
}

View File

@ -12,7 +12,8 @@ $(function () {
var $div = $("<div>", {id: "DatePicker"});
$("body").append($div);
$div.DateTimePicker({
dateTimeFormat: Attendize.DateTimeFormat
dateTimeFormat: Attendize.DateTimeFormat,
dateSeparator: Attendize.DateSeparator
});
});
@ -505,7 +506,7 @@ function toggleSubmitDisabled($submitButton) {
}
/**
*
*
* @returns {{}}
*/
$.fn.serializeObject = function()

View File

@ -28,7 +28,8 @@
var $div = $("<div>", {id: "DatePicker"});
$("body").append($div);
$div.DateTimePicker({
dateTimeFormat: window.Attendize.DateTimeFormat
dateTimeFormat: window.Attendize.DateTimeFormat,
dateSeparator: window.Attendize.DateSeparator
});
});
@ -52,4 +53,4 @@
.in-form-link {
display: block; padding: 5px;margin-bottom: 5px;padding-left: 0;
}
</style>
</style>

View File

@ -1,13 +1,13 @@
@if(!$event->is_live)
<section id="goLiveBar">
<div class="container">
@if(!$event->is_live)
@if(!$event->is_live)
{{ @trans("ManageEvent.event_not_live") }}
<a href="{{ route('MakeEventLive' , ['event_id' => $event->id]) }}"
style="background-color: green; border-color: green;"
class="btn btn-success btn-xs">{{ @trans("ManageEvent.publish_it") }}</a>
@endif
@endif
</div>
</section>
@endif
@ -32,7 +32,7 @@
</span>
-
<span property="endDate" content="{{ $event->end_date->toIso8601String() }}">
@if($event->start_date->diffInHours($event->end_date) <= 12)
@if($event->start_date->diffInDays($event->end_date) == 0)
{{ $event->end_date->format('H:i') }}
@else
{{ $event->endDateFormatted() }}