Laravel 模型中的日期验证
Laravel date validation in model
我试图不允许用户在特定日期保存数据。现在,即使用户选择了特定日期,我的代码也会导致用户无法保存数据。当日期不在特定日期时,如何让用户保存数据?
代码:
static::save(function($model)
{
//User can't save data between this date
$from = date('2018-01-01');
$to = date('2018-11-19');
//Disable save function between specific
if (User::whereBetween('date', [$from, $to])->get()&&
(!property_exists($model, 'always_writable') || !$model->always_writable)) {
$model->setErrors(trans('validation.read_only'));
return false;
}
return $model->validate();
});
带有 unix 时间戳的快速 maffs。
static::save(function($model)
{
//User can't save data between this date
$from = strtotime('2018-01-01');
$to = strtotime('2018-11-19');
//Disable save function between specific
if ( !(property_exists($model, 'always_writable')
&& $model->always_writable)
&& (time() >= $form && time() <=$to)){
$model->setErrors(trans('validation.read_only'));
return false;
}
return $model->validate();
});
编辑:添加了 always_writable
条件。
edit2:让你的 always_writable 检查实际上做了一些明智的事情。
您需要检查查询的计数:
//if count is greater than 0
static::save(function($model)
{
//User can't save data between this date
$from = date('2018-01-01');
$to = date('2018-11-19');
//Disable save function between specific
if (count(User::whereBetween('date', [$from, $to])->get()) > 0 &&
(!property_exists($model, 'always_writable') || !$model->always_writable)) {
$model->setErrors(trans('validation.read_only'));
return false;
}
return $model->validate();
});
不太确定您想要通过查询实现什么:
User::whereBetween('date', [$from, $to])->get()
这基本上让系统中的所有用户的日期列都在 $from 和 $to 之间。大概这将永远是真的。不应该是:
Auth::user()->whereBetween('date', [$from, $to])->count()
这意味着:当前登录用户的日期列是否在 $from 和 $to 之间?
选择:
$userDate = Auth::user()->date;
if (($userDate >= $from && $userDate <= $to) && ... ) {
...
}
我试图不允许用户在特定日期保存数据。现在,即使用户选择了特定日期,我的代码也会导致用户无法保存数据。当日期不在特定日期时,如何让用户保存数据?
代码:
static::save(function($model)
{
//User can't save data between this date
$from = date('2018-01-01');
$to = date('2018-11-19');
//Disable save function between specific
if (User::whereBetween('date', [$from, $to])->get()&&
(!property_exists($model, 'always_writable') || !$model->always_writable)) {
$model->setErrors(trans('validation.read_only'));
return false;
}
return $model->validate();
});
带有 unix 时间戳的快速 maffs。
static::save(function($model)
{
//User can't save data between this date
$from = strtotime('2018-01-01');
$to = strtotime('2018-11-19');
//Disable save function between specific
if ( !(property_exists($model, 'always_writable')
&& $model->always_writable)
&& (time() >= $form && time() <=$to)){
$model->setErrors(trans('validation.read_only'));
return false;
}
return $model->validate();
});
编辑:添加了 always_writable
条件。
edit2:让你的 always_writable 检查实际上做了一些明智的事情。
您需要检查查询的计数:
//if count is greater than 0
static::save(function($model)
{
//User can't save data between this date
$from = date('2018-01-01');
$to = date('2018-11-19');
//Disable save function between specific
if (count(User::whereBetween('date', [$from, $to])->get()) > 0 &&
(!property_exists($model, 'always_writable') || !$model->always_writable)) {
$model->setErrors(trans('validation.read_only'));
return false;
}
return $model->validate();
});
不太确定您想要通过查询实现什么:
User::whereBetween('date', [$from, $to])->get()
这基本上让系统中的所有用户的日期列都在 $from 和 $to 之间。大概这将永远是真的。不应该是:
Auth::user()->whereBetween('date', [$from, $to])->count()
这意味着:当前登录用户的日期列是否在 $from 和 $to 之间?
选择:
$userDate = Auth::user()->date;
if (($userDate >= $from && $userDate <= $to) && ... ) {
...
}