Classic Laravel 5+ form request validation.
Once you generated the module using Twill's CLI module generator, it will also prepare the App/Http/Requests/Twill/ModuleNameRequest.php
for you to use.
You can choose to use different rules for creation and update by implementing the following 2 functions instead of the classic rules
one:
1<?php 2 3public function rulesForCreate() 4{ 5 return []; 6} 7 8public function rulesForUpdate() 9{10 return [];11}
There is also an helper to define rules for translated fields without having to deal with each locales:
1<?php2 3$this->rulesForTranslatedFields([4 // regular rules5], [6 // translated fields rules with just the field name like regular rules7]);
There is also an helper to define validation messages for translated fields:
1<?php2 3$this->messagesForTranslatedFields([4 // regular messages5], [6 // translated fields messages7]);
Once you defined the rules in this file, the UI will show the corresponding validation error state or message next to the corresponding form field.
To validate repeater fields added to your model you can reuse the same rulesForCreate
and rulesForUpdate
methods.
If your repeater is named accordion_item
and you want to add validation to the headline
field you can use:
1public function rulesForCreate()2{3 return ['repeaters.accordion_item.*.header' => 'required'];4}
Alternatively if your field is translatable you can use the helpers as defined above:
1public function rulesForUpdate()2{3 return $this->rulesForTranslatedFields([], [4 'repeaters.accordion_item.*.header' => 'required'5 ]);6}