Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/243.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php zf2表单验证(zfcAdmin和BJYAAuthorize相关)_Php_Validation_Zend Framework2_Zend Form - Fatal编程技术网

Php zf2表单验证(zfcAdmin和BJYAAuthorize相关)

Php zf2表单验证(zfcAdmin和BJYAAuthorize相关),php,validation,zend-framework2,zend-form,Php,Validation,Zend Framework2,Zend Form,在zfcAdmin和BjyAuthorize中集成自定义模块时,我面临验证问题 我的班级: ... $formOptions = $this->settings->getFormSettings(); foreach ($formOptions as $field){ if (isset($field['field'])) $this->add($field['field']); } ... 我的筛选器类: $formOptions = $th

在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”路由,一切都可以正常工作:我仍然可以添加、编辑和删除数据库中的项目

这里的问题是:我需要一些表单元素(在这个特殊情况下是Select)只能由管理员编辑/查看。(我可以为admin编写一个新的控制器/实体类'ad hoc',但我希望对整个站点使用相同的代码。)

我安装并配置了bjyoungblood/BJYAAuthorize模块:它允许我仅向管理员显示一些表单元素/字段,但当我处于编辑模式时,会显示表单验证错误:“值是必需的,不能为空”

代码如下:

//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>&nbsp;'; ?><?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>
如果不允许用户查看资源,则不会回显元素。因此,$request->getPost()没有该表单元素的值,并且isValid()返回了一个错误

有人解决过类似的问题吗?或者有人能给我指出正确的方向吗?
谢谢

问题在于,您没有在FormFilter类中执行任何安全检查,在FormFilter类中您定义了所需的字段


$form->isValid()函数根据这些过滤器元素检查发布的数据。因此,仅阻止视图中的“回波字段”是不够的,您仍然需要对过滤器元件进行安全检查。

另一种方法是制作两种表单,一种是前端表单,另一种是管理员表单。因为管理员的表单将有相同的字段加上一个额外的选择字段,所以您可以使管理员表单扩展前端表单。例如

class myForm 
{
    public function __construct(...) 
    {
         // add fields and set validators
    }
}
管理表单可以是:

class MyAdminForm extends myForm
{
    public function __construct(...)
    {
        parent::__construct(...);
        // add the extra field and extra validator
    }
}
这样,即使您编辑前端表单(或验证器),后端也将始终是最新的

希望这有帮助:)


Stoyan

这个博客值得一读,特别是最后一节关于验证组的内容,谢谢你的提示。我认为,理想情况下,这不是我想要的。字段可能会增加,例如访问级别可能我没有完全理解您的话,但您的帖子为我指出了一个解决方案:基本上,我将ACL检查从视图脚本移动到表单和过滤器类。谢谢
class MyAdminForm extends myForm
{
    public function __construct(...)
    {
        parent::__construct(...);
        // add the extra field and extra validator
    }
}