mirror of
https://github.com/getgrav/grav.git
synced 2025-02-20 19:56:53 +01:00
Improve FormTrait backwards compatibility with existing forms
This commit is contained in:
parent
864c5027c6
commit
6cdfaeb8fb
|
|
@ -1,3 +1,9 @@
|
|||
# v1.6.7
|
||||
## mm/dd/2019
|
||||
|
||||
1. [](#improved)
|
||||
* Improve `FormTrait` backwards compatibility with existing forms
|
||||
|
||||
# v1.6.6
|
||||
## 04/17/2019
|
||||
|
||||
|
|
|
|||
|
|
@ -54,7 +54,7 @@ class FlexForm implements FlexFormInterface
|
|||
$this->setObject($object);
|
||||
$this->setId($this->getName());
|
||||
$this->setUniqueId(md5($uniqueId));
|
||||
$this->errors = [];
|
||||
$this->messages = [];
|
||||
$this->submitted = false;
|
||||
|
||||
$flash = $this->getFlash();
|
||||
|
|
|
|||
|
|
@ -140,6 +140,11 @@ interface FormInterface extends RenderInterface, \Serializable
|
|||
*/
|
||||
public function isValid(): bool;
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getError(): ?string;
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -30,6 +30,13 @@ use Twig\TemplateWrapper;
|
|||
*/
|
||||
trait FormTrait
|
||||
{
|
||||
/** @var string */
|
||||
public $status = 'success';
|
||||
/** @var string */
|
||||
public $message;
|
||||
/** @var string[] */
|
||||
public $messages = [];
|
||||
|
||||
/** @var string */
|
||||
private $name;
|
||||
/** @var string */
|
||||
|
|
@ -38,8 +45,6 @@ trait FormTrait
|
|||
private $uniqueid;
|
||||
/** @var bool */
|
||||
private $submitted;
|
||||
/** @var string[] */
|
||||
private $errors;
|
||||
/** @var Data|object|null */
|
||||
private $data;
|
||||
/** @var array|UploadedFileInterface[] */
|
||||
|
|
@ -164,12 +169,24 @@ trait FormTrait
|
|||
*/
|
||||
public function handleRequest(ServerRequestInterface $request): FormInterface
|
||||
{
|
||||
// Set current form to be active.
|
||||
$grav = Grav::instance();
|
||||
$forms = $grav['forms'] ?? null;
|
||||
if ($forms) {
|
||||
$forms->setActiveForm($this);
|
||||
|
||||
/** @var Twig $twig */
|
||||
$twig = $grav['twig'];
|
||||
$twig->twig_vars['form'] = $this;
|
||||
|
||||
}
|
||||
|
||||
try {
|
||||
[$data, $files] = $this->parseRequest($request);
|
||||
|
||||
$this->submit($data, $files);
|
||||
} catch (\Exception $e) {
|
||||
$this->errors[] = $e->getMessage();
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
|
@ -191,12 +208,17 @@ trait FormTrait
|
|||
|
||||
public function isValid(): bool
|
||||
{
|
||||
return !$this->errors;
|
||||
return $this->status === 'success';
|
||||
}
|
||||
|
||||
public function getError(): ?string
|
||||
{
|
||||
return !$this->isValid() ? $this->message : null;
|
||||
}
|
||||
|
||||
public function getErrors(): array
|
||||
{
|
||||
return $this->errors;
|
||||
return !$this->isValid() ? $this->messages : [];
|
||||
}
|
||||
|
||||
public function isSubmitted(): bool
|
||||
|
|
@ -206,7 +228,7 @@ trait FormTrait
|
|||
|
||||
public function validate(): bool
|
||||
{
|
||||
if ($this->errors) {
|
||||
if (!$this->isValid()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
@ -214,19 +236,14 @@ trait FormTrait
|
|||
$this->validateData($this->data);
|
||||
$this->validateUploads($this->getFiles());
|
||||
} catch (ValidationException $e) {
|
||||
$list = [];
|
||||
foreach ($e->getMessages() as $field => $errors) {
|
||||
$list[] = $errors;
|
||||
}
|
||||
$list = array_merge(...$list);
|
||||
$this->errors = $list;
|
||||
$this->setErrors($e->getMessages());
|
||||
} catch (\Exception $e) {
|
||||
$this->errors[] = $e->getMessage();
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
|
||||
$this->filterData($this->data);
|
||||
|
||||
return empty($this->errors);
|
||||
return $this->isValid();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -252,7 +269,7 @@ trait FormTrait
|
|||
|
||||
$this->submitted = true;
|
||||
} catch (\Exception $e) {
|
||||
$this->errors[] = $e->getMessage();
|
||||
$this->setError($e->getMessage());
|
||||
}
|
||||
|
||||
return $this;
|
||||
|
|
@ -265,7 +282,9 @@ trait FormTrait
|
|||
|
||||
$this->data = null;
|
||||
$this->files = [];
|
||||
$this->errors = [];
|
||||
$this->status = 'success';
|
||||
$this->message = null;
|
||||
$this->messages = [];
|
||||
$this->submitted = false;
|
||||
$this->flash = null;
|
||||
}
|
||||
|
|
@ -310,7 +329,6 @@ trait FormTrait
|
|||
}
|
||||
|
||||
/**
|
||||
|
||||
* Get form flash object.
|
||||
*
|
||||
* @return FormFlash
|
||||
|
|
@ -376,16 +394,6 @@ trait FormTrait
|
|||
$this->flash = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all errors.
|
||||
*
|
||||
* @param array $errors
|
||||
*/
|
||||
protected function setErrors(array $errors): void
|
||||
{
|
||||
$this->errors = array_merge($this->errors, $errors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a single error.
|
||||
*
|
||||
|
|
@ -393,7 +401,19 @@ trait FormTrait
|
|||
*/
|
||||
protected function setError(string $error): void
|
||||
{
|
||||
$this->errors[] = $error;
|
||||
$this->status = 'error';
|
||||
$this->message = $error;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set all errors.
|
||||
*
|
||||
* @param array $errors
|
||||
*/
|
||||
protected function setErrors(array $errors): void
|
||||
{
|
||||
$this->status = 'error';
|
||||
$this->messages = $errors;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -563,7 +583,7 @@ trait FormTrait
|
|||
$value = json_decode($value, true);
|
||||
if ($value === null && json_last_error() !== JSON_ERROR_NONE) {
|
||||
unset($data[$key]);
|
||||
$this->errors[] = "Badly encoded JSON data (for {$key}) was sent to the form";
|
||||
$this->setError("Badly encoded JSON data (for {$key}) was sent to the form");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -583,7 +603,9 @@ trait FormTrait
|
|||
'id' => $this->id,
|
||||
'uniqueid' => $this->uniqueid,
|
||||
'submitted' => $this->submitted,
|
||||
'errors' => $this->errors,
|
||||
'status' => $this->status,
|
||||
'message' => $this->message,
|
||||
'messages' => $this->messages,
|
||||
'data' => $data,
|
||||
'files' => $this->files,
|
||||
];
|
||||
|
|
@ -598,7 +620,9 @@ trait FormTrait
|
|||
$this->id = $data['id'];
|
||||
$this->uniqueid = $data['uniqueid'];
|
||||
$this->submitted = $data['submitted'] ?? false;
|
||||
$this->errors = $data['errors'] ?? [];
|
||||
$this->status = $data['status'] ?? 'success';
|
||||
$this->message = $data['message'] ?? null;
|
||||
$this->messages = $data['messages'] ?? [];
|
||||
$this->data = isset($data['data']) ? new Data($data['data'], $this->getBlueprint()) : null;
|
||||
$this->files = $data['files'] ?? [];
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user