Commit bfa73c9f authored by alexpott's avatar alexpott

Issue #1863020 by dawehner, catch, paulmckibben, sunset_bill, damiankloip,...

Issue #1863020 by dawehner, catch, paulmckibben, sunset_bill, damiankloip, Lendude, alansaviolobo, amarnus, CurtisTAG, jlbellido, deveshpal9, rpayanm: View's build fails when an unrelated form on the same page has validation errors
parent c7dd8211
......@@ -133,6 +133,12 @@ public function renderExposedForm($block = FALSE) {
}
$form = \Drupal::formBuilder()->buildForm('\Drupal\views\Form\ViewsExposedForm', $form_state);
$errors = $form_state->getErrors();
// If the exposed form had errors, do not build the view.
if (!empty($errors)) {
$this->view->build_info['abort'] = TRUE;
}
if (!$this->view->display_handler->displaysExposed() || (!$block && $this->view->display_handler->getOption('exposed_block'))) {
return array();
......
......@@ -36,6 +36,8 @@ class ExposedFormTest extends ViewTestBase {
protected function setUp() {
parent::setUp();
$this->enableViewsTestModule();
$this->drupalCreateContentType(array('type' => 'article'));
// Create some random nodes.
......@@ -398,4 +400,23 @@ protected function getExpectedExposedFormId(ViewExecutable $view) {
return Html::cleanCssIdentifier('views-exposed-form-' . $view->storage->id() . '-' . $view->current_display);
}
/**
* Tests a view which is rendered after a form with a validation error.
*/
public function testFormErrorWithExposedForm() {
$this->drupalGet('views_test_data_error_form_page');
$this->assertResponse(200);
$form = $this->cssSelect('form.views-exposed-form');
$this->assertTrue($form, 'The exposed form element was found.');
$this->assertRaw(t('Filter'), 'Ensure the exposed form is rendered before submitting the normal form.');
$this->assertRaw('<div class="views-row">', 'Views result shown.');
$this->drupalPostForm(NULL, array(), t('Submit'));
$this->assertResponse(200);
$form = $this->cssSelect('form.views-exposed-form');
$this->assertTrue($form, 'The exposed form element was found.');
$this->assertRaw(t('Filter'), 'Ensure the exposed form is rendered after submitting the normal form.');
$this->assertRaw('<div class="views-row">', 'Views result shown.');
}
}
......@@ -5,7 +5,6 @@
use Drupal\Component\Utility\Html;
use Drupal\Component\Utility\Tags;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Form\FormState;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\views\display\DisplayRouterInterface;
......@@ -1223,7 +1222,7 @@ public function build($display_id = NULL) {
/** @var \Drupal\views\Plugin\views\exposed_form\ExposedFormPluginInterface $exposed_form */
$exposed_form = $this->display_handler->getPlugin('exposed_form');
$this->exposed_widgets = $exposed_form->renderExposedForm();
if (FormState::hasAnyErrors() || !empty($this->build_info['abort'])) {
if (!empty($this->build_info['abort'])) {
$this->built = TRUE;
// Don't execute the query, $form_state, but rendering will still be executed to display the empty text.
$this->executed = TRUE;
......
<?php
namespace Drupal\views_test_data\Controller;
/**
* Controller class for views_test_data callbacks.
*/
class ViewsTestDataController {
/**
* Renders an error form page.
*
* This contains a form that will contain an error and an embedded view with
* an exposed form.
*/
public function errorFormPage() {
$build = array();
$build['view'] = array(
'#type' => 'view',
'#name' => 'test_exposed_form_buttons',
);
$build['error_form'] = \Drupal::formBuilder()->getForm('Drupal\views_test_data\Form\ViewsTestDataErrorForm');
return $build;
}
}
<?php
namespace Drupal\views_test_data\Form;
use Drupal\Core\Form\FormInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements a test form that has a validation error.
*/
class ViewsTestDataErrorForm implements FormInterface {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'views_test_data_error_form';
}
/**
* {@inheritdoc}
*/
public function buildForm(array $form, FormStateInterface $form_state) {
$form['text'] = array(
'#type' => 'textfield',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
$form_state->setErrorByName('text', t('Form validation error'));
}
/**
* {@inheritdoc}
*/
public function submitForm(array &$form, FormStateInterface $form_state) {
}
}
......@@ -19,3 +19,11 @@ views_test_data.form_multiple:
_controller: '\Drupal\views_test_data\Controller\ViewsTestFormMultipleController::testPage'
requirements:
_access: 'TRUE'
views_test_data.error_form_page:
path: '/views_test_data_error_form_page'
defaults:
_title: 'Test Views Form Exposed Errors'
_controller: '\Drupal\views_test_data\Controller\ViewsTestDataController::errorFormPage'
requirements:
_access: 'TRUE'
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment