You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to add some custom validation to a specific neo block type attribute. I am using the code below in my validator (based on https://nystudio107.com/blog/custom-matrix-block-validation-rules). I tried several things and it does show a general error, however I would like the block and the specific field to show the error as well (same as with required fields for example). Any pointers on how to achieve this?
public function validateAttribute($model, $attribute)
{
$builderQuery = $model->$attribute;
// Iterate through all of the blocks
$blocks = $builderQuery->getCachedResult() ?? $builderQuery->limit(null)->anyStatus()->all();
$hasError = false;
foreach ($blocks as $block) {
switch ($block->type) {
case 'articleGroup':
if ($block->groupType->value === 'video-preview' && $block->articleGroupCollectionSource->value === 'priority') {
$hasError = true;
$block->addError('groupType', 'Article group type "video-preview" requires collection type "collection"');
Craft::$app->getElements()->saveElement($block);
}
break;
}
}
$builderQuery->setCachedResult($blocks);
if ($hasError) {
$model->addError($attribute, 'One or more blocks have errors');
}
}
It does get in the if statement, so that works ($hasError is true). It also shows the "One or more blocks have errors", but it just does not show in the block itself. Since we have pages with 40-60 blocks on there, just showing a error on the top and bottom is not really what we want. Is there any way to get the $block->addError part also working so it will show an error in the specific block as well?
The text was updated successfully, but these errors were encountered:
What question would you like to ask?
Hi,
I am trying to add some custom validation to a specific neo block type attribute. I am using the code below in my validator (based on https://nystudio107.com/blog/custom-matrix-block-validation-rules). I tried several things and it does show a general error, however I would like the block and the specific field to show the error as well (same as with required fields for example). Any pointers on how to achieve this?
It does get in the if statement, so that works ($hasError is true). It also shows the "One or more blocks have errors", but it just does not show in the block itself. Since we have pages with 40-60 blocks on there, just showing a error on the top and bottom is not really what we want. Is there any way to get the $block->addError part also working so it will show an error in the specific block as well?
The text was updated successfully, but these errors were encountered: