• [deleted]

Hi All,

I used this section of code previously in extend.php meaning I could have a longer post title etc

  // Register extenders here
  new Extend\Compat(function(Dispatcher $events) {
    $events->listen(Validating::class, function(Validating $event) {
      if ($event->type instanceof DiscussionValidator) {
        $rules = $event->validator->getRules();
        
        $rules['title'] = array_map(function(string $rule) {
          if (Str::startsWith($rule, 'max:')) {
            return 'max:180';
          }
          
          return $rule;
        }, $rules['title']);

        $event->validator->setRules($rules);
      }
    });
  }),

This no longer seems to work in Flarum 1.x, and throws an error. Is there any replacement ? It seems extend\compat is where things fall over with the below

PHP Fatal error:  Uncaught Error: Class 'Flarum\Extend\Compat' not found in /home/metabullet/public_html/extend.php:28
Stack trace:
#0 /home/metabullet/public_html/vendor/flarum/core/src/Foundation/Site.php(65): require()
#1 /home/metabullet/public_html/vendor/flarum/core/src/Foundation/Site.php(38): Flarum\Foundation\Site::loadExtenders()
#2 /home/metabullet/public_html/site.php(47): Flarum\Foundation\Site::fromPaths()
#3 /home/metabullet/public_html/flarum(11): require('/home/metabulle...')
#4 {main}
  thrown in /home/metabullet/public_html/extend.php on line 28
  • clarkwinkelmann replied to this.
  • [deleted] from https://discuss.flarum.org/d/26815-minimum-password-length/2

    Untested, but it should be something like this:

    <?php
    
    use Flarum\Extend;
    use Flarum\Discussion\DiscussionValidator;
    use Illuminate\Support\Str;
    
    return [
      // Register extenders here
      (new Extend\Validator(DiscussionValidator::class))
        ->configure(function ($flarumValidator, $validator) {
            $rules = $validator->getRules();
    
            if (!array_key_exists('title', $rules)) {
                return;
            }
    
            $rules['title'] = array_map(function(string $rule) {
              if (Str::startsWith($rule, 'max:')) {
                return 'max:180';
              }
              
              return $rule;
            }, $rules['title']);
    
            $validator->setRules($rules);
        }),
    ];

    I'm not actually sure if the "key exists" check is needed. It might not be with just Flarum core extensions but if another extension adds more attributes to the validators there could be situations where title isn't part of the tested attributes.

    [deleted] from https://discuss.flarum.org/d/26815-minimum-password-length/2

    Untested, but it should be something like this:

    <?php
    
    use Flarum\Extend;
    use Flarum\Discussion\DiscussionValidator;
    use Illuminate\Support\Str;
    
    return [
      // Register extenders here
      (new Extend\Validator(DiscussionValidator::class))
        ->configure(function ($flarumValidator, $validator) {
            $rules = $validator->getRules();
    
            if (!array_key_exists('title', $rules)) {
                return;
            }
    
            $rules['title'] = array_map(function(string $rule) {
              if (Str::startsWith($rule, 'max:')) {
                return 'max:180';
              }
              
              return $rule;
            }, $rules['title']);
    
            $validator->setRules($rules);
        }),
    ];

    I'm not actually sure if the "key exists" check is needed. It might not be with just Flarum core extensions but if another extension adds more attributes to the validators there could be situations where title isn't part of the tested attributes.

      Just in case you also want to extend the number of characters in a discussion, for that you need this code:

      (new Extend\Validator(PostValidator::class))
              ->configure(function ($flarumValidator, $validator) {
                $rules = $validator->getRules();
                $rules['content'] = array_map(function(string $rule) {
                  if (Str::startsWith($rule, 'max:')) {
                    return 'max:200000';
                  }
                  
                  return $rule;
                }, $rules['content']);
      
                $validator->setRules($rules);
            })

      Always make sure your database has the correct column sizes to accept the new content length, or your users might run into 500 errors.

      The Unicode characters can take a lot of bytes, so it's best to have a bit of margin.