我正在使用带Laravel 5.8
的Orchestra Html Form来为我的自定义CMS生成表单。为了验证用户请求,我创建了一个UserRequest类,该类具有规则,消息和authorize方法,以验证表单提交。如果验证失败,例如,电子邮件。唯一验证失败,我得到一个错误,而不是返回带有验证消息的视图:方法Orchestra\Html\Form\FormBuilder::__toString()
不得抛出异常,捕获ErrorException:试图获取非对象的属性。
我还对其他模型使用了Orchestra表单,但是User是唯一具有自己的自定义请求的用户,并且是生成此问题的唯一模型,因此,我想冲突一定与User Request有关。我不知道怎么了。
我将变量转储到视图中,它们似乎是正确的。
模型上的form()
方法也可以正常工作,但是当验证错误后表单在视图上呈现时,它将崩溃。
"orchestra/html": "^3.5",
Laravel 5.8
型号:
public function form()
{
return Form::of('users', function ($form) {
$attributes = [
'enctype' => 'multipart/form-data'
];
$form->attributes($attributes);
$form->submit = $this->exists ? 'Guardar' : 'Criar';
$form->fieldset(function ($form) {
$form->control('select', 'role', function ($control) {
$control->field(function ($row) {
$val = old('role') ?? $this->roles()->first();
return "<select class='form-control input-with-feedback' name='role' required>".Role::selectRole($val)."</select>";
});
});
$form->control('input:text', 'nome', function ($control) {
$control->field(function ($row) {
$val = old('name') ?? $this->name ?? '';
return "<input type='text' class='form-control input-with-feedback' name='name' value='".$val."' required>";
});
});
$form->control('input:email', 'email', function ($control) {
$control->field(function ($row) {
$val = old('email') ?? $this->email ?? '';
return "<input type='email' class='form-control input-with-feedback' name='email' value='".$val."' required>";
});
});
$form->control('input:password', 'password', function ($control) {
$control->field(function ($row) {
return "<input type='password' class='form-control input-with-feedback' name='password' value='$row->password' required>";
});
});
$form->control('input:password', 'repetir password', function ($control) {
$control->field(function ($row) {
return "<input type='password' class='form-control input-with-feedback' name='password_confirmation' value='$row->password_confirmation' required>";
});
});
$form->control('input:file', 'avatar', function ($control) {
$control->field(function ($row) {
$default_img = $this->avatar ?? 0;
$val = "<input type='file' class='form-control input-with-feedback' name='avatar' ><input type='hidden' name='avatar' value='".$default_img."'>";
if ($this->avatar != NULL){
$val .= "<label class='col-md-3 control-label d-block mt-2 mb-0'>Imagem atual:</label><img src='".\CompGallery::getImageFullUrl($this->avatar, '_avatar')."'><label class='control-label mt-2 mb-0 mx-2'>Apagar Imagem</label><input type='checkbox' name='delete_image' value='on'>";
}
return $val;
});
})->label('Avatar');
$form->control('input:checkbox', 'active', function ($control) {
$control->field(function ($row) {
$val = old('active') ? 'checked' : $this->active ? 'checked' : '';
if (is_null($this->active)) $val = 'checked';
return "<label class='switch'><input type='hidden' value='0' name='active'><input type='checkbox' class='form-control input-with-feedback col-md-4' name='active' id='ativo' value='1' ".$val."><span class='slider round'></span></label>";
});
})->label('Ativo');
});
});
}
请求:
public function rules()
{
return [
'role' => 'required',
'name' => 'required|string|max:50',
'email' => 'required|string|email|max:50|unique:users,email,'.$this->id,
'password' => 'required|string|min:8|confirmed',
'active' => 'required',
];
}
/**
* Get the error messages for the defined validation rules.
*
* @return array
*/
public function messages()
{
return [
'name.required' => 'O nome é obrigatório',
'email.required' => 'O email é obrigatório',
'email.unique' => 'O email já existe',
'password.required' => 'A password é obrigatória',
'password.confirmed' => 'As passwords não coincidem'
];
}
查看:
<div class="row">
<div class="col-md-7 module-form">
{!! $form !!}
</div>
</div>
错误消息:方法Orchestra \ Html \ Form \ FormBuilder :: __ toString()不得引发异常,捕获到的ErrorException:试图获取非对象的属性