lundi 18 avril 2022

Laravel validation error on mobile number

im trying to validate the two columns in users table with unique key

...

public function collection(Collection $rows)
{
    $rows = $rows->toArray();
    $i = 1 ;
    foreach ($rows as $key=>$row) {

        $validator = Validator::make($row, $this->rules($row), $this->validationMessages());
        if ($validator->fails()) {
            $ctr = 0;
            foreach ($validator->errors()->messages() as $messages) {
                foreach ($messages as $error) {
                    $this->errors[$i][$ctr]['row'] = $i;
                    $this->errors[$i][$ctr]['error'] = $error;
                    $ctr++;
               }
           }
        }else{
              code....
        }
        $i++;
    }
}



public function startRow(): int
{
    return 2;
}

public function getErrors()
{
    return $this->errors;
}

public function getInsertedRows()
{
   return $this->addedRows;
}

public function rules($row): array
{
 
    return [
        '0' => 'required|max:30',
        '1' =>['required','numeric','digits:10','unique:users,mobile'],
        '2' => 'required|unique:users,email|email',
    ];
}

public function validationMessages()
{
    
   return [
        '0.required' => trans('Name is required'),
        '1.required' => trans('Mobile is required'),
        '1.numeric' => trans('Mobile is required'),
        '1.digits' => trans('Minimum 10 digits for mobile number'),
        '1.unique' => trans('Duplicate Mobile Number'),
        '2.required' => trans('Email is required field'),
        '2.unique' => trans('Email is already in registerd'),
        '2.email' => trans('Invalid Email'),
    ];
}

The code executing proper in localhost server but after hosting production on server the user unique email working properly but unique mobile number not working

I tried this code alse

 Rule::unique('users', 'mobile')->where(function ($query) use($row) {
        return $query->where('mobile', '=',$row[1]);
 }),  

I user Validation rule for custom

new Mobile_rule($row)

class Mobile_rule implements Rule {

public function __construct($row)
{
   
}

public function passes($attribute, $value)
{
   $data = DB::table('users')->where('mobile', '=', $value)->first();
   return ($data)?true:false;
}

public function message()
{
    return 'Duplicate Mobile Number';
}

}

The validation not working?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire