mercredi 1 mai 2019

How to use custom validation in FormRequest?

I have class:

class CreateInvoiceRequest extends FormRequest
{

public function messages()
    {
        return [
            'name' => 'required|string',
            'code' => 'required|string|unique: enterprise_invoces,code',
        ];
    }

    public function rules()
    {
        return [
            'name' => 'required|string',
            'code' => 'required|string|unique: enterprise_invoces,code',
        ];
    }

}

Also custom validation class:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class InvoiceCode implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        //
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The invoice code sholud be in formar <number>.<number>';
    }
}

How to apply InvoiceCode rule for validator inside:

public function rules()
        {
            return [
                'name' => 'required|string',
                'code' => 'required|string|unique: enterprise_invoces,code',
            ];
        }

I tried this:

 return [
                    'name' => 'required|string',
                    'code' => 'required|string|unique: enterprise_invoces,code',
                    'code' => new InvoiceCode()
                ];



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2Ve0Z5m
via IFTTT

Aucun commentaire:

Enregistrer un commentaire