Ad

Show Index Of The Image In The Validation Message In October Cms

- 1 answer

I'm building a function to upload multiple images using OctoberCMS. The validation works fine. What I want is for, when we upload multiple images, if one image doesn't meet the validation rule, the error message will come like this.

october cms validation error message

Is it possible to change progress_image.3 into 3rd image. If so then the error message has to appear like this.

The 3rd image has invalid image dimensions.

I want to show the index of the image in the message. Any suggestions? ideas?

These are my validation rules.

protected $rules = [
    'progress_date' => 'required',
    'progress_image' => 'required',
    'progress_image.*' => 'image|mimes:jpeg,jpg,png|dimensions:width=800,height=500|max:2048'
];

This is my relation.

public $attachMany = [
    'progress_image' => 'System\Models\File'
];
Ad

Answer

May be you can try Validator::replacer

Plugin.php

// ...
use Validator;
use RainLab\Pages\Controllers\Index as PageController;

class Plugin extends PluginBase
{
    public function boot() {

      Validator::replacer('required', function($message, $attribute, $rule, $parameters) {

          // if attribute match with proper signature
          if(starts_with($attribute, 'progress_image.')) {
            $attributeArr = explode('.', $attribute);
            $newMessage = strtr($message, [$attribute => " image no. $attributeArr[1] "]);
            return $newMessage;
          }

          // otherwise return message as it is
          return $message;
      });

      // ...

** I just tested it with normal messages and it worked well, but for your scenario I tried and do some parse and replace string but not able to test it on that kind of field, so may be you need to tweak it little bit.

reference : https://octobercms.com/docs/services/validation#custom-error-messages

Note : It will run for all fields with required validator.

if any doubt please comment.

Ad
source: stackoverflow.com
Ad