简体   繁体   English

drupal7:使用form_set_value()

[英]drupal7: using form_set_value()

I am trying to update a value during the validate phase of a node-form, ie if the custom validation error is fired, just empty one of the fields. 我试图在节点表单的验证阶段更新一个值,即如果触发了自定义验证错误,则只清空其中一个字段。

for the last 30 hours I am trying to make sense of the drupal api, but I am giving up. 在过去的30个小时里,我试图理解drupal api,但我放弃了。 I just do not seem to get the idea what the different values mean. 我似乎并不知道不同的值意味着什么。

the function is: form_set_value($element, $value, &$form_state) 函数是:form_set_value($ element,$ value,&$ form_state)

now i understand that the last value is simply the $form_state, that I am having through the validate function. 现在我明白最后一个值只是$ form_state,我通过validate函数。 but what about $element and $value? 但是$ element和$ value呢?

I was trying a lot and apparently the desired value resides in $form['field_name']['und'][0]['value']['#value'] and only there. 我尝试了很多,显然所需的值存在于$ form ['field_name'] ['und'] [0] ['value'] ['#value']中,并且仅存在。

but when I am trying form_set_value($form['field_name']['und'][0]['value']['#value'],'foo',$form_state) it raises 但是当我尝试使用form_set_value($ form ['field_name'] ['und'] [0] ['value'] ['#value'],'foo',$ form_state)时,它会引发

    Recoverable fatal error: Argument 2 passed to drupal_array_set_nested_value() must be an array, string given, called in /includes/form.inc on line 2436 and defined in drupal_array_set_nested_value()

and when I am trying: 当我尝试时:

    $newvalue = $form['field_name']['und'][0]['value'];
    $newvalue['#value']='foo';
    form_set_value($form['field_name']['und'][0]['value'],$newvalue,$form_state);

it raises: 它提出:

    Warning: mb_strlen() expects parameter 1 to be string, array given in drupal_strlen()

Thanks for any help! 谢谢你的帮助!

Unless I'm really mis-understanding what you're trying to do this will work: 除非我真的误解你要做的事情,否则这会有效:

$form_state['values']['field_name']['und'][0]['value'] = '';

When the form is re-built after a validation error the values in $form_state['values'] are used to re-populate the fields in the form. 在验证错误之后重新构建表单时, $form_state['values']$form_state['values']用于重新填充表单中的字段。 So, if you reset the value in the $form_state['values'] array it will not be there when the form is shown with the validation errors. 因此,如果重置$form_state['values']数组中$form_state['values'] ,当表单显示验证错误时,它将不存在。

After a lot of debugging, I finally managed to make this work. 经过大量的调试,我终于成功完成了这项工作。 The trick lies inside $form['complete form'] . 诀窍在于$form['complete form'] but first things first, how does form_set_value() work and what does it do? 但首先,form_set_value()是如何工作的以及它的作用是什么?

the form_set_value() function form_set_value()函数

as the docs suggested: 正如文档建议:

if you want to update the value of $form['elem1']['elem2'], which should be stored in $form_state['values']['elem1']['elem2'], you would set $element['#parents'] = array('elem1','elem2'). 如果你想更新$ form ['elem1'] ['elem2']的值,它应存储在$ form_state ['values'] ['elem1'] ['elem2']中,你可以设置$ element [ '#parents'] = array('elem1','elem2')。

now what does that mean? 这是什么意思? in my case, I had a textfield called 'field_event_title', which is the name I gave it on creation. 在我的情况下,我有一个名为'field_event_title'的文本字段,这是我在创建时给它的名称。 in $form , all fields have a sub-array in $form['field_name'] , which is in my case $form['field_event_title'] . $form ,所有字段都在$form['field_name']有一个子数组,在我的例子中是$form['field_event_title'] this is where the submitted value also is stored. 这也是存储提交值的地方。 now since it is a textfield, drupal maintains both the language and the delta [question for editors: is this right?] of the inputted data. 既然它是一个文本字段,drupal维护输入数据的语言和delta [编辑问题:这是对吗?]。 so in fact, the value is not stored in $form['field_name']['value'] , but in $form['field_name']['und'][0]['value'] (['und']=language; [0]=delta). 实际上,该值不存储在$form['field_name']['value'] ,而是存在于$form['field_name']['und'][0]['value'] (['und']中] =语言; [0] = delta)。 note that 'und' is the drupal key for the default language of the site, if it is, say, in german, then it would be 'de', however, in most cases it should be 'und'. 请注意,'und'是网站默认语言的drupal密钥,如果是德语,那么它将是'de',但在大多数情况下它应该是'und'。

to actually change the value using form_set_value(), one is ought to invoke the function by writing: form_set_value($form['field_name'],array('und' => array(0 => array('value' => 'foo'))),$form_state); 要使用form_set_value()实际更改值,应该通过写入来调用函数: form_set_value($form['field_name'],array('und' => array(0 => array('value' => 'foo'))),$form_state);

ie $element = $form['field_name'] $value=array('und' => array(0 => array('value' => 'foo'))) ie $element = $form['field_name'] $value=array('und' => array(0 => array('value' => 'foo')))

updating a form to repopulate it with different values than submitted (or clearing them) 更新表单以使用与提交的值不同的值重新填充表单(或清除它们)

but that did not work in my case since I wanted to clear fields once a custom validation error has been invoked. 但这在我的情况下不起作用,因为我想在调用自定义验证错误后清除字段。 now one would suspect that the form repopulates itself using the values inside $form_state['values'] (which is actually the place where the values are stored, the actual place that gets updated when using form_set_value() and the place which generates the $form later.), but that is not the case: it uses the values inside $form_state['complete form'] , which is a 'copy' of $form (notice that it is spelled 'complete form', with a space, not an underscore). 现在有人会怀疑表单使用$form_state['values']的值重新填充自身(实际上是存储值的位置,使用form_set_value()时获得更新的实际位置以及生成$form稍后$form 。),但事实并非如此:它使用$form_state['complete form'] ,这是$form的'副本'(请注意,拼写为'完整形式',带有空格,不是下划线)。

so using $form_state['complete form']['field_name']['und'][0]['value']['#value']='foo'; 所以使用$form_state['complete form']['field_name']['und'][0]['value']['#value']='foo'; is what updates the values that actually repopulate the form on a validation error. 是更新实际在验证错误上重新填充表单的值。 (note: you can, as do I in my usecase, set it to =NULL to simply empty the field). (注意:您可以像在我的用例中一样,将其设置为=NULL以简单地清空该字段)。

summary 摘要

now where is the difference between $form['field_name'] (eg updating through form_set_value() ) and $form['complete form'] ? 现在$form['field_name'] (例如通过form_set_value()更新)和$form['complete form']之间的区别在哪里? well, the former updates the actual value, which then gets stored inside the database, the latter is being used to repopulate a form when it failed a validation. 好吧,前者更新实际值,然后将其存储在数据库中,后者用于在验证失败时重新填充表单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM