Laravel set a common validator for all date fields in the system - validation

I have different date fields in different models, I need to validate these date fields format on save of each model accordingly. is this possible?

Of course, you can. You just need to add this code below to your Model.
public static $rules = [
'date'=> 'reqired|date_format:MM:dd:YYYY' //if date is not required, ommite it
]
You can use different formats for your date in your different Models like MM:dd etc. Hope this helps you.
EDIT
To be able to use multiple date formats in a single validator You can define the multi-format date validation in your AppServiceProvider with the following code:
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Validator::extend('date_multi_format', function($attribute, $value, $formats) {
// iterate through all formats
foreach($formats as $format) {
// parse date with current format
$parsed = date_parse_from_format($format, $value);
// if value matches given format return true=validation succeeded
if ($parsed['error_count'] === 0 && $parsed['warning_count'] === 0) {
return true;
}
}
// value did not match any of the provided formats, so return false=validation failed
return false;
});
}
}
You can later use this new validation rule like that:
'date' => 'date_multi_format:"Y-m-d H:i:s.u","Y-m-d"' //or any other format
Hope this helps, thanks.

Related

Laravel Mutators and Accessors

I have created mutator date function in model to convert the created_at date into human readable time using diffForHumans().
I have done the following
public function setDateAttribute($value){
return \Carbon\Carbon::parse($value)->diffForHumans();
}
It works fine but it affects in all. it is possible to apply this mutator only on the specified function of the controller rather than all function
A small logic in the mutator would do the job:-
public function setDateAttribute($value){
if ( request()->path() === "/yourURL/To/IndexMethod"){
return $this->attributes['date'] = \Carbon\Carbon::parse($value)->diffForHumans();
} else {
return $this->attributes['date'] = $value;
}
}
the idea is to check by the url.
getting request path helper
EDIT
Comparison using route name is better, as exact path can contain id/slugs.
public function setDateAttribute($value){
if ( \Route::currentRouteName() === "/yourRouteName/To/IndexMethod"){
return $this->attributes['date'] = \Carbon\Carbon::parse($value)->diffForHumans();
} else {
return $this->attributes['date'] = $value;
}
}
getting route name

how to Automatically calculate expiry date in backpack laravel?

i have a field which name is month what i want is when user set the month it automatic calculate with created at month and then upload it to database.
PS - sorry i am new at laravel
Please see Edit:
my store method looks like
<?php
namespace App\Http\Controllers\Admin;
use Backpack\CRUD\app\Http\Controllers\CrudController;
// VALIDATION: change the requests to match your own file names if you need
form validation
use App\Http\Requests\ClientsRequest as StoreRequest;
use App\Http\Requests\ClientsRequest as UpdateRequest;
use Carbon\Carbon;
class ClientsCrudController extends CrudController
{
public function setup()
{
$this->crud->setModel('App\Models\Clients');
$this->crud->setRoute(config('backpack.base.route_prefix') . '/clients');
$this->crud->setEntityNameStrings('clients', 'clients');
$this->crud->setFromDb();
}
public function store(StoreRequest $request)
{
// your additional operations before save here
$start_day = Carbon::parse($request->created_at);
$expiry_day = $start_day->addMonths($request->month);
$request->input($expiry_day);
$redirect_location = parent::storeCrud($request);
// your additional operations after save here
// use $this->data['entry'] or $this->crud->entry
return $redirect_location;
}
public function update(UpdateRequest $request)
{
// your additional operations before save here
$redirect_location = parent::updateCrud($request);
// your additional operations after save here
// use $this->data['entry'] or $this->crud->entry
return $redirect_location;
}
}
Assuming that you mean months ammount ( i.e for a monthly payment )
You can do it like this in your store method i.e:
$start_day = Carbon::parse($request->created_at); //get a carbon instance with created_at as date
$expiry_day = $start_day->addMonths($request->user_selected_months); //add X months to created_at date
public function store(StoreRequest $request) {
date_default_timezone_set('asia/calcutta');
$month = $request->month;
$expiry_date = Carbon::now()->addMonths($month);
$request['expiry_date'] = $expiry_date;
}

timestampTz fields in Laravel

