CodeIgniter Forums
Do you know withInput() and Validation errors? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=31)
+--- Thread: Do you know withInput() and Validation errors? (/showthread.php?tid=82735)



Do you know withInput() and Validation errors? - kenjis - 08-16-2022

redirect()->withInput() and Validation errors have an undocumented behavior.

If you redirect with withInput():
PHP Code:
if (! $this->validate($rules)) {
    return redirect()->back()->withInput();

https://github.com/kenjis/ci4-validation-tutorial/blob/009ef2d8d5411b2b34ef70ccaeb411c30fbe1b2d/app/Controllers/Form.php#L53-L55

You can use the Validation object with the validation errors in the previous page.
PHP Code:
<?= service('Validation')->listErrors() ?>
https://github.com/kenjis/ci4-validation-tutorial/blob/2836228014b6b2b2baf6594699cf6094e90d9fc6/app/Views/form2.php#L7

But this feature has bugs or design flaw.
So I would like to remove the feature, and add a new helper function to get the validation errors.

Any opinion?


RE: Do you know withInput() and Validation errors? - luckmoshy - 08-16-2022

Yes even I was been asking myself such a thing but I neglected to pursue a query !!! ???


RE: Do you know withInput() and Validation errors? - InsiteFX - 08-17-2022

Sounds great to me.


RE: Do you know withInput() and Validation errors? - kenjis - 08-17-2022

@luckmoshy What do you mean?
Have you ever used the feature?


RE: Do you know withInput() and Validation errors? - luckmoshy - 08-17-2022

@kenjis of cause at earlier from CI 4.0 I didn't know what to do with redirect withinput back and get a specific error because of  CI documentation The global return
PHP Code:
redirect()->back()->withInput(); 
  was not shown any specific errors result method or how to get a specific error or succeeded reply from failed action query. So when you call this action, I totally agree with you that we should have errors resulting from the helper function than using a 
PHP Code:
<?= service('Validation')->listErrors() ?>
(validation) service in view or  Angel look we see we can get an errors to list ok and how about succeded result ?? or  Rolleyes is it formed for only errors result???!!!

@kenjis  I truly have been using this  global CI
PHP Code:
redirect()->back()->withInput(); 
  with additional fourth session methods like 
PHP Code:
redirect()->back()->withInput()->with('danger/*success*/',/*In modal*/$this->modal->errors() /*or something else apart with modal */); 
and then
PHP Code:
session()->get('danger/*success*/'); / 
in a view

but I may have a view of the point that it could be good if you design a new helper function and may have a specific single error validation result perimeter or with multiple validations on its parameter something like that or  this
PHP Code:
redirect()->back()->withInput(); 

in  additionally, Why this
PHP Code:
redirect()->back()->withInput(); 
if you have an app with a bulk of routes with
Code:
routes priority
it redirects back to an unintended route  so it's forcedly me to use
PHP Code:
redirect()->to(current_url())->withInput(); 



RE: Do you know withInput() and Validation errors? - kenjis - 08-31-2022

The undocumented redirect()->withInput() and Validation errors behavior was treated as a bug.
It won't work, so do not use it.

Since v4.3, you can use Form helper validation_list_errors() (and validation_errors(), validation_show_error()).
See https://github.com/kenjis/ci4-validation-tutorial/blob/21e3c0811eb32540da8f706a9312641dd3e42e73/app/Views/form2.php#L7


RE: Do you know withInput() and Validation errors? - kenjis - 10-16-2022

Added explanation to README.
https://github.com/kenjis/ci4-validation-tutorial#readme


RE: Do you know withInput() and Validation errors? - InsiteFX - 10-17-2022

Thank you @kenjis