希望有人能帮助解决这个问题
我的目标是:创建一个创建HTML表单的类。
$cont->individual_fields("text", "feedName", "feed-name", null, "The name of the feed:", "200", "YES");
上面使用personalize_fields()方法为要在文本输入字段中找到的各种HTML属性添加值。
方法:
public function individual_fields($type, $name, $id, $class = null, $desc, $maxlength = null, $value = null, $select = null) { }
在该方法中,我创建了一个多维数组:
$fields_essentials = array(
'type' => $type,
'name' => $name,
'id' => $id,
'desc' => $desc,
'class' => $class,
'maxlength' => $maxlength,
'value' => $value,
'select' => $select);
因此,我现在有了一个具有属性标签和值的多维数组。太棒了
现在,我使用foreach循环遍历数组,创建HTML表单字段。
所以它最终是:
$text = '<tr>';
$text .= "<th><label for='"$id'">$desc</label></th>";
$text .= '<td><'.$form.' ></td>';
$text .= '</tr>';
return $text;
HTML如下:
<label for="feed-name">The name of the feed:</label>
<input type="text" value="" maxlength="200" class="regular-text code " id="feed-name"name="affiliate_hoover_plugin_options[feedName]"/>
到目前为止,一切都非常棒。
现在我遇到的问题是将personalize_fields()方法的返回结果放入另一个名为create_form()的方法中:
echo $cont->create_form("post", "#action", "optionForm", "option-form", "multipart/form-data");
public function create_form($method, $action = null, $name = null, $id = null, $enctype = null) {
extract($this->config_settings());
$form = null;
$form .= "<form method='"$method'" ";
($action !== null) ? $form .= " action='"$action'"" : null;
($name !== null) ? $form .= " name='"$name'"" : null;
($id !== null) ? $form .= " id='"$id'"" : null;
($enctype !== null) ? $form .= " enctype='"$enctype'"" : null;
$form .= ">";
$form .= '<fieldset>';
$form .= "<legend>$page_title</legend>";
$form .= '<table class="form-table">';
$form .= '<tbody>';
$form .= '</tbody>';
$form .= '</table>';
// input fields here
// INDIVIDUAL FIELDS FROM individual_fields() method here
// permenant settings for every form
$form .= $this->perm_fields();
$form .= '<input type="submit" id="submit" name="submitForm" class="button-primary" value="Save Changes">';
$form .= '</fieldset>';
$form .= '</form>';
return $form;
}
如何保存personalize_fields()方法返回的值并在create_form()方法中使用它
我尝试了很多不同的方法,在这个问题上花了很多时间,我想知道问题是否在于我如何构建类方法,而不是范围问题。
如果你已经从上到下阅读了这篇文章,我感谢
编辑:
阅读下面的回复并仔细思考,我可以将字段详细信息作为任何数组添加到create_form函数中,这样它看起来像这样:
$cont->create_form("post", "#action", "optionForm", "option-form", "multipart/form-data", array("text", "feedName", "feed-name", null, "The name of the feed:", "200", "YES"), array("text", "urlName", "url-name", null, "The URL of the feed:", "300", "YES"));
不过,这更像是我想要的一块复杂的代码。如果能够将其分离,那就太好了。
您可以创建如下函数(在$cont
中):
protected $fields = array();
function push_field( $field){
$this->fields[] = $field;
}
// And usage:
$cont->push_field( $text);
// In create form:
foreach( $this->fields as $field){
echo $field . "'n";
}
然而,我会创建这样的类:
class Field {
$attrbites = array(); // HTML attributes
$properties = array(); // Like field title or so
public function __construct($type, $name, $id, $class = null, $desc, $maxlength = null, $value = null, $select = null) { }
public function getHtml(){ ...
return $text;
}
}
并且在显示表单时生成所有html,而不是将验证添加到Field
,添加装饰器,使用它们进行操作会更容易。