我有一些标准的单个复选框,用于定义特定的布尔值,即is_active
<label class="form-check-label text-google">
<input type="checkbox" name="is_active" id="is_active" value="1" class="form-check-input"
@if (old('is_active') == '1' || (empty(old()) && $doctor->is_active == 1)) checked @endif>
{{ __('Doctor activated') }}
<i class="input-helper"></i>
</label>
当将其发送到我的控制器时,请求会通过一个自定义的request类运行,我目前在其中执行以下操作。主要是对更新方法很重要,所以取消选中的复选框将在数据库中更新。
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
$this->request->set("is_active", $this->request->has("is_active"));
return [ ...
我还将Model中的is_active属性强制转换为布尔值。这里的最佳实践是什么?我找不到任何能指引我走向正确方向的东西。。。
提前谢谢。
由于您想在验证前触摸请求,因此可以使用以下方法:prepareForValidation()
:
准备输入进行验证
如果您需要在如果应用验证规则,则可以使用prepareForValidation方法:
use IlluminateSupportStr; /** * Prepare the data for validation. * * @return void */ protected function prepareForValidation() { $this->merge([ 'slug' => Str::slug($this->slug), ]); }
因此,在您的情况下,您可以执行:
public function prepareForValidation()
{
$this->merge([
'is_active' => $this->request->has('is_active')),
]);
}
当我读到您想知道是否有值时,实际上我认为您可以使用类Requestfilled
函数。