Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Weird recursion with Validation
  • Hi! I'm having a weird problem with the Validation. My errors array explodes in size due to recursion and I can't for the life of me figure out why. I've been staring at the code for about one hour now and I know it's probably a silly mistake but I can't find it. Here's some code: In controller function action_add_offer() (currently printing out the error array and then returning):
    if (Input::method() == 'POST')
      {
        $val = Validation::factory('offer');
        $val->add('offer_value', 'Value')->add_rule('required');
       
        if($val->run())
        {
          Session::set_flash('notice', 'passed...');
        }
        else
        {
          echo 'Size '.count($val->errors());
          echo '<pre;>';
          print_r($val->errors());
          echo '</pre>';
          return;
          Session::set_flash('notice', $val->errors());
        }
    

    and this is the output from print_r($val->errors()) when validation fails:
    Array
    (
        [offer_value] => Fuel\Core\Validation_Error Object
            (
                [field] => Fuel\Core\Fieldset_Field Object
                    (
                        [fieldset:protected] => Fuel\Core\Fieldset Object
                            (
                                [name:protected] => offer
                                [fields:protected] => Array
                                    (
                                        [offer_value] => Fuel\Core\Fieldset_Field Object
     *RECURSION*
                                    )
    
                                [validation:protected] => Fuel\Core\Validation Object
                                    (
                                        [fieldset:protected] => Fuel\Core\Fieldset Object
     *RECURSION*
                                        [input:protected] => Array
                                            (
                                                [offer_value] => 
                                            )
    
                                        [validated:protected] => Array
                                            (
                                            )
    
                                        [errors:protected] => Array
     *RECURSION*
                                        [callables:protected] => Array
    
    
    
    ...... a couple of hundred more lines like the above.....
    
    
    
    
                [previous:Exception:private] => 
                [xdebug_message] => ( ! ) Fuel\Core\Validation_Error:  in C:\wamp\www\dev\fuellogin\fuel\core\classes\validation.php on line 309
    Call Stack
    #TimeMemoryFunctionLocation
    10.0006395368{main}(  )..\index.php:0
    20.01681515552Fuel\Core\Request->execute(  )..\index.php:42
    30.04593807784call_user_func_array
    (  )..\request.php:429
    40.04593807968Controller_Companies->action_add_offer(  )..\request.php:0
    50.05754978048Fuel\Core\Validation->run(  )..\companies.php:151
    60.05764978944Fuel\Core\Validation->_run_rule(  )..\validation.php:277
    
    In the base view:
    <?=render('companies/_offer_form', array('company_id' => $company->id));?>
    

    and _offer_form.php:
    <h3>Offer</h3>
    <div>
    <?=Form::open('companies/add_offer/'.$company_id);?>
    <p>
     <?=Form::label('Probability', 'offer_probability');?>
     Low<?=Form::radio('offer_probability', 'Low', array('checked'));?>
      Medium<?=Form::radio('offer_probability', 'Medium');?>
      High<?=Form::radio('offer_probability', 'High');?>
    </p>
    <p>
     <?=Form::label('Value', 'offer_value');?>
      <?=Form::input('offer_value', Input::post('offer_value', ''), array('autocomplete' => 'off'));?>
    </p>
    <p>
     <?=Form::label('Contactdate', 'offer_contactdate');?>
      <?=Form::input('offer_contactdate', Input::post('offer_contactdate', ''), array('autocomplete' => 'off'));?>
    </p>
    <p>
     <?=Form::label('Comment', 'offer_comment');?>
     <?=Form::textarea('offer_comment', Input::post('offer_comment', ''), array('cols' => 40, 'rows' => 4));?></p>
    <div class="actions">
     <?=Form::submit();?>
    </div>
    <?=Form::close();?>
    </div>
    

    Anyone got any ideas? Thanks!
  • https://github.com/samitrimal/mod-cms
    download it and see i have implemented a validation in simple way.
    Hopr it helps u
    regards ,
    samitrimal
  • Sorry, couldn't find any Validation implemented in any of your controllers. Correct link?
  • $val->errors() will return an array of Validation_Error objects, each of which will contain references to the Fieldset it belongs to and to the field that caused the error. Both of those contain references to eachother which print_r/var_dump will output as *RECURSION* because it notices it already output those objects. These Validation_Error objects will output as error message when cast to string or echo'ed.
  • Yeah I've seen that, the odd thing about this is that I've been able to do
    Session::set_flash('notice', $val->errors())
    

    in other controllers but with this controller/form the recursion kicks off and the cookie gets too big. I can't figure out why this is happening here and not in other places. Anyone got any experience with this?

Howdy, Stranger!

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

In this Discussion