and after $val->run() I want to return trimmed string to error page by using $val->validated()
In case input value is " namexxx " it returns "namexxx" -> OK But if input value is " " (whitespace only), "required" rules fail then it returns " " (not trimmed). Is there anyway to return an empty string? Any help would be much appreciated...
It's Fuel's architecture to NEVER modify input in any way. Validation returns validated input that you can fetch using the validated() method, but that only includes fields that validate, and yours doesn't.
Since you have to pass the input back to the view in your controller for your form to be reloaded, you can do this in your controller:
// prepare the raw input for redisplay if ($data = \Input::post()) { foreach ($data as $key => $value) { $data[$key] = trim($value); } }
// show the view return \View::forge('myview', $data);
I suggest not to add the result of validated() to it, in case you have other rules that modify the input. It would appear strange to the user if the form reloads with data that the user hasn't put in. I would even go sofar as to not do this at all, if the user entered only whitespace, there is no harm in reloading the form with that.
Hi WanWizard, I understand that it's Fuel's architecture to not alter user input values, but it's project requirement to do that ;-) I will try to trim raw Input before $val->run($data) (then it's no need to add rule "trim" to validation) and passing $data into view as your suggestion.