Skip to content

Commit 06786ad

Browse files
[5.x] Handle 0 values in text fields and null string in slugs (#13786)
Co-authored-by: Jason Varga <jason@pixelfear.com>
1 parent e55c8b9 commit 06786ad

5 files changed

Lines changed: 34 additions & 4 deletions

File tree

resources/js/components/slugs/Slug.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export default class Slug {
113113
this.#controller = new AbortController;
114114

115115
let aborted = false;
116-
return axios.post(cp_url('slug'), payload, { signal: this.#controller.signal })
116+
return axios.post(cp_url('slug'), payload, { signal: this.#controller.signal, transformResponse: [(data) => data] })
117117
.then(response => response.data)
118118
.catch(e => {
119119
if (axios.isCancel(e)) {

resources/js/components/slugs/Slugify.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ export default {
4242
handler() {
4343
if (!this.shouldSlugify) {
4444
this.slug = this.to;
45-
} else if (!this.from) {
45+
} else if (this.from === null || this.from === undefined || this.from === '') {
4646
this.slug = '';
4747
} else {
4848
this.slugify();

src/Fieldtypes/Text.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,10 @@ public function process($data)
154154

155155
public function preProcessIndex($value)
156156
{
157-
if ($value) {
158-
return $this->config('prepend').$value.$this->config('append');
157+
if (is_null($value)) {
158+
return null;
159159
}
160+
161+
return $this->config('prepend').$value.$this->config('append');
160162
}
161163
}

tests/Feature/SlugTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ public static function slugProvider()
4040
'german characters' => ['Björn Müller', '-', 'de', 'bjoern-mueller'],
4141
'arabic characters' => ['صباح الخير', '-', 'ar', 'sbah-alkhyr'],
4242
'alternate separator' => ['one two three', '_', 'en', 'one_two_three'],
43+
'null string' => ['null', '-', 'en', 'null'],
44+
'zero string' => ['0', '-', 'en', '0'],
45+
'false string' => ['false', '-', 'en', 'false'],
4346
];
4447
}
4548
}

tests/Fieldtypes/TextTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,29 @@ public static function processValuesProvider()
3333
'number' => ['number', [0, 3, 3, 3.14, null]],
3434
];
3535
}
36+
37+
#[Test]
38+
#[DataProvider('preProcessIndexProvider')]
39+
public function it_pre_processes_index_values($config, $value, $expected)
40+
{
41+
$field = (new Text)->setField(new Field('test', array_merge([
42+
'type' => 'text',
43+
], $config)));
44+
45+
$this->assertSame($expected, $field->preProcessIndex($value));
46+
}
47+
48+
public static function preProcessIndexProvider()
49+
{
50+
return [
51+
'string value' => [[], 'hello', 'hello'],
52+
'null value' => [[], null, null],
53+
'zero integer' => [[], 0, '0'],
54+
'zero string' => [[], '0', '0'],
55+
'zero with prepend' => [['prepend' => '$'], 0, '$0'],
56+
'zero with append' => [['append' => '%'], 0, '0%'],
57+
'zero with prepend and append' => [['prepend' => '$', 'append' => '%'], 0, '$0%'],
58+
'string with prepend' => [['prepend' => '$'], 'hello', '$hello'],
59+
];
60+
}
3661
}

0 commit comments

Comments
 (0)