Use Statamic Blueprint to validate Collection Entry data

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 …


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 data
10];
11 
12// validate the data
13$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 data
20$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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Use Statamic Blueprint to validate Collection Entry data." Hussein Al Hammad | Sciencx - Saturday January 13, 2024, https://www.scien.cx/2024/01/13/use-statamic-blueprint-to-validate-collection-entry-data/
HARVARD
Hussein Al Hammad | Sciencx Saturday January 13, 2024 » Use Statamic Blueprint to validate Collection Entry data., viewed ,<https://www.scien.cx/2024/01/13/use-statamic-blueprint-to-validate-collection-entry-data/>
VANCOUVER
Hussein Al Hammad | Sciencx - » Use Statamic Blueprint to validate Collection Entry data. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/01/13/use-statamic-blueprint-to-validate-collection-entry-data/
CHICAGO
" » Use Statamic Blueprint to validate Collection Entry data." Hussein Al Hammad | Sciencx - Accessed . https://www.scien.cx/2024/01/13/use-statamic-blueprint-to-validate-collection-entry-data/
IEEE
" » Use Statamic Blueprint to validate Collection Entry data." Hussein Al Hammad | Sciencx [Online]. Available: https://www.scien.cx/2024/01/13/use-statamic-blueprint-to-validate-collection-entry-data/. [Accessed: ]
rf:citation
» Use Statamic Blueprint to validate Collection Entry data | Hussein Al Hammad | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.