如何验证与codeigniter和jQuery多维数组 [英] How to validate multidimensional arrays with Codeigniter and Jquery
问题描述
您好我需要验证多维形式像这样
<输入类型=文本级=输入XLARGE span5 REQID =contact_first_nameNAME =酒店并[d?= $ ID>] [ contact_first_name]VALUE =< = SET_VALUE('酒店[contact_first_name]')>>
<输入类型=文本级=输入XLARGE span5 REQID =contact_last_nameNAME =酒店并[d?= $ ID>] [contact_last_name]VALUE =< = SET_VALUE( 酒店[contact_last_name]')>>
我不知道最终阵列的尺寸,怎么一回事,因为输入是通过jQuery dinamically补充说。
我使用codeigniter Form_Validation服务器端和通过jQuery的室内用的JQuery验证的客户端。
这是我的form_validation规则
$配置['add_hotel'] =阵列(
阵列(
'场'=> 酒店[] [HOTEL_NAME]',
标签= GT; 酒店名称,
规则= GT; '需要'
)
阵列(
'场'=> 酒店[] [contact_first_name]',
标签= GT; '名字',
规则= GT; 修剪|需要'
)
阵列(
'场'=> 酒店[] [contact_last_name]',
标签= GT; '姓',
规则= GT; 修剪|需要'
)
这是如何我通过jQuery验证
做 $(#add_hotel)。验证({
规则:{
酒店[] [HOTEL_NAME]:要求 / * errorElement:分区,
包装:分区* /
},
消息:{
酒店[] [HOTEL_NAME]:请输入协会名称
},
submitHandler:功能(形式){
form.submit();
}
不知道怎么的Valide每个酒店[]输入用自己的ID,或者还有另一种方式来定义输入,可以更简单
//发布阵列
$酒店= $这个 - >输入 - >后(酒店); 如果(!空($酒店))
{
//循环遍历酒店和添加验证
的foreach($酒店作为的$ id => $数据)
{
$这个 - > form_validation-> set_rules(酒店['的$ id。] [contact_first_name]','名','需要|微调');
$这个 - > form_validation-> set_rules(酒店['的$ id。] [contact_last_name]','姓','需要|微调');
}
} //适用所有的时间默认规则
$这个 - > form_validation-> set_rules(用户名,用户名,必要); 如果($这个 - > form_validation->运行()== FALSE)
{
//错误
}
其他
{
//成功
}
Hi I need to validate a multidimensional form like this
<input type="text" class="input-xlarge span5 req" id="contact_first_name" name="hotel[<?=$id?>][contact_first_name]" value="<?= set_value('hotel[contact_first_name]') ?>">
<input type="text" class="input-xlarge span5 req" id="contact_last_name" name="hotel[<?=$id?>][contact_last_name]" value="<?= set_value('hotel[contact_last_name]') ?>">
I dont know the dimensions of the final array, beacuse the inputs are added dinamically via jquery.
I'm using Codeigniter Form_Validation for the server side and via JQuery wiht the JQuery Validator for the client side.
This are my form_validation rules
$config['add_hotel'] = array(
array(
'field' => 'hotel[][hotel_name]',
'label' => 'Hotel Name',
'rules' => 'required'
),
array(
'field' => 'hotel[][contact_first_name]',
'label' => 'First Name',
'rules' => 'trim|required'
),
array(
'field' => 'hotel[][contact_last_name]',
'label' => 'Last Name',
'rules' => 'trim|required'
),
and this is how i'm doing it via jquery validator
$("#add_hotel").validate({
rules: {
"hotel[][hotel_name]": "required"
/* errorElement: "div",
wrapper: "div"*/
},
messages: {
"hotel[][hotel_name]": "Please enter the Association Name"
},
submitHandler: function(form) {
form.submit();
}
Don't know how to valide each Hotel[] input with its own id, or maybe there is another way to defining the inputs that can be simpler
// Posted array
$hotel = $this->input->post('hotel');
if(!empty($hotel))
{
// Loop through hotels and add the validation
foreach($hotel as $id => $data)
{
$this->form_validation->set_rules('hotel[' . $id . '][contact_first_name]', 'First name', 'required|trim');
$this->form_validation->set_rules('hotel[' . $id . '][contact_last_name]', 'Last name', 'required|trim');
}
}
// Default rules that apply all the time
$this->form_validation->set_rules('username', 'Username', 'required');
if ($this->form_validation->run() == FALSE)
{
// Errors
}
else
{
// Success
}
这篇关于如何验证与codeigniter和jQuery多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!