Skip to content

Commit

Permalink
ENH allow stacked messages on FormMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewandante committed Oct 3, 2023
1 parent 44b1700 commit b1b9b41
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,8 @@ public function loadMessagesFrom($result)
$owner = $this;
}

$owner->setMessage($message['message'], $message['messageType'], $message['messageCast']);
$owner->appendMessage($message['message'], $message['messageType'], $message['messageCast']);

}
return $this;
}
Expand Down
25 changes: 24 additions & 1 deletion src/Forms/FormMessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ trait FormMessage
*/
protected $messageCast = null;


/**
* Returns the field message, used by form validation.
*
Expand Down Expand Up @@ -92,6 +91,30 @@ public function setMessage(
return $this;
}

/**
* Appends a message to the existing message if the types match, overrides if not
*/
public function appendMessage(
string $message,
string $messageType = ValidationResult::TYPE_ERROR,
string $messageCast = ValidationResult::CAST_TEXT,
): static {
if (!empty($this->message)) {
$canBeMerged = ($messageType === $this->getMessageType() && $messageCast === $this->getMessageCast());
// Checks that the exact message string is not already contained before appending
if ($canBeMerged && strpos($this->message, $message) === false) {
$message = sprintf(
'%s%s%s',
$this->message,
$messageType === ValidationResult::CAST_HTML ? '<br />' : PHP_EOL,
$message
);
}
}

return $this->setMessage($message, $messageType, $messageCast);
}

/**
* Get casting helper for message cast, or null if not known
*
Expand Down
35 changes: 35 additions & 0 deletions tests/php/Forms/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1030,6 +1030,41 @@ public function testFieldMessageEscapeHtml()
);
}

public function testFieldMessageAppend()
{
$form = $this->getStubForm();
$form->appendMessage('Just a string');
$parser = new CSSContentParser($form->forTemplate());
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
$this->assertStringContainsString('Just a string', $messageEls[0]->asXML());

$form->appendMessage('Certainly different');
$parser = new CSSContentParser($form->forTemplate());
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
$this->assertStringContainsString('Just a string' . PHP_EOL . 'Certainly different', $messageEls[0]->asXML());

$form->appendMessage('Just a string');
$parser = new CSSContentParser($form->forTemplate());
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
$this->assertStringContainsString('Just a string' . PHP_EOL . 'Certainly different', $messageEls[0]->asXML());
$this->assertEquals(1, substr_count($messageEls[0]->asXML(), 'Just a string'), 'Should not append if already present');

$form->appendMessage('This is a good message', ValidationResult::TYPE_GOOD);
$parser = new CSSContentParser($form->forTemplate());
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
$this->assertStringContainsString('This is a good message', $messageEls[0]->asXML());
$this->assertStringNotContainsString('Just a string', $messageEls[0]->asXML());
$this->assertStringNotContainsString('Certainly different', $messageEls[0]->asXML());

$form->appendMessage('HTML is the future of the web', ValidationResult::TYPE_GOOD, ValidationResult::CAST_HTML);
$parser = new CSSContentParser($form->forTemplate());
$messageEls = $parser->getBySelector('#Form_Form_key1_Holder .message');
$this->assertStringContainsString('HTML is the future of the web', $messageEls[0]->asXML());
$this->assertStringNotContainsString('This is a good message', $messageEls[0]->asXML());
$this->assertStringNotContainsString('Just a string', $messageEls[0]->asXML());
$this->assertStringNotContainsString('Certainly different', $messageEls[0]->asXML());
}

public function testGetExtraFields()
{
$form = new FormTest\ExtraFieldsForm(
Expand Down

0 comments on commit b1b9b41

Please sign in to comment.