7.4 表单验证

龸龶龳龲龮龭龬龩龧龶龤龢龶龞龜龘龖龕龞龜龔龐龍龳龲龌龈龇

1龂validateForm

龬齟齞龧龶齝龲龍龳龲龞龜龘龖齙齖齔齓齐龳龲齍齌龍齉齞齇齄齃齁鼾鼻鼷鼵5龇

/**
 * {@inheritdoc}
 */

public function validateForm(array &$form, FormStateInterface $form_state) {
    if (!$form_state->isValueEmpty('company_name')) {
        if (strlen($form_state->getValue('company_name')) <= 5) {
            $form_state->setErrorByName('company_name', t('Company name is less than 5 characters'));
        }
    }
}
  • isValueEmpty: 黜龕龞龜齉齞齇齄齃

  • getValue: 黛黚齍齌龍齉

  • setErrorByName: 默黗黓黑黐黏齙黍龧龶黋龌齞齍齌黇黃齙黍黀龶黋龌齞黐黏麽麻龇

2龂龢龶龞龜麵麴麰

龧龶龳龲龭龬麯麫龢龶龞龜麵麴麰龇麩龩龳龲麦麣validateForm麢龶龞龜麰龇龬齟齞龢龶龞龜麰龍麡麞麝麛:

$form_state->setValidateHandlers([
  ['::validateForm'],
  ['::method1'],
  [$this, 'method2']
]);

麏麋麉::麇$this龮龭龬麄麀鹼鹺鹷鹶龍龘龖龇

本书共79小节:

评论 (写第一个评论)