Skip to content

Commit

Permalink
Merge branch '5.x' into feature/ajax-form
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer authored Jan 4, 2023
2 parents 32e80b8 + f219456 commit dc43421
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
47 changes: 44 additions & 3 deletions core-bundle/contao/forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ class Form extends Hybrid
*/
protected $strTemplate = 'form_wrapper';

/**
* @var array<string>
*/
private array $errors = array();

/**
* Remove name attributes in the back end so the form is not validated
*
Expand Down Expand Up @@ -101,6 +106,36 @@ public function generate()
return parent::generate();
}

/**
* @return array<string>
*/
public function getErrors(): array
{
return $this->errors;
}

public function hasErrors(): bool
{
return !empty($this->errors);
}

/**
* @param array<string> $errors
*/
public function setErrors(array $errors): self
{
$this->errors = $errors;

return $this;
}

public function addError(string $error): self
{
$this->errors[] = $error;

return $this;
}

/**
* Generate the form
*/
Expand Down Expand Up @@ -267,7 +302,7 @@ protected function compile()
}

// Remove any uploads, if form did not validate (#1185)
if ($doNotSubmit && $hasUpload)
if (($doNotSubmit || $this->hasErrors()) && $hasUpload)
{
foreach ($arrFiles as $upload)
{
Expand All @@ -285,7 +320,7 @@ protected function compile()

// Add a warning to the page title
if (
$doNotSubmit
($doNotSubmit || $this->hasErrors())
&& !Environment::get('isAjaxRequest')
&& ($responseContext = System::getContainer()->get('contao.routing.response_context_accessor')->getResponseContext())
&& $responseContext->has(HtmlHeadBag::class)
Expand All @@ -308,7 +343,8 @@ protected function compile()
$strAttributes .= ' class="' . $arrAttributes[1] . '"';
}

$this->Template->hasError = $doNotSubmit;
$this->Template->hasError = $doNotSubmit || $this->hasErrors();
$this->Template->errors = $this->getErrors();
$this->Template->attributes = $strAttributes;
$this->Template->enctype = $hasUpload ? 'multipart/form-data' : 'application/x-www-form-urlencoded';
$this->Template->maxFileSize = $hasUpload ? $this->objModel->getMaxUploadFileSize() : false;
Expand Down Expand Up @@ -596,6 +632,11 @@ protected function processFormData($arrSubmitted, $arrLabels, $arrFields, $arrFi
System::getContainer()->get('monolog.logger.contao.forms')->info('Form "' . $this->title . '" has been submitted by a guest.');
}

if ($this->hasErrors())
{
return;
}

$targetPageData = null;

// Check whether there is a jumpTo page
Expand Down
1 change: 1 addition & 0 deletions core-bundle/contao/templates/forms/form_wrapper.html5
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<?php else: ?>
<?php $this->insert('form_inline', $this->arrData) ?>
<?php endif; ?>

</div>
<!-- indexer::continue -->

Expand Down

0 comments on commit dc43421

Please sign in to comment.