textdrupal8表格(代码片段)

author author     2022-12-02     149

关键词:

- know if you are rebuilding the whole form or only part of it and wrap the form accordingly with div element that has ID attribute that you will use in the #ajax definition on the triggering element as 'wrapper'. Use #prefix and #suffix attributes for this($form['#prefix'] = '<div id="myform-ajax-wrapper">'; $form['#suffix'] = '</div>';). Also keep in mind that if you have custom template for your form to NOT render the prefix and suffix in this case( form|without('#prefix', '#suffix') ) otherwise they will get rendered twice - by your template and also by the form theme wrapper. You cannot prevent this by setting #theme_wrappers to empty array since the form template contains the actual form html element.
- in your ajax submit handler, return the whole form or portion of it that you've wrapped and want to rebuild(return $form or return $form['myelement']). You can additionally use ajax commands instead of just returning the form structure but that is more advanced stuff.
- store every value in form state's storage until you submit the form. Do this in the submit handler($form_state->set('somevalue', $form_state->getValue('somevalue'))) and always call $form_state->setRebuild() if you are not doing the final form submission. I prefer having custom submit handlers but having more logic in the primary submit handler is totally ok as well.
- always use #name attribute on the button that is doing the submission and if you have only single form submit handler use $for_state->getTriggeringElement()['#name'] to detect which element has submitted the form.
- if you are using 'trigger_as' in the #ajax definition, in case you want to submit the form with select element for example, always use the same #ajax definition like you do on the button. In my experience it is required - although not stated in the documentation.
- using #limit_validation_errors can get very tricky sometimes and to figure out why the form is not working can take quite some time, so use it carefully(this is good for isolating form errors only on the element(s) that you are actually rebuilding so that your code does not influence other parts of the form).
- always use buttons to submit the form and if you want to have something fancy, like select being the triggering element, use the 'trigger_as' option of the #ajax configuration and hide the real button with 'js-hide' class for good UI.
- in the form definition, get the default values from the form state's storage if they exist or assign them into the storage if they don't. Otherwise the form won't work properly.
- don't use $this or anything else that you don't have access to externally, otherwise it will break the ajax. always use static ajax handlers.
- when finally submitting the form, depending on the fact that you (don't)have a custom form submission handler for the ajax, disable the form rebuilding by calling $form_state->setRebuild(FALSE).
- you can use the :: shorthand calls in the ajax submit element($element['#ajax']['callback'] = '::ajaxFormRebuild'; and $element['#submit'] = [['::ajaxFormSubmitHandler'];).
- the ajax callback is purely to return the rebuilt form or ajax commands. Never return altered form(ie. do not alter the form array in this callback).

textdrupal8主题(代码片段)

查看详情

textdrupal8响应图像(代码片段)

查看详情

textdrupal8预处理(代码片段)

查看详情

textdrupal8模块的最小composer安装(代码片段)

查看详情

textdrupal8模块的最小composer安装(代码片段)

查看详情

html联系表格和登录表格(代码片段)

查看详情

html联系表格7订购表格(代码片段)

查看详情

html订阅表格和地址表格(代码片段)

查看详情

html表格(代码片段)

查看详情

phplaravel表格(代码片段)

查看详情

markdowngoogle表格(代码片段)

查看详情

phputm表格(代码片段)

查看详情

phpacf表格(代码片段)

查看详情

htmlphp表格(代码片段)

查看详情

markdownbs表格(代码片段)

查看详情

cssflexbox表格(代码片段)

查看详情

phpprocesswire表格(代码片段)

查看详情

htmlhtml-表格(代码片段)

查看详情