Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * {@inheritdoc}
- */
- public function buildForm(array $form, FormStateInterface $form_state) {
- $form['#tree'] = true; //Make the form fields a hierachical array*
- $form['#prefix'] = '<div id="sponsorship-form-wrapper">';
- $form['#suffix'] = '</div>';
- $form['sponsor'] = [
- '#type' => 'fieldset',
- '#title' => $this->t('You (sponsor)'),
- ];
- $form['sponsor']['civility'] = [
- '#type' => 'select',
- '#title' => $this->t('Civility'),
- '#options' => array('Miss' => $this->t('Miss'), 'Mister' => $this->t('Mister')),
- '#size' => 1,
- /* .......... */
- $form['sponsor']['submit'] = [
- '#type' => 'submit',
- '#value' => t('Send'),
- ];
- $form['sponsee'] = [
- '#type' => 'fieldset',
- '#title' => $this->t('Your sponsee'),
- '#prefix' => '<div id="sponsees-fieldset-wrapper">',
- '#suffix' => '</div>',
- ];
- //Default value of nb_sponsees to 1
- if ($form_state->getValue('nb_sponsee') == null) {
- $form_state->setValue('nb_sponsee', 1);
- }
- for ($i = 0; $i < $form_state->getValue('nb_sponsee'); $i++) {
- $form['sponsee'][$i]['civility'] = [
- '#type' => 'select',
- '#title' => $this->t('Civility'),
- '#options' => array('Miss' => $this->t('Miss'), 'Mister' => $this->t('Mister')),
- '#size' => 1,
- ];
- $form['sponsee'][$i]['firstname'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Firstname'),
- '#maxlength' => 64,
- '#size' => 64,
- ];
- $form['sponsee'][$i]['name'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Name'),
- '#maxlength' => 64,
- '#size' => 64,
- ];
- $form['sponsee'][$i]['phone'] = [
- '#type' => 'textfield',
- '#title' => $this->t('Phone'),
- '#maxlength' => 64,
- '#size' => 64,
- ];
- $form['sponsee'][$i]['e_mail'] = [
- '#type' => 'email',
- '#title' => $this->t('E-mail'),
- ];
- }
- //If less of 3 sponsee we add the button
- if ($form_state->getValue('nb_sponsee') < 3) {
- $form['sponsee']['add_sponsee'] = [
- '#type' => 'submit',
- '#name' => 'add_sponsee',
- '#submit' => [$this, 'ajaxHandleSponseesSubmit'],
- '#value' => t('Add a sponsee'),
- '#ajax' => array(
- 'callback' => [$this, 'ajaxHandleSponseesCallback'],
- 'wrapper' => 'sponsees-fieldset-wrapper',
- 'event' => 'click',
- 'effect' => 'fade',
- 'progress' => array('message' => '', 'type' => 'throbber'),
- ),
- ];
- }
- return $form;
- }
- /**
- * {@inheritdoc}
- */
- public function validateForm(array &$form, FormStateInterface $form_state) {
- parent::validateForm($form, $form_state);
- }
- /**
- * {@inheritdoc}
- */
- public function submitForm(array &$form, FormStateInterface $form_state) {
- // Display result.
- foreach ($form_state->getValues() as $key => $value) {
- drupal_set_message($key . ': ' . $value);
- }
- }
- /*
- * Custom Submit to increment nb_sponsees
- */
- function ajaxHandleSponseesSubmit(array &$form, FormStateInterface &$form_state) {
- $form_state->setValue('nb_sponsee', $form_state->getValue('nb_sponsee') + 1);
- $form_state->setRebuild(true);
- }
- /**
- * Ajax submit handler that will return the form structure
- */
- public function ajaxHandleSponseesCallback(array &$form, FormStateInterface &$form_state) {
- return $form['sponsee'];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement