vincent15000's avatar

#[Validate()] rules not working

Hello,

I have a very strange behavior with some rules.

#[Validate('required', message: 'Le nombre de portions est obligatoire.', onUpdate: false)]
#[Validate('integer', message: 'Le nombre de portions doit être un nombre entier.', onUpdate: false)]
#[Validate('min:1', message: 'Le nombre de portions doit être supérieur ou égal à 1.', onUpdate: false)]
#[Validate('max:255', message: 'Le nombre de portions doit être inférieur ou égal à 255.', onUpdate: false)]
public $portions = null;

According to me, these rules should work, but the problem is that they don't work.

  • required : ok

  • integer : ok

  • min:1 : not working

  • max:255 : not working

When I type a negative value or a value greater than 255, the validation is successful whereas it should generate an error.

Do you have any idea why it doesn't work ?

Thanks a lot.

V

0 likes
4 replies
vincent15000's avatar

@jlrdw I have already tried this and it doesn't work.

#[Validate([
    'portions' => ['required|integer|min:1|max:255']
], message: [
    'required' => 'Le nombre de portions est obligatoire.',
    'integer' => 'Le nombre de portions doit être un nombre entier.',
    'min' => 'Le nombre de portions doit être supérieur ou égal à 1.',
    'max' => 'Le nombre de portions doit être inférieur ou égal à 255.'
], onUpdate: false)]
public $portions = null;
vincent15000's avatar

I have the solution.

Livewire dehydrates and hydrates the properties. For a string it hydrates a Stringable object instead of a string, so the min and max can't be applied.

To solve this problem, I have created custom rules (here only the MinValue custom rule, but I have also created a MaxValue one).

class MinValue implements ValidationRule
{
    public $value;
    public $message;

    public function __construct($value, $message)
    {
        $this->value = $value;
        $this->message = Str::of($message)->replace('{value}', $this->value);
    }

    /**
     * Run the validation rule.
     *
     * @param  \Closure(string): \Illuminate\Translation\PotentiallyTranslatedString  $fail
     */
    public function validate(string $attribute, mixed $value, Closure $fail): void
    {
        if (intval($value->__toString()) < $this->value) {
            $fail($this->message);
        }
    }
}

And then I apply these rules on the validation.

#[Validate([
    'portions' => [
        'required',
        'integer',
        new MinValue(1, 'Le nombre de portions doit être supérieur ou égal à {value}.'),
        new MaxValue(255, 'Le nombre de portions doit être inférieur ou égal à {value}.'),
    ]
], message: [
    'required' => 'Le nombre de portions est obligatoire.',
    'integer' => 'Le nombre de portions doit être un nombre entier.',
], onUpdate: false)]
public $portions = null;
vincent15000's avatar
vincent15000
OP
Best Answer
Level 63

I have searched why the rules I presented in the post didn't work.

In fact they work fine, but only if I don't filter the fields. In the form, before validation, I apply a filter to remove all unnecessary spaces and return null if the string is null. The problem is that I filtered also the numeric values, but it wasn't a good idea.

Now I don't filter the numeric values any more and these filters work fine.

#[Validate('required', message: 'Le nombre de portions est obligatoire.', onUpdate: false)]
#[Validate('integer', message: 'Le nombre de portions doit être un nombre entier.', onUpdate: false)]
#[Validate('min:1', message: 'Le nombre de portions doit être supérieur ou égal à 1.', onUpdate: false)]
#[Validate('max:255', message: 'Le nombre de portions doit être inférieur ou égal à 255.', onUpdate: false)]
public $portions = null;

Please or to participate in this conversation.