Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Multiple Checkboxes and Radio Buttons
  • It looks like there's no support for multiple checkboxes and radio buttons. I don't see any way to mark them checked and although all the other form types are "sticky" when a form is redisplayed, the checkboxes and radio buttons are not. Also, I searched through all the core code and could not find "checked" used in conjunction with a form. Am I correct or just missing how to do it? For example, when adding a checkbox or radio button field using the Fieldset add function (see below), I don't see a way to check the boxes.
      $form->add('checkbox_test',
       'Checkbox Test',
       array(
        'type' => 'checkbox',
        'options' => array('3' => 'Three', '4' => 'Four'),
       ),
       array(
       array('required')
       )
      );
    
      $form->add('radio_test',
       'Radio',
       array(
        'type' => 'radio',
        'options' => array('5' => 'Five', '6' => 'Six'),
       ),
       array(
       )
      );
    
  • There wasn't until 5 minutes ago, now you can pass the value or an array of values (for checkboxes) and the inputs will be checked when they have a corresponding value. (this only works when using the options array, otherwise you'll have to set the checked status using set_attribute('checked', 'checked')) When using $fieldset->repopulate() you don't have to set them yourself, it will be taken from the $_POST array.
  • Wow, such service! I'll download the develop branch and give it a try in the next day or so. Thanks
  • Hi - going back to the original topic, can someone help me understand how to do this correctly please? I'm currently building up a collection of checkboxes like this:
    $fieldset->add('roles', 'Roles', array(
     'type' => 'checkbox',
     'options' => array(
      'one' => 'one',
      'two' => 'two'
    )
    ));
    

    I've got both the key and the value set to the same as I need the name of the checkbox and label to match. What I need to figure out how to populate which boxes should be pre-checked. Can anyone help? Cheers!
  • I just downloaded the newest code and noticed a validation error that I was not seeing before: ErrorException [ Error ]: Using $this when not in object context
    COREPATH/classes/validation.php @ line 443 438 * @param mixed
    439 * @return bool
    440 */
    441 public function _validation_required($val)
    442 {
    443 return ! $this->_empty($val);
    444 }
    445
    446 /**
    447 * Special empty method because 0 and '0' are non-empty values
    448 *
  • Damn, deleted my reply by accident. I'm pretty sure that shouldn't happen. I wrote it in a way that the method should always be called non-static, and this error should show when it's called staticly. Can you put the full report with backtrace and earlier errors on http://scrapyrd.com/ or post a screenshot?
  • There's not much of a backtrace, and I couldn't figure out how to post a screen shot, so I just copied everything on the page. Let me know if there's any other info that I can supply. Error! ErrorException [ Error ]: Using $this when not in object context
    COREPATH/classes/validation.php @ line 443 438 * @param mixed
    439 * @return bool
    440 */
    441 public function _validation_required($val)
    442 {
    443 return ! $this->_empty($val);
    444 }
    445
    446 /**
    447 * Special empty method because 0 and '0' are non-empty values
    448 * Backtrace COREPATH/base.php @ line 182 177
    178if ( ! function_exists('fuel_shutdown_handler'))
    179{
    180 function fuel_shutdown_handler()
    181 {
    182 return \Error::shutdown_handler();
    183 }
    184}
    185
    186if ( ! function_exists('fuel_exception_handler'))
    187{ Prior Contents (show) <br />
    <b>Fatal error</b>: Using $this when not in object context in <b>M:\xampp\htdocs\_fuel-v1.0-rc2\core\classes\validation.php</b> on line <b>443</b><br /> Fuel PHP is released under the MIT license.
  • Hmm, that's too bad - this is a shutdown error, and those don't give a backtrace. Are you sure you're not calling the method _validation_required() yourself staticly? (either from within using static::_validation_required(), or from the outside using Validation::_validation_required()) I just tested the require rule and it works without errors for me (found another unrelated bug by accident though)
  • Ok, I've narrowed it down to the one line of code that now causes the error. It's in the model code where I try to use the add_callable() function. I've extended the validation class to add a few callback functions. Here's the abbreviated model code (I took out all fields but one). The second line of code ($val->add_callable('validation') causes the error to occur. If I remove it, the error goes away.
     public static function set_form_fields(Fieldset $form, $user_id = null)
     &#123;
      // Add my validation class in order to use callbacks strong_password and unique
      $val = $form->validation();
      $val->add_callable('validation');
    
      $form->add('first_name',
       'First Name',
       array(
        'type' => 'text',
        'value' => ! empty($admin['first_name']) ? $admin['first_name'] : '',
        'id' => 'first-name',
        'size' => 50,
       ),
       array(
        array('required'),
        array('trim'),
        array('max_length', 50),
       )
      );
    
      $form->add('submit', null,
       array(
        'type' => 'submit',
        'value' => 'Submit'
       )
      );
    }
    
  • You're adding "validation" which is the Validation class, it supersedes the call to the validation instance and thus required is called staticly (callable string = call static, callable instance = call non-static) on the validation class. You don't need to add "Validation" as a callable, it's always there as the last option.
  • Yes, I removed those 2 lines and all is good.
  • I'm not sure I understand what you're asking, but the way to set which checkboxes are checked is to include the "value" field. So in your example, you can set the "one" checkbox like below. To set both checkboxes you would use: 'value' => array('one', 'two'). If I misunderstood your question, sorry, and try to explain again.
    $fieldset->add('roles', 'Roles', array(
     'type' => 'checkbox',
     'options' => array(
      'one' => 'one',
      'two' => 'two'
    ),
    'value' => array('one')
    )
    

  • It looks like I spoke too soon. After further testing, when I remove the 2 validation lines, the callback functions in the extended validation class are not recognized, and I receive "Notice" messages that the rules are invalid: Notice: Invalid rule "unique" passed to Validation, not used. in (unknown) [(unknown)]: add_rule
    Notice: Invalid rule "unique" passed to Validation, not used. in (unknown) [(unknown)]: add_rule
    Notice: Invalid rule "strong_password" passed to Validation, not used. in (unknown) [(unknown)]: add_rule If I place the callback validation functions in the Model, they are found, but they're not found in the extended validation class. I've traced the code through core/classes/fieldset/field.php add_rule() function, and the validation class does not appear in the $callables array. The model class does appear in the $callables array.
  • There's an instance of the Validation class in the callables array, as I explained before. If the extension methods aren't found that can only mean one thing: it's loading the original class. Are you sure you followed the instructions here: http://fuelphp.com/docs/general/extending_core.html Also if you're on RC2, there was a bug in the factory method that made it always return the core class - this has been fixed in the develop branch already.
  • I believe I've followed the instructions on extending the core. Here's the entry in bootstrap.php:
    Autoloader::add_classes(array(
     // Add classes you want to override here
     // Example: 'View' => APPPATH.'classes/view.php',
     'Security' => APPPATH.'classes/security.php',
     'Session' => APPPATH.'classes/session.php',
     'Validation' => APPPATH.'classes/validation.php',
     'Fieldset' => APPPATH.'classes/fieldset.php',
    ));
    

    I also put a dummy static function in my extended validation class that I call and can see the code executing it, so it seems my validation class has been recognized. I'm running a debugger and traced the code through core/classes/fieldset/field.php add_rule() function, and the validation class does not appear in the $callables array. Am I looking in the right place? Here's my abbreviated extended validation class:
    class Validation extends \Fuel\Core\Validation
    &#123;
    
     public static function test()
     &#123;
      $x = 1;
     }
    
     public function _validation_unique($form_value, $params)
     &#123;
      list($db_table, $unique_field, $id_field, $id, $form_name) = explode('|', $params);
    
      $query = DB::select("LOWER (\"$unique_field\")")
       ->from($db_table)
       ->where("$unique_field", '=', MBSTRING ? mb_strtolower($form_value) : strtolower($form_value))
       ->and_where($id_field, '!=', $id)
       ;
    
      $result = $query->execute();
      if($result->count() == 0)
      &#123;
       return true;
      }
      else
      &#123;
       // Get the Validation instance for the current Fieldset and set error message
       $val = Fieldset::instance($form_name)->validation();
       $val->set_message('unique', 'That :label is already used. Please pick a different :label.');
       return false;
      }
     }
    }
    

    On a side note, when trying to have a generic validation callback such as _validation_unique() and trying to add an error message, the validation instance is needed. In order to get the validation instance when using a Fieldset, the name assigned to the Fieldset is needed. I ended up passing the Fieldset name (which is not a public variable) to _validation_unique(). Is there an easier or preferred way to to do this?
  • Jelmer Schreuder wrote on Sunday 22nd of May 2011:
    There's an instance of the Validation class in the callables array, as I explained before. Also if you're on RC2, there was a bug in the factory method that made it always return the core class - this has been fixed in the develop branch already.

    I've deleted the stuff you've answered and left what answers your repeated questions. EDIT:
    On a side note, when trying to have a generic validation callback such as _validation_unique() and trying to add an error message, the validation instance is needed. In order to get the validation instance when using a Fieldset, the name assigned to the Fieldset is needed. I ended up passing the Fieldset name (which is not a public variable) to _validation_unique(). Is there an easier or preferred way to to do this?
    $this->set_message()? EDIT 2:
    And from outside the Validation class you can fetch the currently running validation instance with Validation::active(). (ONLY IN develop BRANCH, not in RC2)
  • I'm sorry, but I'm not sure I followed your answer. However, I downloaded the latest core from the develop branch, and the callback functions still generate an error notice that they're unknown even though the extended validation class appears to be recognized. I don't know if this is any help, but when I trace through the fieldset/field.php add_rule() function, $callables[0] = 'Model_Admin' which is a string representing the model where the fieldset fields are added. But $callables[1] is not a string with the name of the class. Instead it's an object Fuel\Core\Validation. So the function method_exists is false for both elements of the $callables array.
  • Mitchell Steinberg wrote on Sunday 22nd of May 2011:
    I'm sorry, but I'm not sure I followed your answer. However, I downloaded the latest core from the develop branch, and the callback functions still generate an error notice that they're unknown even though the extended validation class appears to be recognized. I don't know if this is any help, but when I trace through the fieldset/field.php add_rule() function, $callables[0] = 'Model_Admin' which is a string representing the model where the fieldset fields are added. But $callables[1] is not a string with the name of the class. Instead it's an object Fuel\Core\Validation. So the function method_exists is false for both elements of the $callables array.
    Ok, now we're getting somewhere. I'm not sure why you're surprised that the 2nd callable is an instance of the Validation class, as that's exactly what I said it would & should be (otherwise you couldn't use $this in the validation methods). Now the question is why it's an instance of Fuel\Core\Validation when you say you're sure that your extension is used. Could you do the following:
    - check which type of class the validation instance you're using is, using get_class($validation)
    - show me how you're creating the instance And make sure you really do have the latest from https://github.com/fuel/core/tree/develop - recently someone else was telling me he had and he quite clearly had downloaded the master branch.
  • The core directory that I downloaded is: fuel-core-v1.0-rc2.1-239-g47c2038.zip, so hopefully that's the latest. If there's anything else I can check to verify I have the latest, just let me know.
    Could you do the following:
    - check which type of class the validation instance you're using is, using get_class($validation)
    - show me how you're creating the instance

    I wasn't sure where to put the call to the get_class() function, but since the error occurs in the Model set_form_fields() function when the $form->add() function is called, I put it at the beginning of the set_form_fields() function. Here's what I did:
     public static function set_form_fields(Fieldset $form, $user_id = null)
     &#123;
      $val = $form->validation();
      $c = get_class($val);
    .....
      $form->add('email',
       'Email',
       array(
        'type' => 'text',
        'value' => ! empty($admin['email']) ? $admin['email'] : '',
        'id' => 'email',
        'size' => 50,
       ),
       array(
        array('required'),
        array('trim'),
        array('max_length', 255),
        array('valid_email'),
        array('unique', "auth_users|email|user_id|$user_id|$form_name"),
       )
      );
    .....
    }
    
    The class $c is 'Fuel\Core\Validation'. If there's anything else useful to examine, just let me know.
  • You need the latest from the develop branch, that's a download from a tag and only has a security fix over RC2. (it is RC2.1) You can download the latest from the core develop branch: https://github.com/fuel/core/zipball/develop FrenkyNet provides a (unofficial) full download of Fuel with all the latest from not only the core but also the packages: http://fuel.frenky.net/latest_fuel.zip
  • I guess I'm a bit confused. The link you provided points to the same file I downloaded earlier today: fuel-core-v1.0-rc2.1-239-g47c2038.zip
  • Ah, I'm sorry, not sure why it includes the tag in the filename but from a quick check it looks allright. But there's a bug in there that makes the validation() method return a Fuel\Core\Validation instance instead of taking it from global. I'll commit a fix in a few minutes. On another note, unless you need add_field() you can call the add() method directly on the fieldset instance - because that's exactly what the add() method in validation does (the Validation instance just forwards the method call to the Fieldset instance).
  • Unless I misunderstand, I believe I am calling the add() function on the fieldset. Is this what you were suggesting? Abbreviated function set_form_fields():
     public static function set_form_fields(Fieldset $form, $user_id = null)
     &#123;
                    .......
    
      $form->add('email',
       'Email',
       array(
        'type' => 'text',
        'value' => ! empty($admin['email']) ? $admin['email'] : '',
        'id' => 'email',
        'size' => 50,
       ),
       array(
        array('required'),
        array('trim'),
        array('max_length', 255),
        array('valid_email'),
        array('unique', "auth_users|email|user_id|$user_id|$form_name"),
       )
      );
                   
                   .......
    }
    
  • Lol, yeah sorry, you're right - it's late and I'm tired.
  • No problem. BTW, I just downloaded the new develop code 240, and it looks like the issue has been fixed. Thanks for working with me on this. It's much appreciated.

Howdy, Stranger!

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

In this Discussion