我正在使用http://kristijanhusak.github.io/laravel-form-builder/overview/quick-start.html中的表单生成器。我有一个表格的页面。此页面是index()。当用户单击表单中的“提交”按钮时,页面将被重定向到确认页面。在确认页面中,我打印表单请求以检查是否使用(1)$ form-> getRequest()-> all(),(2)$ form-> getRequest()-> old(), (3)$ form-> getData()。第三个打印来自用户输入的所有请求。确认页面上还有一个取消和确认按钮。单击取消按钮后,它会重定向回index(),但是问题是当它重定向回index()时,所有请求都变为null,这会将所有用户输入从index()中删除。有解决方法吗?
代码:
public function index()
{
if (!$this->hasPermit(self::PERMIT_READ, 'h_1_203')) {
return $this->permitError();
}
// $this->subTitle = '一覧';
$this->setupContentHeader();
$this->pageInfoUpdate();
$wares = $this->listSql();
$this->paging($wares);
$wares = $wares->get()->toArray();
$form = $this->form(WarehouseForm::class, [
'method' => 'POST',
'url' => route('head.system.h_1_203@confirmInsert'),
'class' => 'pjax-container',
]);
var_dump($form->getRequest()->all());
var_dump($form->getRequest()->old());
var_dump($form->getData());
// Disable form if permission is readonly or below
if ($this->permit < self::PERMIT_FULL) {
$form->disableFields();
}
return view('head.system.h_1_203', compact('form'), [
'wares' => $wares,
]);
}
public function confirmInsert(Request $request)
{
if (!$this->hasPermit(self::PERMIT_READ, 'h_1_203')) {
return $this->permitError();
}
$this->formInputConvert();
// $this->subTitle = '一覧';
$this->setupContentHeader();
$this->pageInfoUpdate();
$wares = $this->listSql();
$this->paging($wares);
$wares = $wares->get()->toArray();
$form = $this->form(WarehouseForm::class, [
'method' => 'POST',
'url' => route('head.system.h_1_203@insert'),
'class' => 'pjax-container',
]);
var_dump($form->getRequest()->all());
var_dump($form->getRequest()->old());
var_dump($form->getData());
$form->modify('count_flg', 'choice', [
'selected' => Request::input('count_flg'),
]);
$form->disableFields();
$form->redirectIfNotValid();
// Disable form if permission is readonly or below
if ($this->permit < self::PERMIT_FULL) {
$form->disableFields();
}
return view('head.system.h_1_203', compact('form'), [
'wares' => $wares,
'confirm' => true,
]);
}
class WarehouseForm extends Form
{
public function buildForm()
{
$defVal = $this->getRequest()->all();
if (!array_has($defVal, 'ware_id')) {
$defVal = $this->getRequest()->old();
}
if (!array_has($defVal, 'ware_id')) {
$defVal = $this->getData();
if (array_has($defVal, 'ware_id')) {
$ware = Ware::find($defVal['ware_id']);
$defVal['ware_id'] = $ware->ware_id;
$defVal['ware_cd'] = $ware->ware_cd;
$defVal['ware_name'] = $ware->ware_name;
$defVal['count_flg'] = $ware->count_flg;
$defVal['nondisp_flg'] = $ware->nondisp_flg;
$defVal['note'] = $ware->note;
}
}
$this
->add('ware_id', 'hidden', [
'value' => @$defVal['ware_id'] ?: null,
])
->add('ware_cd', 'text', [
'label' => '倉庫コード',
'rules' => 'required|regex:/^[0-90-9]*$/',
'attr' => [
'size' => 3,
'maxLength' => 3,
],
'value' => @$defVal['ware_cd'] ?: null,
'error_messages' => [
'ware_cd.regex' => '倉庫コードは数字を入力してください。']
])
->add('ware_name', 'text', [
'label' => '倉庫名',
'rules' => 'required',
'attr' => [
'size' => 22,
'maxLength' => 10,
],
'value' => @$defVal['ware_name'] ?: null,
])
->add('count_flg', 'choice2', [
'label' => '発注点カウント',
'choices' => collect(Ware::$CountFlgData)->pluck('name', 'id')->toArray(),
'expanded' => true,
'multiple' => false,
'selected' => !isset($defVal['count_flg']) ? '1' : @$defVal['count_flg'] ? '1' : '2',
])
->add('nondisp_flg', 'checkbox',[
'label' => '非表示',
'checked' => @$defVal['nondisp_flg'] == null ? false : @$defVal['nondisp_flg']
])
->add('note', 'text', [
'label' => '備考',
'attr' => [
'size' => 34,
'maxLength' => 30,
],
'value' => @$defVal['note'] ?: null,
])
->add('submit', 'submit', [
'label' => '登 録',
'attr' => ['class' => 'btn btn-primary submit'],
])
->add('clear', 'button', [
'label' => 'クリア',
'attr' => ['class' => 'btn btn-warning pull-left', 'onclick' => 'transition("'.route('head.system.h_1_203').'")'],
]);
}
}