Skip to content

Commit 1dcc3db

Browse files
committed
Try to fix placeholder application for bureaucracy fields
We were applying replacements in setVal() but that is weirdly called during parsing and then gets cached. But it's also called again when the validation fails... It's all a mess. This tries to work around the issue, but I am far from sure this hits all the (edge) cases. Really without a refactoring of bureaucracy it's nearly impossible to interface cleanly with it.
1 parent 62ab667 commit 1dcc3db

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

helper/field.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,15 +47,25 @@ public function initialize($args)
4747
*/
4848
protected function setVal($value)
4949
{
50+
global $INPUT;
51+
5052
if (!$this->column) {
5153
$value = '';
5254
} else {
53-
$value = $this->replace($value);
54-
$validator = new ValueValidator();
55-
$this->error = !$validator->validateValue($this->column, $value);
56-
if ($this->error) {
57-
foreach ($validator->getErrors() as $error) {
58-
msg(hsc($error), -1);
55+
56+
// This method is called at parsing time, and again on action submit
57+
// we really only want to validate the value on action submit
58+
//
59+
// At parsing time, the value might still be a placeholder. Ideally
60+
// bureaucracy would already handle this. But well...
61+
if($INPUT->post->has('bureaucracy')) {
62+
$validator = new ValueValidator();
63+
64+
$this->error = !$validator->validateValue($this->column, $value);
65+
if ($this->error) {
66+
foreach ($validator->getErrors() as $error) {
67+
msg(hsc($error), -1);
68+
}
5969
}
6070
}
6171
}
@@ -154,22 +164,23 @@ public function replacementMultiValueCallback($matches)
154164
* Returns a Value object for the current column.
155165
* Special handling for Page and Lookup literal form values.
156166
*
167+
* Used for rendering the field only.
168+
*
157169
* @return Value
158170
*/
159171
protected function createValue()
160172
{
161-
/*
173+
// The value in bureaucracy is always a string, if unset init it.
162174
$preparedValue = $this->opt['value'] ?? '';
163-
if($this->column->isMulti()) {
164-
// multi-value fields are treated as comma-separated lists
165-
$preparedValue = explode(',', $preparedValue);
166-
$preparedValue = array_map('trim', $preparedValue);
167-
$preparedValue = array_filter($preparedValue);
168-
}
169-
*/
170175

171-
// input value or appropriately initialized empty value
172-
$preparedValue = $this->opt['value'] ?? ($this->column->isMulti() ? [] : '');
176+
// apply placeholder replacements
177+
$preparedValue = $this->replace($preparedValue);
178+
179+
// Validating here is actually not a validation, that will happen again in setVal() when
180+
// the form is submitted. instead, here we're using it's data cleanup functionality on the value.
181+
$validator = new ValueValidator();
182+
$validator->validateValue($this->column, $preparedValue);
183+
/** @var string|array $preparedValue is now an array or a string */
173184

174185
// page fields might need to be JSON encoded depending on usetitles config
175186
if (

0 commit comments

Comments
 (0)