Home > Blockchain >  Laravel form request: Change data after validation
Laravel form request: Change data after validation

Time:09-15

How do I change data after validation?

The below code will not result in phone having spaces removed in $request->validated() but it is in $request.

My form request:

class StoreUserRequest extends FormRequest
{
    public function rules()
    {
        return [
            'phone' => ['required', 'string']
        ];
    }

    public function passedValidation()
    {
        $this->merge(['phone' => str_replace(' ', '', $this->get('phone'))]);
    }
}

My controller:

$validated = $request->validated();
$user = Customer::create($validated);

Should I use https://laravel.com/docs/8.x/validation#adding-after-hooks-to-form-requests instead and Joe would I use it?

CodePudding user response:

You are not required to use the validated data and you can still access and use the original request data after the validation and do any data manipulation you like,

the $validated data only has the fields you pass into, so depending on the situation, you may or may not want to use it.

you have different ways to do it

public function storeRequest(Request $request) {

    // validation return 422 on failure
    $validated = $request->validate( [
        'first_name' => 'required|max:255',
        'last_name' => 'required|max:255',
        'email' => 'required|email:filter|max:255|unique:users',
        'phone' => 'required',
        'some_data' => 'required|min:6',
    ]);

    // Validation fine, can still access the original request
    $request->merge(['phone' => str_replace(' ', '', $request->input('phone'))]);

    // use original request and remove some data
    $user = Customer::create( $request->except('some_data') );

    //or pick some data from original request
    $user = Customer::create( $request->only('first_name', 'last_name', 'email', 'phone') );

    // or use the validated data and update the phone value
    $validated['phone'] = str_replace(' ', '', $validated['phone'] ); // or str_replace(' ', '', $request->input('phone') )
    $user = Customer::create( $validated );

} 

CodePudding user response:

You could create a custom cast.

The get method is responsible for transforming a raw value from the database into a cast value, while the set method should transform a cast value into a raw value that can be stored in the database.

<?php
 
namespace App\Casts;
 
use Illuminate\Contracts\Database\Eloquent\CastsAttributes;
 
class Phone implements CastsAttributes
{
    public function get($model, $key, $value, $attributes)
    {
        return $value;
    }
 
    public function set($model, $key, $value, $attributes)
    {
        return str_replace(' ', '', $value);
    }
}

And in your model

<?php
 
namespace App\Models;
 
use App\Casts\Phone;
use Illuminate\Database\Eloquent\Model;
 
class User extends Model
{
    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'phone' => Phone::class,
    ];
}

More info here: https://laravel.com/docs/9.x/eloquent-mutators

  • Related