1

I have a validation rule as shown below. I am using exists to ensure that the contract is unique. The issue now is that contract numbers are stored with spaces in the database so this validation is not working for those cases (for example it will say the contract number does not exist which is due to the space before the number). To solve this, I want to do trim(contract_number). Please how can I apply the trim function to the contract_number below?

public function rules()
{
    return [   
        'tel' => 'required|max:25',
        'contract' => 'required|digits:9|exists:accounts,contract_number',
        'nom' => 'required|max:255',
        'language' => 'required|max:2',
        'g-recaptcha-response' => 'required|captcha',
    ];
}
Fokwa Best
  • 3,322
  • 6
  • 36
  • 51
  • 1
    Will not help much, but why are the contract numbers stored (in database) with a space before them? Any good reason? – brombeer Jul 21 '20 at 10:07
  • you can find your answer here https://stackoverflow.com/questions/29386203/laravel-5-validation-trim – Mubin Jul 21 '20 at 10:08
  • @Mubin Laravel uses the `TrimStrings` middleware, so all inputs should already be trimmed. The "problem" is that data _in the database_ has a string before them – brombeer Jul 21 '20 at 10:10
  • Hi @kerbh0lz, I think the info in that field where imported from an excel file hence the spaces in some cases – Fokwa Best Jul 21 '20 at 10:10
  • 1
    Instead of dealing with custom validators (and possibly other custom parts of code) that deal with those spaces personally I would just get rid of the spaces in the database – brombeer Jul 21 '20 at 10:13
  • Hi @kerbh0lz, the database has more than 2 million records. We have asked the DBA to remove the spaces in the database but he recommended that we should trim at the application level. – Fokwa Best Jul 21 '20 at 10:17
  • Is your DBA the one who wrongly imported the data in the first place? ;) Was just a suggestion, I'd rather have clean data and _never ever_ have to deal with that problem again. – brombeer Jul 21 '20 at 10:22
  • I think is another team that imported the data wrongly – Fokwa Best Jul 21 '20 at 10:25
  • @FokwaBest Does I answer your question? You can check my answer's in the list – Basheer Kharoti Jul 21 '20 at 16:01
  • Hi @BasheerKharoti, Thanks for your answer but how do I add the trim function to the contract number coming from the database using your approach? You know the contract number are stored in the database with spaces so when the user enter the contract number in the form and click validate, it says it doesn't exist due to the space. I know trimming before validating will solve the problem. – Fokwa Best Aug 05 '20 at 16:12
  • Something like `'contract' => 'required|digits:9|exists:accounts,trim(contract_number)'` – Fokwa Best Aug 05 '20 at 16:14
  • @FokwaBest Laravel does trimming out of the box. See `app\Http\Kernel.php` – Basheer Kharoti Aug 06 '20 at 04:42
  • Hi @BasheerKharoti, the contract number is not coming from the form, It is coming from the database. Does laravel also trim info coming from DB out of the box? The issue I am facing is this, we have a form with contract number field, when user enter his contract number and click "Check", some are not found because they are stored with spaces before them in the database. – Fokwa Best Aug 06 '20 at 12:48
  • @FokwaBest see my updated answer – Basheer Kharoti Aug 07 '20 at 05:38
  • Hi @BasheerKharoti, Thanks alot. Your answer works – Fokwa Best Aug 10 '20 at 17:56

1 Answers1

1

From the Laravel docs, you can use withValidator method on Custom Form Request to add any further validations on the request. Remove the rule exists:accounts,trim(contract_number) from the list, and try custom rule with after hook.

/**
* Configure the validator instance.
 *
 * @param  \Illuminate\Validation\Validator  $validator
 * @return void
 */
public function withValidator($validator)
{
    $validator->after(function ($validator) {
        if (!\DB::table('accounts')->whereRaw('TRIM(contract_number) = ?', [request('contract_number')])->exists()) {
            $validator->errors()->add('contract_number', 'Contract number does not exists');
        }
    });
}
Basheer Kharoti
  • 4,202
  • 5
  • 24
  • 50