Created
May 20, 2016 08:43
-
-
Save cmcintosh/77e4cc4d5f0ef4bcb3d7cb3083a432aa to your computer and use it in GitHub Desktop.
buildForm Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$form['username'] = array( | |
'#type' => 'textfield', | |
'#title' => t('Username'), | |
'#ajax' => [ | |
'callback' => '::validateEmailAjax', | |
'event' => 'change', | |
], | |
'#suffix' => '<span class="username-available"></span>' | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function validateEmailAjax(array &$form, FormStateInterface $form_state) { | |
$available = $this->validateUsername($form, $form_state); // returns back boolean. | |
$response = new AjaxResponse(); | |
$css = ''; | |
if ($available) { | |
$css = "<style>.username-available { border: 1px solid green; }</style>"; | |
$message = t('This username is available.'); | |
} | |
else { | |
$css = "<style>.username-available { border: 1px solid red; }</style>"; | |
} | |
$response->addCommand(new CssCommand('#edit-username', $css)); | |
$response->addcommand(new HtmlCommand('.username-available', $message)); | |
return $response; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment