禁止在使用安全组件和jQuery一个CakePHP的表单输入元素 [英] Disabling input elements in a CakePHP form that uses Security component and jQuery

查看:111
本文介绍了禁止在使用安全组件和jQuery一个CakePHP的表单输入元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在CakePHP中有两个活的搜索文本输入的表单。他们中的每一个,当用户选择一结果更新一隐藏字段的值。该模型被称为录制,以及所涉及的属性。

I have a form in CakePHP that has two live-search text input. Each one of them updates the value of a hidden field when the user selects a result. The model is called Record, and the attributes involved are


  • budget_id

  • program_id

  • concept_id

  • budget_id
  • program_id
  • concept_id

我创建了一个表格的以这种方式使用表单助手

...
<?php echo $this->Form->create('Record') ?>
<h1>Create a record</h1>

<?php echo $this->Form->hidden('Record.budget_id', array('value' => $budget['Budget']['id'])) ?>

<?php echo $this->Form->hidden('Record.program_id') ?>
<?php echo $this->Form->input('Record.program_id_search', array(...)) ?>

<?php echo $this->Form->hidden('Record.concept_id') ?>
<?php echo $this->Form->input('Record.concept_id_search', array(...)) ?>

<?php echo $this->Form->submit('Send') ?>
<?php echo $this->Form->end(); ?>
...

正如你所看到的,存储模型属性的输入字段是隐藏的。本次现场搜索框配置了jQuery的自动完成插件。

As you can see, the input fields that store the model attributes are hidden. The live-search boxes are configured with the jQuery's autocomplete plugin.

接着我禁用了 beforeFilter 方法了两个附加字段CakePHP的手动建议,从而使安全组件忽略它们和表单通过验证:

Following the CakePHP manual recommendations I have disabled the two extra fields in beforeFilter method, so that the Security component ignores them and the form passes validation:

public function beforeFilter() {
  $this->Security->disabledFields = array(
    'Record.program_id_search',
    'Record.concept_id_search',
  );
}

看来,每当我改变从Javascript隐藏的输入值的CakePHP生气,并将其发送给我的的黑洞的方法。根据文档的确定。

It seems that CakePHP gets angry whenever I change the value of hidden inputs from Javascript and it sends me to the blackhole method. That's OK according to documentation.

但让我惊讶的是,安全组件一直无视我的 disabledFields 设置。

But what surprises me is that the Security component keeps ignoring my disabledFields settings.

我一直在寻找在几个网络资源,每个人都点到 disabledFields 选项。但它不为我工作。

I've been searching in several web sources and everybody point to the disabledFields options. But it does not work for me.

有什么建议?

谢谢!

更新

我已经找到一种解决方法,但它真的很丑陋。我已经取代定期选择字段隐藏的输入字段,但设置CSS display属性为

I have found a workaround but it's really really ugly. I have replaced the hidden input fields with regular select fields, but setting the CSS display property as none.

这样的安全组件不抱怨了,和用户保持观看一对夫妇的活搜索框。

This way the Security component does not complain anymore, and the user keeps viewing a couple of live-search boxes.

我不明白为什么改变选择的JavaScript它的确定,但改变一个隐藏的输入没有。

I don't understand why changing a select with Javascript it's ok, but changing a hidden input not.

推荐答案

这是因为在安全组件的锁定隐藏字段,在散不只是他们的名字,而且他们的价值保存。因此,当你改变自己的价值,你让整个窗体无效。唯一的解决办法是切换这些领域从隐藏到正常场,包裹里面显示:无; DIV

It happens because the Security Component locks the hidden fields, saving in the hash not just their name but also their value. Therefore when you change their value, you make the whole form invalid. The only solution is to switch those fields from hidden to normal field, wrapped inside a display:none; div.

另一种方法是禁止在该领域的检查,但您发布的code不是这样做的正确方法。你应该组件的配置过程中,而不是指定的字段,如:

Another way would be to disable the checks on that field, but the code you posted isn't the right way to do it. You should instead specify the fields during the configuration of the component, like this:

var $components = array('Security' => array(
    'blackHoleCallback' => 'callback',
    'requireAuth' => array('action1', 'action2'),
    'allowedControllers' => array('controller'),
    'allowedActions' => array('action1', 'action2'),
    'disabledFields' => array('Record.program_id_search', 'Record.concept_id_search')
    )
);

这篇关于禁止在使用安全组件和jQuery一个CakePHP的表单输入元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