我正面临在zfcAdmin和BjyAuthorize中集成自定义模块的验证问题。
我的表单类:
...
$formOptions = $this->settings->getFormSettings();
foreach ($formOptions as $field){
if (isset($field['field']))
$this->add($field['field']);
}
...
我的过滤器类:
$formOptions = $this->settings->getFormSettings();
foreach ($formOptions as $filter){
if (isset($filter['filter']))
$this->add($filter['filter']);
}
...
字段、过滤器和其他选项从配置文件中检索。
基本上一切都很好:表单数据可以从数据库中添加,编辑或删除。zfcAdmin模块安装后也没有出现问题。使用'site/mymodule'路由和'site/admin/mymodule'路由,一切都很好:我仍然可以添加,编辑和删除db中的项目。
这里的问题:我需要一些表单元素(一个选择在这种特殊情况下)编辑/只能由管理员查看。(我可以为管理员写一个新的控制器/实体类'特设',但我想为整个网站使用相同的代码。)
我安装并配置了bjyoungblood/BjyAuthorize模块:它允许我显示一些表单元素/字段只管理,但当我在编辑模式一个表单验证错误显示:"值是必需的,不能为空"
代码如下:
//view/mymodule/mymodule/update.phtml
<div id="page" style="margin-top: 50px;">
<?php if (isset($this->messages) && count($this->messages) > 0 ): ?>
<?php foreach ($this->messages as $msg): ?>
<div class="alert alert-<?php echo $this->escapeHtmlAttr($msg['type']); ?>">
<?php if (isset($msg['icon'])) echo '<i class="'.$this->escapeHtmlAttr($msg['icon']).'"></i> '; ?><?php echo $this->escapeHtml($msg['message']); ?>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php
$title = 'Edit Item';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<?php
$form = $this->form;
$form->setAttribute('action', $this->url($this->route . 'mymodule/update', array('action' => 'update', 'id' => $this->id )));
$form->prepare();
$form->setAttribute('method', 'post');
$input = $form->getInputFilter();
?>
<?php echo $this->form()->openTag($form) ?>
<dl class="zend_form">
<?php foreach ($form as $element): ?>
<?php
//CHECK USER PRIVILEDGES
$elName = $element->getName();
$elResource = isset($this->form_options[$elName]['auth']) ? $this->form_options[$elName]['auth']['resource'] : "userresource";
$elPrivilege = isset($this->form_options[$elName]['auth']) ? $this->form_options[$elName]['auth']['privilege'] : "view";
//SHOW THE ELEMENT IF ALLOWED
if($this->isAllowed($elResource, $elPrivilege)):
?>
<?php if ($element->getLabel() != null): ?>
<dt><?php echo $this->formLabel($element) ?></dt>
<?php endif ?>
<?php if ($element instanceof Zend'Form'Element'Button): ?>
<dd><?php echo $this->formButton($element) ?></dd>
<?php elseif ($element instanceof Zend'Form'Element'Select): ?>
<dd><?php echo $this->formSelect($element) . $this->formElementErrors($element) ?></dd>
<?php else: ?>
<dd><?php echo $this->formInput($element) . $this->formElementErrors($element) ?></dd>
<?php endif ?>
<?php else: ?>
<?php
?>
<?php endif ?>
<?php endforeach ?>
</dl>
<?php echo $this->form()->closeTag() ?>
</div>
<div class="clear-both"></div>
我的控制器动作
//controller
public function updateAction(){
$messages = array();
$id = (int)$this->getEvent()->getRouteMatch()->getParam('id');
$form = $this->getServiceLocator()->get('FormItemService');
$itemMapper = $this->getItemMapper();
$item = $itemMapper->findById($id);
$form->bind($item);
$request = $this->getRequest();
if($request->isPost()){
$form->setData($request->getPost());
if ($form->isValid()) {
die('c');//never here
$service = $this->getServiceLocator()->get('mymodule'Service'Item');
if ( $service->save($form->getData()) )
{
$messages[] = array(
'type' => 'success',
'icon' => 'icon-ok-sign',
'message' => 'Your profile has been updated successfully!',
);
}
else
{
$messages[] = array(
'type' => 'error',
'icon' => 'icon-remove-sign',
'message' => 'Profile update failed! See error messages below for more details.',
);
}
}else{
var_dump($form->getMessages());//Value is required and can't be empty
}
}
return array(
'messages' => $messages,
'form' => $form,
'id' => $id,
'form_options' => $this->getServiceLocator()->get('mymodule_module_options')->getFormSettings(),
'route' => $this->checkRoute($this->getEvent()->getRouteMatch()->getmatchedRouteName())
);
}
如果不允许用户查看资源,则不回显该元素。因此$request->getPost()对于该表单元素没有值,并且由isValid()返回错误。
有没有人解决过类似的问题或者有人能给我指出正确的方向吗?由于
问题是您没有在定义必需字段的FormFilter类中进行任何安全检查。
$form->isValid()函数根据这些过滤器元素检查提交的数据。因此,这还不足以防止视图中的'echo字段',您仍然需要对过滤器元素应用安全检查。
另一种方法是制作两个表单,一个用于前端,一个用于管理。由于管理表单将具有相同的字段加上一个额外的选择字段,您可以使管理表单扩展前端表单。例如
class myForm
{
public function __construct(...)
{
// add fields and set validators
}
}
和admin表单可以是:
class MyAdminForm extends myForm
{
public function __construct(...)
{
parent::__construct(...);
// add the extra field and extra validator
}
}
这样,即使您编辑了前端表单(或验证器),后端也将始终是最新的。
希望这对你有帮助:),
斯托亚