Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Overwrite post value after form sending but before validation
  • Hi everyone, i have a dropdown/select field populated with entries from my database. So far no problem. However, i want to add an additional entry showing the typical initial hint like "- Please select customer -". Ive done this in my controller:
    $customers = Arr::assoc_to_keyval(Model_Customer::find('all'), 'id', 'name');
    $customers[0] = '- Please select customer -';
    asort($customers);
      
    $this->template->set_global('customers', $customers);
    
    This way i can easily bind the $customers to my dropdown field in the form. Works still great. Now comes the problem: When i submit the form, i want to check if the field is zero, which means "not set" as seen above, and then unset the field so the validation triggers because the field is required. Ive done this in the create action of my controller...
    if(Input::post('customer_id') == 0) unset($_POST['customer_id']);
       
    $val = Model_Event::validate('create');
    
    if ($val->run()) 
    {
    ...
    
    After the unset function i get a NULL value when i do var_dump(Input::post('customer_id' )) , however the validation dont triggers for this field. Ive searched a while and found out, that the validation uses Input::param() instead of Input::post() to get the field values. So i tried to dump this also right after the unset command. But the strange thing is, that var_dump(Input::param('customer_id' )) gives me a string "0" instead a correct NULL. I guess this is a bug, as param() should show the same values as post() ?? Any idea for a workaround? Thanks
    Daniel
  • The Input class only retrieves input parameters (either $_POST, $_GET or php://input) once, processes them, and then stores them into a class property. Every time you call Input::post(), param(), or one of the other methods, this property is used, and not the original global arrays. Use of those is discouraged. You can retrieve all data in the controller, do your thing, and pass the rest on to validation:
    // get the post data
    $post = \Input::post();
    
    // erase the customer_id if it's value is equal 0
    \Input::post('customer_id') === 0 and unset($post['customer_id']);
    
    if ($val->run($post))
    {
        // validated !
    }
    

Howdy, Stranger!

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

In this Discussion