Laravel 5.4 supports the Postgres TIMESTAMP WITH TIME ZONE field type in migrations:
$table->timestampTz('scheduled_for');
Laravel can be set up to convert date fields (DATE, DATETIME, TIMESTAMP) into Carbon objects (and does so by default for the created_at and updated_at TIMESTAMP fields), but putting scheduled_for into the $dates field causes an error with the timezone-aware version:
InvalidArgumentException with message 'Trailing data'
Looking in the database and tinker, the field's value appears to be something like 2017-06-19 19:19:19-04. Is there a native way to get a Carbon object out of one of these field types? Or am I stuck using an accessor?
Resurrecting this question, hopefully with a helpful answer that gets accepted.
Laravel assumes a Y-m-d H:i:s database timestamp format. If you're using a Postgres timestampz column, that's obviously different. You need to tell Eloquent how to get Carbon to parse that format.
Simply define the $dateFormat property on your model like so:
Class MyModel extends Eloquent {
protected $dateFormat = 'Y-m-d H:i:sO';
}
Credit where credit is due: I found this solution in a GitHub issue
Put this inside your model
protected $casts = [
'scheduled_for' => 'datetime' // date | datetime | timestamp
];
Using $dates is more likely obsolete as $casts do the same stuff (maybe except $dateFormat attribute which can work only for $dates fields iirc, but I saw some complaining on it)
Edit
I was testing Carbon once on Laravel 5.4 and I created a trait for it
this is not production level code yet so include it in your model on your own risk
<?php namespace App\Traits;
use Carbon\Carbon;
trait castTrait
{
protected function castAttribute($key, $value)
{
$database_format = 'Y-m-d H:i:se'; // Store this somewhere in config files
$output_format_date = 'd/m/Y'; // Store this somewhere in config files
$output_format_datetime = 'd/m/Y H:i:s'; // Store this somewhere in config files
if (is_null($value)) {
return $value;
}
switch ($this->getCastType($key)) {
case 'int':
case 'integer':
return (int) $value;
case 'real':
case 'float':
case 'double':
return (float) $value;
case 'string':
return (string) $value;
case 'bool':
case 'boolean':
return (bool) $value;
case 'object':
return $this->fromJson($value, true);
case 'array':
case 'json':
return $this->fromJson($value);
case 'collection':
return new BaseCollection($this->fromJson($value));
case 'date':
Carbon::setToStringFormat($output_format_date);
$date = (string)$this->asDate($value);
Carbon::resetToStringFormat(); // Just for sure
return $date;
case 'datetime':
Carbon::setToStringFormat($output_format_datetime);
$datetime = (string)$this->asDateTime($value);
Carbon::resetToStringFormat();
return $datetime;
case 'timestamp':
return $this->asTimestamp($value);
default:
return $value;
}
}
/**
* Return a timestamp as DateTime object with time set to 00:00:00.
*
* #param mixed $value
* #return \Carbon\Carbon
*/
protected function asDate($value)
{
return $this->asDateTime($value)->startOfDay();
}
/**
* Return a timestamp as DateTime object.
*
* #param mixed $value
* #return \Carbon\Carbon
*/
protected function asDateTime($value)
{
$carbon = null;
$database_format = [ // This variable should also be in config file
'datetime' => 'Y-m-d H:i:se', // e -timezone
'date' => 'Y-m-d'
];
if(empty($value)) {
return null;
}
// If this value is already a Carbon instance, we shall just return it as is.
// This prevents us having to re-instantiate a Carbon instance when we know
// it already is one, which wouldn't be fulfilled by the DateTime check.
if ($value instanceof Carbon) {
$carbon = $value;
}
// If the value is already a DateTime instance, we will just skip the rest of
// these checks since they will be a waste of time, and hinder performance
// when checking the field. We will just return the DateTime right away.
if ($value instanceof DateTimeInterface) {
$carbon = new Carbon(
$value->format($database_format['datetime'], $value->getTimezone())
);
}
// If this value is an integer, we will assume it is a UNIX timestamp's value
// and format a Carbon object from this timestamp. This allows flexibility
// when defining your date fields as they might be UNIX timestamps here.
if (is_numeric($value)) {
$carbon = Carbon::createFromTimestamp($value);
}
// If the value is in simply year, month, day format, we will instantiate the
// Carbon instances from that format. Again, this provides for simple date
// fields on the database, while still supporting Carbonized conversion.
if ($this->isStandardDateFormat($value)) {
$carbon = Carbon::createFromFormat($database_format['date'], $value)->startOfDay();
}
// Finally, we will just assume this date is in the format used by default on
// the database connection and use that format to create the Carbon object
// that is returned back out to the developers after we convert it here.
$carbon = Carbon::createFromFormat(
$database_format['datetime'], $value
);
return $carbon;
}
}

