Two options :
- you can sanitize the values before validation : override the all() method of the form request :
public function all()
{
$this->merge([
'sanitized_mobile_no' => $this->sanitizeMobileNo($this->mobile_no),
// etc
]);
return parent::all();
}
Then you validate the field sanitized_mobile_no and use it in your controller. I don't erase the raw field name because I want to display to the user what he typed when validation fails and he gets back to the form.
- you can use model mutators to sanitize values before saving to the db :
class MyModel extends Model
{
public function setMobileNoAttribute ($mobile_no)
{
$this->attribute['mobile_no'] = $this->sanitizeMobileNo($mobile_no);
}
}
When saving mobile_no column, it will go through this logic and change the mobile_no value.