Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Validation and trim input value
  • Hello,
    I'm trying to use Validation to trim an input value and check if trimmed value is empty or not like this

    $val->add_field('name', 'Name', 'trim|required|max_length[100]');
    $val->add_field('address', 'address', 'required');

    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...
  • HarroHarro
    Accepted Answer
    No, there isn't.

    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.

    Thank you so much for the prompt reply!

  • And I understand that. ;-).

    Just trying to explain why Fuel doesn't do it.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion