Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Entry: validate data with collection blueprint #1387

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions content/collections/repositories/entry-repository.md
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,38 @@ $entry->afterSave(function ($entry) {
$entry->save();
```

### Validate entry data

A Collectiion Blueprint can be used to validate the data before you use it to create an entry. This relies on the same field validation rules you already have in the Blueprint.

```php
use Statamic\Facades\Blueprint;
use Statamic\Facades\Entry;

$data = [
'title' => 'Statamic is awesome',
'slug' => 'statamic-is-awesome',
'foo' => 'bar',
];


$validData = Blueprint::setDirectory('resources/blueprints/collections/article')
->find('article')
->fields()
->addValues($data)
->validator()->validate();


$entry = Entry::make()
->collection('article')
->slug(data_get($validData, 'slug'))
->published()
->data($validData);


$entry->save();
```

### Localization

#### Localizing an entry
Expand Down