Create new page programmatically in multi-lang - validation does not work

Heya

I am sort of stuck here and don’t really know whats wrong :frowning:

In an old Kirby instance with 3.3.3 this seems so be working fine, but with 3.8.4 it no longer works correctly for me?

# pages/test.yml
title: Test
preset: page

fields:
  email:
    type: email
    required: true
  salutation:
    type: text
    required: true
  firstname:
    type: text
    required: true
    validate:
      min: 2
      max: 255
  lastname:
    type: text
    required: true
    validate:
      min: 2
      max: 255
# default.php
<?php
$data = [
	'salutation' => 'MALE',
	// 'firstname' => 'First Name',
	'email' => 'mail@example.com',
	'lastname' => 'Last Name',
];

$new_page = new Page([
	'slug' => Str::random(5)
	'template' => 'test',
	'content' => $data,
]);

echo '<pre>'; print_r($new_page->errors()); echo '</pre>';
?>

output with 3.3.3 which is correct. firstname is not included in $data

Array
(
    [firstname] => Array
        (
            [label] => Firstname
            [message] => Array
                (
                    [required] => Please enter something
                    [min] => Please enter a value equal to or greater than 2
                )

        )

)

output with 3.8.4 (same code, same everything, just newer version of kirby)

Array
(
    [email] => Array
        (
            [label] => Email
            [message] => Array
                (
                    [required] => Please enter something
                )

        )

    [salutation] => Array
        (
            [label] => Salutation
            [message] => Array
                (
                    [required] => Please enter something
                )

        )

    [firstname] => Array
        (
            [label] => Firstname
            [message] => Array
                (
                    [required] => Please enter something
                    [min] => Please enter a value equal to or greater than 2
                )

        )

    [lastname] => Array
        (
            [label] => Lastname
            [message] => Array
                (
                    [required] => Please enter something
                    [min] => Please enter a value equal to or greater than 2
                )

        )

)

Changing the language slug (so /de or /en) only results in the translated error messages

This has all been tested with plainkit.
Since the versions differ a lot, I am guessing something changed, but I can’t seem to find it in the docs?

Thanks for any hint or solution!

Just creating a new page instance doesn’t validate the blueprint. That’s because a page is created as a draft, where no validation happens. Instead, validation takes place when updating the page (or when trying to publish it).

ah I see - did that change?
it did work in 3.3.3?

sorry. me again.

I tried updating after creating a new page, but the validation is still not correct?

$data = [
	'salutation' => 'MALE',
	'firstname' => 'First Name',
	'email' => 'mail@example.com',
	// 'lastname' => 'Last Name',
];

$new_page = new Page([
	'slug' => Str::random(5) . time() . Str::random(5),
	'template' => 'test',
	'content' => $data,
]);

$new_page->content()->update([
  'headline' => 'headline',
]);


echo '<pre>'; print_r($new_page->errors()); echo '</pre>';
echo '<pre>'; print_r($new_page->content()); echo '</pre>';

output

Array
(
    [email] => Array
        (
            [label] => Email
            [message] => Array
                (
                    [required] => Please enter something
                )

        )

    [salutation] => Array
        (
            [label] => Salutation
            [message] => Array
                (
                    [required] => Please enter something
                )

        )

    [firstname] => Array
        (
            [label] => Firstname
            [message] => Array
                (
                    [required] => Please enter something
                    [min] => Please enter a value equal to or greater than 2
                )

        )

    [lastname] => Array
        (
            [label] => Lastname
            [message] => Array
                (
                    [required] => Please enter something
                    [min] => Please enter a value equal to or greater than 2
                )

        )

)
Kirby\Cms\Content Object
(
    [salutation] => MALE
    [firstname] => First Name
    [email] => mail@example.com
    [headline] => headline
)

Sorry, what I wrote above was not quite complete. Only when a published page is updated is the blueprint evaluated.

oki doki!
But this has been changed in an update, has it not?

How could I use the internal validation for a new page (as previously with 3.3.3)?
If I add $new_page->publish()->errors() the listed errors are still the same as above

$data = [
	'salutation' => 'MALE',
	'firstname' => 'First Name',
	'email' => 'mail@example.com',
	// 'lastname' => 'Last Name',
];

$new_page = new Page([
	'slug' => Str::random(5) . time() . Str::random(5),
	'template' => 'test',
	'content' => $data,
]);

$new_page->content()->update([
  'headline' => 'headline',
]);

echo '<pre>'; print_r($new_page->publish()->errors()); echo '</pre>';

or does the page actually need to be added (e.g. using createChild())?

Never done it like that. But in any case, you have to store the result of updating in a new variable, because these objects are immutable. Try:

$newPage = $new_page->update([
  'headline' => 'headline',
]);
$newPage->publish()->errors();

thank you!