【问题标题】:CI4 - Form Validation with multipart fromdataCI4 - 使用多部分表单数据进行表单验证
【发布时间】:2020-07-10 04:58:02
【问题描述】:

我正在使用 CI4 验证库在服务器端验证我的表单。表单标签是使用form_open_multipart 辅助函数创建的。 这是 HTML 和控制器的示例代码

ProductController.php(控制器)

class ProductController extends BaseController
{
 ...

 public function create()
    {
        if (!$this->validateFrom()) {
            echo '<pre>'.print_r($this->validator->getErrors());die();
            $this->session->setFlashdata('validator', $this->validator);
            $this->session->setFlashdata('product_form', $this->request->getPost());
            return redirect('admin.product.new');
        }
        ...
    }
 private function validateFrom()
   {
    return $this->validate([
        'product_name' => [
            'label' => 'Product name',
            'rules' => 'required',
            'errors' => [
                'required' => '{field} is required.'
            ]
        ]
    ]);
  }

 ...

}

ProductForm.php(查看文件)

<?= form_open_multipart(route_to('prodyct.create'), ['id' => 'prodForm', 'class' => 'form-horizontal']); ?>

<div class="form-group row">
    <label for="product_name" class="col-4 col-form-label text-right">Product Name</label>
    <div class="col-8 p-0">
        <input type="text"
               class="form-control <?= $validator->hasError('product_name') ? 'is-invalid' : '' ?>"
               id="product_name"
               name="product_name"
               value="<?= $productForm->product_name ?? null ?>"
               placeholder=""/>
        <?php if ($validator->hasError('product_name')): ?>
            <div class="invalid-feedback">
                <?= $validator->getError('product_name'); ?>
            </div>
        <?php endif; ?>
    </div>
</div>
<div class="form-group row">
    <label for="product_image" class="col-4 col-form-label text-right">Product Image</label>
    <div class="col-8 p-0">
        <input type="text"
               class="form-control <?= $validator->hasError('product_image') ? 'is-invalid' : '' ?>"
               id="product_image"
               name="product_image"
               value="<?= $productForm->product_image ?? null ?>"
               placeholder=""/>
        <?php if ($validator->hasError('product_image')): ?>
            <div class="invalid-feedback">
                <?= $validator->getError('product_image'); ?>
            </div>
        <?php endif; ?>
    </div>
</div>

<?= form_close(); ?>

在提交表单时,即使提供了值,它也总是验证失败。

Array
(
    [product_name] => Product Name is required.
)

当我检查 CodeIgniter\Validation\Validation 的方法 withRequest 时,似乎当它尝试使用 $this-&gt;data = $request-&gt;getRawInput(); 获取数据时,getRawInput 正在返回 null。

谁能帮我解决这个问题?

谢谢!

【问题讨论】:

  • 您是使用 Ajax 发送数据还是仅仅发送一个帖子?因为 getRawInput() 是用于解析 ajax 数据体而不是用于发布数据。您将不得不改用 $request->post() 。此外,您不需要单独的方法来验证数据。我认为在控制器中这样做不是一个好习惯。
  • @DhavalChheda 我只是发布一个帖子数据。当我使用控制器的验证方法时,它需要带有 DI 的请求对象,我只需要设置验证规则。但是 CI4 传入请求正在尝试从 php://input 读取数据而不考虑内容类型,因此这是框架的问题,我将为此向贡献者报告此问题。感谢您的回复。

标签: php validation multipartform-data codeigniter-4


【解决方案1】:

我认为使用base_url() 比使用route_to('prodyct.create') 更好

像这样..

<?= form_open_multipart(base_url('home/create'), ['id' => 'prodForm', 'class' => 'form-horizontal']); ?>

因为当我尝试var_dump(route_to('some_route') 时,它的返回值是布尔值route_to(...) boolean false

【讨论】:

  • 如果查看CI4的文档route_to是用来生成反向路由的。所以使用它是因为如果 Route URI 有任何变化,那么没有必要在项目中的所有地方找到 URI 并更新它。它会自动适应这种变化。 codeigniter4.github.io/userguide/general/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-13
  • 2018-02-25
  • 1970-01-01
  • 2021-01-14
  • 2013-03-10
  • 2021-05-14
  • 2018-05-11
相关资源
最近更新 更多