CodeIgniter 购物车页面上的表单验证
Form Validation on CodeIgniter Cart page
CodeIgniter v2.1.2
我有一个非常简单的 CodeIgniter 购物车页面设置,按照 the CodeIgniter documentation。
此购物车页面显示购物车中有许多产品,用于编辑每个产品数量的 input
元素具有分配的 name
属性,如下所示:1[qty]
、2[qty]
、3[qty]
、4[qty]
、5[qty]
等
<input name="1[qty]" type="text" ...
<input name="2[qty]" type="text" ...
<input name="3[qty]" type="text" ...
<input name="4[qty]" type="text" ...
<input name="5[qty]" type="text" ...
....
这是根据 CodeIgniter 文档的方案,它似乎运行良好,因为 POST 数组自动采用正确的格式以便轻松更新购物车。
$this->cart->update( $this->input->post() );
似乎 cart->update()
会忽略任何无效输入。换句话说,购物车仅在数量为数值时更新。但是,我认为我需要将 CodeIgniter 表单验证应用于购物车页面。也许我不需要它并且对您对此的想法也很感兴趣。
在我的控制器中,我通常会使用 set_rules
按字段名称 设置规则 ,但我不知道字段 name
中的内容当我有未知数量的字段时的参数。
documentation says I can declare validation on arrays,但我无法让以下代码为我工作。我试过 [qty]
、[][qty]
和其他几个。不工作,我的意思是当购物车页面上的数量字段包含无效数据时没有验证错误,它总是通过验证并点击 update()
函数。
$this->load->library('form_validation');
$this->form_validation->set_rules('[][qty]', 'Quantity', 'numeric|strip_tags|xss_clean');
if ($this->form_validation->run() === TRUE) // passed validation -> submission
{
// update the cart
$this->cart->update( $this->input->post() );
....
}
else
{
// display validation errors
....
}
当字段数量是动态的且命名如上所述时,如何对购物车页面应用验证?
我什至需要验证数量字段吗?购物车数据暂时存储在会话中,当它被传递到cart->update()
函数时,无效数据似乎被自动剥离。
因为我不知道如何在数组上声明验证,所以我使用 for
循环根据数组中唯一行项目的数量在每个 "QTY" 字段上声明规则根据 count($this->cart->contents())
的值购物车。 (与 $this->cart->total_items()
相反,后者计算所有内容,包括单行项目的倍数。)
在控制器...
$this->load->library('form_validation');
for ($i = 1; $i <= count($this->cart->contents()); $i++)
{
$this->form_validation->set_rules($i . '[qty]', 'Item #' . $i . ' quantity', 'numeric|strip_tags|xss_clean');
}
我仍然无法确认在这种情况下是否需要进行表单验证。通常,需要服务器端验证来保护数据库免受不良数据或安全性的影响。在这种情况下,cart->update()
函数似乎会自动拒绝无效条目。
CodeIgniter v2.1.2
我有一个非常简单的 CodeIgniter 购物车页面设置,按照 the CodeIgniter documentation。
此购物车页面显示购物车中有许多产品,用于编辑每个产品数量的 input
元素具有分配的 name
属性,如下所示:1[qty]
、2[qty]
、3[qty]
、4[qty]
、5[qty]
等
<input name="1[qty]" type="text" ...
<input name="2[qty]" type="text" ...
<input name="3[qty]" type="text" ...
<input name="4[qty]" type="text" ...
<input name="5[qty]" type="text" ...
....
这是根据 CodeIgniter 文档的方案,它似乎运行良好,因为 POST 数组自动采用正确的格式以便轻松更新购物车。
$this->cart->update( $this->input->post() );
似乎 cart->update()
会忽略任何无效输入。换句话说,购物车仅在数量为数值时更新。但是,我认为我需要将 CodeIgniter 表单验证应用于购物车页面。也许我不需要它并且对您对此的想法也很感兴趣。
在我的控制器中,我通常会使用 set_rules
按字段名称 设置规则 ,但我不知道字段 name
中的内容当我有未知数量的字段时的参数。
documentation says I can declare validation on arrays,但我无法让以下代码为我工作。我试过 [qty]
、[][qty]
和其他几个。不工作,我的意思是当购物车页面上的数量字段包含无效数据时没有验证错误,它总是通过验证并点击 update()
函数。
$this->load->library('form_validation');
$this->form_validation->set_rules('[][qty]', 'Quantity', 'numeric|strip_tags|xss_clean');
if ($this->form_validation->run() === TRUE) // passed validation -> submission
{
// update the cart
$this->cart->update( $this->input->post() );
....
}
else
{
// display validation errors
....
}
当字段数量是动态的且命名如上所述时,如何对购物车页面应用验证?
我什至需要验证数量字段吗?购物车数据暂时存储在会话中,当它被传递到
cart->update()
函数时,无效数据似乎被自动剥离。
因为我不知道如何在数组上声明验证,所以我使用 for
循环根据数组中唯一行项目的数量在每个 "QTY" 字段上声明规则根据 count($this->cart->contents())
的值购物车。 (与 $this->cart->total_items()
相反,后者计算所有内容,包括单行项目的倍数。)
在控制器...
$this->load->library('form_validation');
for ($i = 1; $i <= count($this->cart->contents()); $i++)
{
$this->form_validation->set_rules($i . '[qty]', 'Item #' . $i . ' quantity', 'numeric|strip_tags|xss_clean');
}
我仍然无法确认在这种情况下是否需要进行表单验证。通常,需要服务器端验证来保护数据库免受不良数据或安全性的影响。在这种情况下,cart->update()
函数似乎会自动拒绝无效条目。