php 表单验证失败后重新填充 Codeigniter 中的复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6338220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Repopulating checkboxes in Codeigniter after Unsuccessful Form Validation
提问by Nyxynyx
I have a problem repopulating a set of checkboxes after an unsuccessful form validation returns the user back to the same form. Dropdown menus and text inputs could be repopulated, but not checkboxes!
在不成功的表单验证将用户返回到相同的表单后,我在重新填充一组复选框时遇到问题。可以重新填充下拉菜单和文本输入,但不能重新填充复选框!
Here's a snippet of the code for the checkboxes:
这是复选框的代码片段:
<td>
<?php echo form_checkbox('ambience[]', 'casual', set_checkbox('ambience[]', 'casual')); ?> Casual <br />
<?php echo form_checkbox('ambience[]', 'romantic', set_checkbox('ambience[]', 'romantic')); ?> Romantic <br />
<?php echo form_checkbox('ambience[]', 'outdoor', set_checkbox('ambience[]')); ?> Outdoor / Alfresco <br />
<?php echo form_checkbox('ambience[]', 'trendy', set_checkbox('ambience[]')); ?> Hip & Trendy <br />
<?php echo form_checkbox('ambience[]', 'vibrant', set_checkbox('ambience[]')); ?> Vibrant <br />
<?php echo form_checkbox('ambience[]', 'up_scale', set_checkbox('ambience[]')); ?> Upscale <br />
</td>
The code snippet for text input which successfully repopulated is:
成功重新填充的文本输入的代码片段是:
<?php echo form_dropdown('price_range', $options, set_value('price_range')); ?>
Any ideas? I'm really confused why set_checkbox
does not work as advertised.
有任何想法吗?我真的很困惑为什么set_checkbox
不像宣传的那样工作。
回答by ndcisiv
In order for set_checkbox to work properly, an actual validation rule needs to be used on that element. I ran into this problem and could not get the value to show on a resubmit until I included:
为了使 set_checkbox 正常工作,需要在该元素上使用实际的验证规则。我遇到了这个问题,并且无法在重新提交时显示值,直到我包含以下内容:
$this->form_validation->set_rules('checkbox_name', 'checkbox_title', 'trim');
Then, everything worked perfect.
然后,一切都很完美。
回答by Francois Deschenes
You set_checkbox
calls are wrong. When you're using an array like "ambience[]" in form_checkbox, you don't want to include the square brackets ([]) in your set_checkbox
call. The other problem is that set_checkbox requires a second parameter which you've only included in the first 2 checkboxes.
你set_checkbox
叫错了。当您在 form_checkbox 中使用像“ambience[]”这样的数组时,您不希望在set_checkbox
调用中包含方括号 ([]) 。另一个问题是 set_checkbox 需要第二个参数,您只包含在前两个复选框中。
The set_checkbox should always be like this:
set_checkbox 应该始终是这样的:
set_checkbox('ambience', 'value');
Where 'value' is the second parameter of the form_checkbox
call. Like this:
其中“值”是form_checkbox
调用的第二个参数。像这样:
form_checkbox('ambience[]', 'value', set_checkbox('ambience', 'value'));
回答by ngl5000
I actually found that it only works if you use like this:
我实际上发现它只有在你这样使用时才有效:
form_checkbox('ambience[]', 'value', set_checkbox('ambience[]', 'value'));
You need the square array brackets on the name for it to work properly.
您需要名称上的方括号方括号才能正常工作。
回答by internet-nico
Here's a working example. You are required to include the array name with brackets []in each of $this->form_validation->set_rules()
, form_checkbox()
and set_checkbox()
.
这是一个工作示例。您需要在每个,和 中包含带括号 []的数组名称。$this->form_validation->set_rules()
form_checkbox()
set_checkbox()
In the controller:
在控制器中:
$this->load->library('form_validation');
$this->form_validation->set_rules('set_reminder_days[]', 'Reminder Day', 'trim');
if( $this->form_validation->run() == FALSE ) //Field validation failed.
{
//form validation errors will show up automatically
}
else //Validation success.
{
//This is an array of all checked checkboxes
$reminder_days = $this->input->post('set_reminder_days');
}
In the view:
在视图中:
$day_options = array(
'S' => 'Sunday',
'M' => 'Monday',
'T' => 'Tuesday',
'W' => 'Wednesday',
'Th' => 'Thursday',
'F' => 'Friday',
'Sa' => 'Saturday'
);
foreach( $day_options as $key => $day_option ) {
echo form_checkbox('set_reminder_days[]', $key, set_checkbox('set_reminder_days[]', $key), 'class="form-checkbox"');
}
回答by akbarbin
This is not use form helper. I try to use this code.
这不是使用表单助手。我尝试使用此代码。
<input type="checkbox" name="tes_display" value="1" <?php echo set_checkbox('tes_display', '1', FALSE); ?> />
回答by Joe Nyugoh
I tried all the solutions here none worked. So i collected all the data, packed it into an array, then use a loop to check if the values in the array match the value of the selected box, if so change the checked attribute to checked.
我在这里尝试了所有解决方案都没有奏效。因此,我收集了所有数据,将其打包到一个数组中,然后使用循环检查数组中的值是否与所选框的值匹配,如果匹配,则将选中的属性更改为选中。
Here is the html code:
这是html代码:
<div class="col-sm-10 tags">
<label>
<input class="tags" type="checkbox" name="tags[]" value="love" >Love
</label>
<label>
<input class="tags" type="checkbox" name="tags[]" value="God" >God
</label>
<label>
<input class="tags" type="checkbox" name="tags[]" value="Reality" >Reality
</label>
<label>
<input class="tags" type="checkbox" name="tags[]" value="Entrepreneurship">Entrepreneurship
</label>
</div>
Here is the javascript code in a function
这是函数中的 javascript 代码
(function(){
var tag_string = '<?php echo $_post['tags']; ?>',
tags = tag_string.split(', ');
boxes = document.getElementsByClassName('tags');
for(i = 0; i< boxes.length;i++ ){
if(tags.toString().includes(boxes[i].value)){
boxes[i].checked = "checked";
}
}
})();
回答by Mahder
In one of my projects, I have used it as follows:
在我的一个项目中,我使用它如下:
In the view I did:
在我看来:
$checkbox_name = 'menu_type_id[]';
$menu_type_attributes = array(
'name' => 'menu_type_id[]',
'id' => 'menu_type_id',
'class' => 'checkbox-custom rectangular',
'value' => $menu_type->get_id(),
'checked' => set_checkbox('menu_type_id[]', $menu_type->get_id()),
'aria-describedby' => 'menuTypeIdHelp'
);
echo form_checkbox($menu_type_attributes);
In the controller I have the validation (FYI, you will have to have form validation on the checkbox if you want to retain the checked status of the checkbox)
在控制器中,我进行了验证(仅供参考,如果要保留复选框的选中状态,则必须对复选框进行表单验证)
$this->form_validation->set_rules('menu_type_id[]', 'Menu type(s)', 'required');
Even though your business rule does not require you to have at least one checkbox checked, you will still need to put a validation for the checkbox. For eg, you could use
即使您的业务规则不要求您至少选中一个复选框,您仍然需要对复选框进行验证。例如,你可以使用
$this->form_validation->set_rules('menu_type_id[]', 'Menu type(s)', 'trim');
etc.
$this->form_validation->set_rules('menu_type_id[]', 'Menu type(s)', 'trim');
等等。
Hope this will help some one out there, b/c this information is not available in Code Igniter's documentation.
希望这会对那里的人有所帮助,b/c 此信息在 Code Igniter 的文档中不可用。
回答by evannah
I realised that set_checkbox takes 3 parameters :
我意识到 set_checkbox 需要 3 个参数:
set_checkbox(string $checkboxname, string $value, boolean $isChecked);
For example :
例如 :
echo form_checkbox('mycbx[]',
$item['id'],
set_checkbox('mycbx[]', $item['id'], false)
);
or this way :
或者这样:
$checkbox = array(
'name' => 'mycbx[]',
'value' => $item['id'],
'checked' => set_checkbox('mycbx[]', $item['id'], false)
);
echo form_checkbox($checkbox);
回答by Jason Savage
function set_checkbox_array($field, $value)
{
$arr = isset($_POST[$field]) ? $_POST[$field] : FALSE;
return ($arr !== FALSE && is_array($arr) && in_array($value, $arr)) ? 'checked="checked"' : '';
}