This content originally appeared on Hussein Al Hammad and was authored by Hussein Al Hammad
When programmatically creating or updating a Collection Entry, you can lean on the Collection’s Blueprint to help you validate the fields values:
1<?php 2 3use Statamic\Facades\Blueprint; 4use Statamic\Facades\Entry; 5 6$data = [ 7 'slug' => 'some-title', 8 'title' => 'Some title', 9 // more data10];11 12// validate the data13$validated_data = Blueprint::setDirectory('resources/blueprints/collections/my_collection')14 ->find('my_collection')15 ->fields()16 ->addValues($data)17 ->validator()->validate();18 19// create & save the entry with the validated data20$entry = Entry::make()21 ->collection('my_collection')22 ->slug(data_get($validated_data, 'slug'))23 ->published()24 ->data($validated_data);25 26$entry->save();
This content originally appeared on Hussein Al Hammad and was authored by Hussein Al Hammad
Hussein Al Hammad | Sciencx (2024-01-13T00:00:00+00:00) Use Statamic Blueprint to validate Collection Entry data. Retrieved from https://www.scien.cx/2024/01/13/use-statamic-blueprint-to-validate-collection-entry-data/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.