LARAVEL 5 How to store empty DateTime input to Null value instead of 0000:00:00 00:00 value

i had two input Datetime field.
User can choose to fill in either these two field.
For the empty input Datetime field, i want it to store as Null value in Database.
But currently the empty input Datetime field is store as 0000:00:00 00:00 value in Database. What code should i modified?
you can use model observers to unset the attribute before saving when it is empty. But be sure that the fields are nullable
class Flight extends Model
{
public static function boot()
{
parent::boot();
self::saving(function ($flight_model) {
if(empty($flight_model->arriveDateTime)) unset($your_model->attributes['your_date_field1'];
if(empty($flight_model->departDateTime)) unset($your_model->attributes['your_date_field2'];
});
}
}
this discussion would be a good reference
Update
to do the limitation in the controller you'll use required_without_all:foo,bar,... which as described in doc.s
The field under validation must be present and not empty only when all of the other specified fields are not present
to use it we'll add new rules like
$rules = array(
'arriveDateTime' => 'required_without_all:departDateTime',
'departDateTime' => 'required_without_all:arriveDateTime',
);
$validator = Validator::make(Input::all(), $rules);
and if you're using rules already just append this two roles to them. that's for the controller validation part.
for the view part I'll assume you've two inputs with id="arriveDateTime" and id="departDateTime" the code would be like
$(function() {
<!-- if the first input value changed disable the second input -->
$('#arriveDateTime').on('change', function() {
if ($(this).val() == '') {
$('#departDateTime').prop("disabled", false);
} else {
$('#departDateTime').prop("disabled", true);
}
});
<!-- if the second input value changed disable the first input -->
$('#departDateTime').on('change', function() {
if ($(this).val() == '') {
$('#arriveDateTime').prop("disabled", false);
} else {
$('#arriveDateTime').prop("disabled", true);
}
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<input type="text" id="arriveDateTime" />
<input type="text" id="departDateTime" />
All you need is Eloquent Mutators.
Below a simplified example, basically from https://laravel.com/docs/5.4/eloquent-mutators#defining-a-mutator
class User extends Model
{
/* Set or mutate value of property */
public function setDateInputAttribute($value)
{
$this->attributes['date_input'] = $this->dateTimeOrNull($value);
}
/* This method should get another place, because it's not the core of this model */
private function dateTimeOrNull($value)
{
/*
Check if given datetime is valid. Yes? Return original $value
For simplicity, I use PHP's DateTime class
*/
if (DateTime::createFromFormat('Y-m-d H:i:s', $value) !== false) {
return $value;
}
/* Datetime is not valid. Return null */
return null;
}
}
Further
I suppose your datetime format is like yyyy-mm-dd hh:ii:ss
You can uses laravel mutators. Here's one for date of birth.
public function setDob($value)
{
$this->attributes['dob'] = strlen($value)? Carbon::createFromFormat('d/m/Y', $value) : null;
}

check if dates are not within range before saving in yii

how can i check if the dates posted in user's form exist in a range before saving? It's suppose to check different scenario date ranges. Below is just one of them. If it doesn't fall into range, it then posts.
is there a better way to do this?
$model = Table::model();
$criteria = new CDbCriteria;
//this is where i don't know how to get the values ($start,$end) from user before posting
$criteria->addCondition('start_date < '.$start);
$criteria->addCondition('end_date > '.$end);
You have to create custom validation function :
In rules add : this rules will work for both insert and update.
public function rules()
{
return array(
array('dateField', 'myCheckdate',),
);
}
In your custom validation function , you can apply your code to check the date range
public function myCheckdate($attribute,$params)
{
if(!$this->hasErrors())
{
if(Condition == false) // place your condition here
{
$this->addError($attribute,'The date is incorrect.');
}
}
}

Resources