256 Kilobytes

Answers in Frameworks | By August R. Garcia

Published 3 months agoTue, 03 Dec 2019 17:53:01 -0800

167 views, 0 RAMs, and 0 comments

Tags: HTML, Laravel, Validation

Profile Photo - August R. Garcia August R. Garcia LARPing as a Sysadmi... Portland, OR
🗎 213 🗨 1114 🐏 336
Site Owner

E.g., a validation rule where "<p>This is a&nbsp;post</p>" is counted as 14 characters instead of 26 characters.

Anyway, here's the code. Also see the Laravel documentation on creating custom validation rules for more information.

1. Create the File for the MaxLengthIgnoreHtml Validation Rule

php artisan make:rule MaxLengthIgnoreHtml

2. Create the MaxLengthIgnoreHtml Validation Rule

Edit the file that was created at /App/Rules/MaxLengthIgnoreHtml.php.


namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class MaxLengthIgnoreHtml implements Rule
        public function __construct($max_length) {
                $this->max_length = $max_length;

         * Determine if the validation rule passes.
         * @param  string  $attribute
         * @param  mixed  $value
         * @return bool
        public function passes($attribute, $value) {
                // This will remove HTML tags and convert characters like &nbsp; to their single-character entities. 
                // Then, if the resulting string is longer than the maximum allowed length, the test fails; otherwise, it succeeds
                return (  strlen(html_entity_decode(strip_tags($value))) <= $this->max_length  ) ? TRUE : FALSE;     

         * Get the validation error message.
         * @return string
        public function message() {
            return 'The field :attribute must be under ' . htmlspecialchars(number_format($this->max_length)) . ' characters.';

3. Use the Custom Validation Rule

// In some controller, probably 
$this->validate($request, [
  "some_field" => ['nullable', 'string', new \App\Rules\MaxLengthIgnoreHtml($max_char_count)],
Download more RAM. 🐏 ⨉ 0 Posted by August R. Garcia 3 months ago 🕓 Posted at 03 December, 2019 17:53 PM PST

Sir, I can do you a nice SEO.

Post a New Comment

To leave a comment, login to your account or create an account.

Do you like having a good time?

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account
You can also login to an existing account or recover your password. All use of this site is subject to terms outlined in the terms of service and privacy policy.