Issue #2459753 by tim.plunkett: EntityForm::validate() should be able to modify the form structure

8.0.x
Alex Pott 2015-04-15 18:37:18 +02:00
parent 8bb0609820
commit d4d83576d2
21 changed files with 37 additions and 20 deletions

View File

@ -121,7 +121,7 @@ abstract class ContentEntityConfirmFormBase extends ContentEntityForm implements
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
// Override the default validation implementation as it is not necessary
// nor possible to validate an entity in a confirmation form.
}

View File

@ -74,7 +74,7 @@ class ContentEntityForm extends EntityForm implements ContentEntityFormInterface
* For more information about entity validation, see
* https://www.drupal.org/node/2015613.
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
$entity = $this->buildEntity($form, $form_state);
$this->getFormDisplay($form_state)->validateFormValues($entity, $form, $form_state);

View File

@ -247,7 +247,7 @@ class EntityForm extends FormBase implements EntityFormInterface {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
// @todo Remove this.
// Execute legacy global validation handlers.
$form_state->setValidateHandlers([]);

View File

@ -105,7 +105,7 @@ interface EntityFormInterface extends BaseFormIdInterface {
* @return \Drupal\Core\Entity\ContentEntityTypeInterface
* The built entity.
*/
public function validate(array $form, FormStateInterface $form_state);
public function validate(array &$form, FormStateInterface $form_state);
/**
* Form submission handler for the 'save' action.

View File

@ -123,7 +123,7 @@ abstract class ActionFormBase extends EntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
if ($this->plugin instanceof PluginFormInterface) {

View File

@ -273,7 +273,7 @@ class BlockForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
// The Block Entity form puts all block plugin form elements in the

View File

@ -223,7 +223,7 @@ class BlockContentForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
if ($this->entity->isNew()) {
$exists = $this->blockContentStorage->loadByProperties(array('info' => $form_state->getValue(['info', 0, 'value'])));
if (!empty($exists)) {

View File

@ -314,7 +314,7 @@ class CommentForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
$comment = parent::validate($form, $form_state);
// Customly trigger validation of manually added fields and add in

View File

@ -112,7 +112,7 @@ class ContactFormEditForm extends EntityForm implements ContainerInjectionInterf
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
// Validate and each email recipient.

View File

@ -38,7 +38,7 @@ class EntityDisplayModeAddForm extends EntityDisplayModeFormBase {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
$form_state->setValueForElement($form['id'], $this->targetEntityTypeId . '.' . $form_state->getValue('id'));

View File

@ -204,7 +204,7 @@ abstract class FilterFormatFormBase extends EntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
// @todo Move trimming upstream.

View File

@ -286,7 +286,7 @@ class NodeForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
$node = parent::validate($form, $form_state);
if ($node->id() && (node_last_changed($node->id(), $this->getFormLangcode($form_state)) > $node->getChangedTime())) {

View File

@ -204,7 +204,7 @@ class NodeTypeForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
$id = trim($form_state->getValue('type'));

View File

@ -133,7 +133,7 @@ class ResponsiveImageStyleForm extends EntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
// Only validate on edit.
if ($form_state->hasValue('keyed_styles')) {
// Check if another breakpoint group is selected.

View File

@ -144,7 +144,7 @@ abstract class SearchPageFormBase extends EntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
// Ensure each path is unique.

View File

@ -155,7 +155,7 @@ abstract class DateFormatFormBase extends EntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
// The machine name field should already check to see if the requested

View File

@ -94,7 +94,7 @@ class TermForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
// Ensure numeric values.

View File

@ -354,7 +354,7 @@ abstract class AccountForm extends ContentEntityForm {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\user\UserInterface $account */
$account = parent::validate($form, $form_state);

View File

@ -162,7 +162,7 @@ class ViewAddForm extends ViewFormBase {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
$wizard_type = $form_state->getValue(array('show', 'wizard_key'));
$wizard_instance = $this->wizardManager->createInstance($wizard_type);
$form_state->set('wizard', $wizard_instance->getPluginDefinition());

View File

@ -258,7 +258,7 @@ class ViewEditForm extends ViewFormBase {
/**
* {@inheritdoc}
*/
public function validate(array $form, FormStateInterface $form_state) {
public function validate(array &$form, FormStateInterface $form_state) {
parent::validate($form, $form_state);
$view = $this->entity;

View File

@ -9,6 +9,7 @@ namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Entity\EntityForm;
use Drupal\Core\Form\FormState;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Tests\UnitTestCase;
/**
@ -123,4 +124,20 @@ class EntityFormTest extends UnitTestCase {
$this->assertNull($result->get('key_controlled_by_plugin_collection'));
}
/**
* @covers ::validate
*/
public function testValidate() {
$form_object = $this->getMock('Drupal\Core\Entity\EntityFormInterface');
$form_object->expects($this->once())
->method('validate')
->willReturnCallback(function (array &$form, FormStateInterface $form_state) {
$form['foo'] = 'bar';
});
$form['baz'] = 'bim';
$form_state = new FormState();
$form_object->validate($form, $form_state);
$this->assertArrayHasKey('foo', $form);
}
}