Welcome Guest, Not a member yet? Register   Sign In
File Validation with redirect
#1

Hi
I have a problem with my file (image) upload.
The upload itself works fine. However, an error occurs in the validation in connection with the resulting redirect, which I cannot explain to myself.
I use this redirect with all form controllers and so far it has output exactly what it should output. As an example I have attached both examples once.
I have also attached the associated private function for validation.
When I use form_open, everything is displayed as it should be displayed.
When I change to form_open_multipart the error occurs

Sorry for my bad english

When I try this the statusError is shown
PHP Code:
if(!$this->validate($this->validation_edit_profile_image())):
    return redirect()->back()
                     ->with('statusError'lang('system/auth/pages/edit_profile_image.messages.error.edit_profile_image'));
else:
    // image upload itself working fine
endif; 

When I add ->with('validation', $this->validator) the statusError and the validation is not shown

PHP Code:
if(!$this->validate($this->validation_edit_profile_image())):
    return redirect()->back()
                     ->with('statusError'lang('system/auth/pages/edit_profile_image.messages.error.edit_profile_image'))
                     ->with('validation'$this->validator);
else:
    // image upload itself working fine
endif; 
In all other controllers where a form is submitted this is working fine, for example
PHP Code:
if(!$this->validate($this->validation_edit_profile())):
    return redirect()->back()
                     ->with('statusError'lang('system/auth/pages/edit_profile.messages.error.edit_profile'))
                     ->with('validation'$this->validator)
                     ->withInput();
else:
    // profile updater
endif; 

Private Validation Function for the Image Upload
PHP Code:
private function validation_edit_profile_image()
    {
        $rules = [
 
'avatar' => [
 
'rules'  => 'uploaded[avatar],is_image[avatar]|ext_in[avatar,png,jpeg,jpg,gif]|mime_in[avatar,image/png,image/jpg,image/jpeg,image/gif]|max_size[avatar,2048]|max_dims[avatar,1024,1024]',
 
'errors' => [
 
'ext_in' => lang('system/auth/validation/clubs.edit_profile_image.profile_image.ext_in'),
 
'is_image' => lang('system/auth/validation/clubs.edit_profile_image.profile_image.is_image'),
 
'max_dims' => lang('system/auth/validation/clubs.edit_profile_image.profile_image.max_dims'),
 
'max_size' => lang('system/auth/validation/clubs.edit_profile_image.profile_image.max_size'),
 
'mime_in' => lang('system/auth/validation/clubs.edit_profile_image.profile_image.mime_in'),
 
'uploaded' => lang('system/auth/validation/clubs.edit_profile_image.profile_image.uploaded')
 ]
            ]
 ];
 return 
$rules;
    
PHP Code:
private function validation_edit_profile()
    {
        $countries_list $this->countriesModel->getCountriesValidationList();
        $languages_list $this->languagesModel->getLanguagesValidationList();
        $nationalities_list $this->countriesModel->getCountriesValidationList();
        $rules = [
 
'country' => [
 
'rules'  => 'required|in_list['.$countries_list.']',
 
'errors' => [
 
'in_list' => lang('system/auth/validation/edit_profile.country.in_list'),
 
'required' => lang('system/auth/validation/edit_profile.country.required')
 ]
            ],
 
'firstname' => [
 
'rules'  => 'required|min_length[3]|max_length[255]',
 
'errors' => [
 
'max_length' => lang('system/auth/validation/edit_profile.firstname.max_length'),
 
'min_length' => lang('system/auth/validation/edit_profile.firstname.min_length'),
 
'required' => lang('system/auth/validation/edit_profile.firstname.required')
 ]
            ],
            'language' => [
                'rules'  => 'required|in_list['.$languages_list.']',
                'errors' => [
                    'in_list' => lang('system/auth/validation/edit_profile.language.in_list'),
                    'required' => lang('system/auth/validation/edit_profile.language.required')
                ]
            ],
            'lastname' => [
 
'rules'  => 'permit_empty|min_length[3]|max_length[255]',
 
'errors' => [
 
'max_length' => lang('system/auth/validation/edit_profile.lastname.max_length'),
 
'min_length' => lang('system/auth/validation/edit_profile.lastname.min_length')
 ]
 ],
            'location' => [
 
'rules'  => 'permit_empty|min_length[3]|max_length[255]',
 
'errors' => [
 
'max_length' => lang('system/auth/validation/edit_profile.location.max_length'),
 
'min_length' => lang('system/auth/validation/edit_profile.location.min_length')
 ]
            ],
            'nationality' => [
                'rules'  => 'required|in_list['.$nationalities_list.']',
                'errors' => [
                    'in_list' => lang('system/auth/validation/edit_profile.nationality.in_list'),
                    'required' => lang('system/auth/validation/edit_profile.nationality.required')
                ]
            ]
 ];
 return 
$rules;
    
Reply
#2

(This post was last modified: 11-03-2021, 12:40 AM by manager.)

(11-02-2021, 07:54 AM)pyst4r1983 Wrote: When I add ->with('validation', $this->validator) the statusError and the validation is not shown
PHP Code:
if(!$this->validate($this->validation_edit_profile_image())):
    return redirect()->back()
                     ->with('statusError'lang('system/auth/pages/edit_profile_image.messages.error.edit_profile_image'))
                     ->with('validation'$this->validator);
else:
    // image upload itself working fine
endif; 

The problem is when you return redirect()->back()->with('validation', $this->validator) second parameter should be string or an array.
In you code you're setting as a parameter validation class object.
Your code should be like
PHP Code:
redirect()->back()->with('validation'$this->validator->getErrors()) 

It sets validation errors as flash data in session.
Reply
#3

Good morning

I tried your code but i got still errors

This is my view code, I want to display the errors per each form field
This is working in EVERY other Controller/View Combination as posted below
Only the file upload will not work like this

PHP Code:
<?= form_label(lang('system/auth/pages/edit_profile_image.form.label.profile_image'), 'profile_image', ['class' => 'form-label']); ?>
<?= form_upload
('avatar'set_value('avatar'$user->profile_image), ['autocomplete' => 'off''class' => 'form-control'.$viewData['bs_form_control_size'].((session()->has('validation') && session('validation')->hasError('avatar'))?' is-invalid':'').((session()->has('validation') && !session('validation')->hasError('avatar'))?' is-valid':''), 'id' => 'profile_image']); ?>
<?= 
(session()->has('validation') && session('validation')->getError('avatar'))?'<div class="form-text text-danger fw-bold">'.session('validation')->getError('avatar').'</div>':''?>

Why can i give class to session in other controllers and here its not workin?

Greetz
pyst4r1983
Reply




Theme © iAndrew 2016 - Forum software by © MyBB