sanuglia's avatar

Form Request validation; withValidator() to check if any rules() have already failed?

In Laravel 5.4, it appears that the withValidator() method gets executed even if the validation rules fail. Example:

class ClientAttachBusinessRequest extends FormRequest
{
    public function rules()
    {
        return [
            'businessName' => 'required',
            'fromDate'     => 'sometimes|nullable|date',
            'toDate'       => 'sometimes|nullable|date|greater_than_field:fromDate',
        ];
    }

    
    /**
     * Configure the validator instance.
     *
     * @param  \Illuminate\Validation\Validator $validator
     *
     * @return void
     */
    public function withValidator( $validator )
    {
        
        $validator->after(

            function ( $validator ) {
                
                // Extract the business id from the string.
                $businessString = $this->request->get( 'businessName' );  // Start with "Murazik-Swaniawski (ID: 30)"
                $businessString      = explode( "ID:", $businessString ); // Now have "30)"
                $businessId     = rtrim( $businessString[ 1 ], ")" ); // Now have "30"
                
                // Find the business Record 
                $this->business = Business::where( 'id', $businessId )->get()->first();
                
                if ( $this->business ) {
                    
                    $businessName = $this->business->businessName;
                    
                    $client = Client::find( $this->client_id );
                    
                    if ( $client->businesses->contains( $this->business->id ) ) {
                        
                        $validator->errors()->add(
                            'businessName',
                            "The business: <strong>$businessName</strong> is already attached to this Client (See above ⬆ ). Please select another." )
                        ;
                    }
                }
                $validator->errors()->add( 'businessName', 'Invalid business.' );
            }
        );
    }

}

The documentation indicates that the withValidator() method is used as an "AFTER hook to a form request".

Now, if the businessName submitted is empty (""), then withValidator() will fail with this code because it can't extract the businessId from the submitted string.

Sure, I could check for a valid businessName string before I do that, but I'm curious to know how I could check for any validation rules that have already failed upon entering withValidator()?

0 likes
2 replies
sanuglia's avatar

Ah, never mind. I found this nifty little method:

    public function withValidator( $validator )
    {
    
        if ( $validator->fails() ) {
            // Handle errors
        }
        ...
    }

...that I can use at the beginning of the withValidator() method.

hamedghaderi's avatar

Too late but this is the other way you can do it:

public function withValidator(Validator $validator) 
{
		$validator->after(function (Validator $validator) {
                $validatedData = $validator->safe()->all();
                ...
         });
 }

Please or to participate in this conversation.