Joomla PHP:复选框不会取消选中 [英] Joomla PHP: Checkbox won't uncheck
问题描述
组成一个简单的组件.如果我在选中复选框的情况下保存了新记录,则可以保存.如果我返回并取消选中先前选中的项目,它将恢复为选中状态.我以为它与一个隐藏的字段有关,可以手动将其中一个放在其中,但也没有解决问题,因此将其取出.
Making a simple component. If I save a new record with the box checked, it saves fine. If I go back and uncheck a previously checked item it reverts back to checked. I thought it had something to do with a hidden field, played with putting one of those in manually but that didn't fix it either so took it out.
我在这里: xml片段:
Here's where I'm at: An xml snippet:
<fieldset name="checks">
<field name="checkbox1"
type="checkbox"
label="First Checkbox"
value="1"
filter="intval"
/>
<field name="checkbox2"
type="checkbox"
label="Second Checkbox"
value="1"
filter="intval"
/>
...
</fieldset>
edit.php文件:
The edit.php file:
<div class="width-45 fltlft">
<fieldset class="adminform">
<legend>Checkboxes</legend>
<ul class="adminformlist">
<?php foreach ($this->form->getFieldset('checks') as $field): ?>
<li>
<?php echo $field->label; ?>
<?php echo $field->input; ?>
</li>
<?php endforeach ?>
</ul>
</fieldset>
</div>
也将其添加到form.token的末尾:
Also added this towards the end before the form.token:
<input type="hidden" name="task" id="task" value="completion.edit" />
我尝试删除xml中的value ="1",但是却遇到了相反的问题,就是根本无法保存支票.
I tried deleting the value="1" in the xml but then I had the opposite problem where the check wouldn't save at all.
有什么想法吗?
谢谢!
============================
=============================
型号:
<?php
defined( '_JEXEC' ) or die;
jimport('joomla.application.component.modeladmin');
class AssessModelCompletion extends JModelAdmin
{
//tells it what kind of record and the prefix
public function getTable($type = 'Completion', $prefix = 'AssessTable', $config = array())
{
return JTable::getInstance($type, $prefix, $config);
}
//Load the data into the edit form
protected function loadFormData()
{
$data = JFactory::getApplication()->getUserState('com_assess.edit.completion.data', array()); //first try to get the data from the session, not db
if (empty($data)) {
$data = $this->getItem(); //this gets the data
}
return $data;
}
//Stores data in a session in case a field is missed
public function getForm($data = array(), $loadData = true)
{
$form = $this->loadForm('com_assess.completion', 'completion', array('control' => 'jform', 'load_data' => $loadData));
return $form;
}
}
和表:
<?php
defined ( '_JEXEC' ) or die;
class AssessTableCompletion extends JTable
{
public function __construct(&$db)
{
parent::__construct('#__tablename_completions', 'completion_id', $db);
}
}
====================================
======================================
参考: https://stackoverflow.com/questions/6964333/joomla-1-6-admin-form-processing-grouped复选框形式
推荐答案
所以我有很多字段,并且想在我的edit.php字段中简单地遍历它们以保持其清洁.尽管提供的所有答案都是正确的,但它们并不容易实现-很快就变得非常混乱,无法正常工作或无法找到一种更清洁的解决方法.我对此进行了一段时间的咀嚼,然后今天发现基本上是字段覆盖.
So I had a lot of fields and wanted to simply loop through them in my edit.php field to keep it clean. While all the answers offered were right they weren't easy to implement - got really messy really quickly, had trouble getting it to work, or couldn't figure out a cleaner way around it. I chewed on this for awhile and then today came across what is basically a field override.
键:
标准表单字段类型位于 joomla/libraries/joomla/form/fields/.您不应该存储自定义 字段,也不必在自己的代码中使用此路径, 但是标准类型通常是很好的例子.
The standard form field types are located in joomla/libraries/joomla/form/fields/. You should not store custom fields there, nor should you have to use this path in your own code, but the standard types are usually good examples.
属于您的组件的自定义字段类型通常是 位于管理员/组件//模型/字段中.您可以在自己的路径中指定此路径或其他路径 代码
The custom field types that belong to your component are usually located in administrator/components//models/fields. You can specify this or another path in your code
因此,我将checkbox.php复制到了模型/字段.然后,在文件末尾,我在复选框标记之前添加了一个空字段:
So, I copied checkbox.php to models/fields. Then, towards the end of the file I added the empty field before the checkbox tag:
<input type="hidden" name="'.$this->name.'" id="'.$this->id.'" value="0" /><input type="checkbox" .....
现在,每当我需要一个复选框时,也会写入空白字段.可能不是最有效的解决方案,但实施起来很简单,有望对其他人有所帮助.
Now, whenever I need a checkbox the empty field is also written. May not be the most efficient solution but simple to implement and can hopefully help someone else.
请注意,每次Joomla更新时,您可能都需要比较核心版本,以防发生更改.
As a note, with every Joomla update you would probably need to compare the versions in the core in case there was a change.
这篇关于Joomla PHP:复选框不会取消选中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!