0
votes

In laravel, I have created a form. At the moment, I am working on the validation of the input fields of this form. I ran into a problem when I tried to validate some input fields and others not. For example, mail should be validated but catering_name not (it isn't necessary to fill in this field, its an option)

I have tried all validation methods I could find. I keep getting the same error.

Method Illuminate\Validation\Validator::validatePhone does not exist.

I guess I am missing something.

I have tried:

  • Validator::make($request->...
  • $this->validate(request(), [ ...
  • $request->validate([ ...

Bellow, you will find all the data that should be inputted in the database. If I remove the validation part, the data got inserted into the database. I think the problem lays with how I try to validate. Thanks for any help.

 $this->validate(request(), [
        'add_name'              => 'required|min:3',
        'add_mail'              => 'required|email', 
        'name'                  => 'required|min:3',
        'email'                 => 'required|email', 
        'telefone'              => 'numeric|phone', 
        'gsm'                   => 'numeric|phone', 
        'event'                 => 'required|min:3',
        'date_start'            => 'required|date|after:tomorrow',
        'date_end'              => 'required|date|after_or_equal:event_date_start',
        'location'              => 'required|min:3',
        'number'                => 'required',
    ]);

    $event = new Event;

    $event->add_name     = request('add_name');
    $event->add_mail     = request('add_mail');
    $event->name       = request('name');
    $event->email      = request('email');
    $event->telefone   = request('telefone');
    $event->gsm        = request('gsm');
    $event->name         = request('name');
    $event->date_start   = request('date_start');
    $event->date_end     = request('date_end');
    $event->location           = request('location');
    $event->number       = request('number');
    $event->catering           = request('catering');
    $event->catering_name      = request('catering_name');
    $event->remarks            = request('remarks');
    $event->status             = Event::STATUS_0;

    $event->save();
1
Take a look at the validation rules available for Laravel 5.8: laravel.com/docs/5.8/validation#available-validation-rules There isn't a phone rule (which is what your error validatePhone does not exist. is saying). You're going to need to add one of your own: laravel.com/docs/5.8/validation#custom-validation-rules. Try that out first, and if you have a specific issue, update your question. - Tim Lewis
Thanks a lot. I couldn't find the problem and it was just the phone. - ageans
No problem. Just a side note, unless you have an application that requires them to be in a certain format, like E.164 (which is +1{number}) for automated calling/texting, forcing a certain format might be unnecessary. But again, depends on the usage. - Tim Lewis

1 Answers

1
votes

Unfortunately phone is not one of the default validation. You can try something like:

[
    'telefone' => 'required|regex:/(01)[0-9]{9}/',
]

You can see the available list of validations given by Laravel here. There are a wide variety of more complex options depending on how important it is to you.

  • There are packages for easy plug and play like Laravel-Phone.
  • You can create your own custom validation using php artisan make:rule phone_number and then editing the new rule made:

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class PhoneNumber implements Rule
{
    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        // logic here, most likely some sort of regex.
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return 'The :attribute must be a valid phone number.';
    }
}