Fork me on GitHub

Nibble Development

Nibble Development

PHP classes and libraries for developers

Radio button field

The radio button field creates a set of radio buttons for a given set of options. This means that options must be supplied and thus there are two required arguments to create a radio field instead of one as with the three field types mentioned above. There are also four optional, additional arguments for further customisation. The simplest instantiation of a radio button field will look something like:

  1. Label: This argument is the same as the text field label argument, simply the string to be displayed as a label for the radio button set.
  2. Options: The second argument is an array of options for the user to select from, each option is created as a radio button. The array format dictates the radio button value and the radio button label. The array can simply be an array of values, in which case the values will be the labels and the radio button values will start at zero and increment by one for each option, this is because arrays automatically have numeric keys if none are specified. If the radio button value needs to be customised, the array can use custom keys which will be used. You can even mix and match custom keys with automatic keys:
  3. Required: The third argument states whether the user must select one of the radio buttons or not, exactly the same as the text field required argument.
  4. False values: The fourth argument is an array of false values, the values in the array relate to the keys of the options array. If a value from this array matches a key from the options array then that option will not validate if the user selects it. This means that you can personalise the radio buttons for each user, some users may be able to select an option, some may not. (this functionality was designed originally for the select field where a “please select” option may be desired but not as a valid selection)  array('car') // will invalidate the value "Choice two" as its key is "car" in the options example above

A fully customised radio field should look something like the below example:

Luke Rotherfield

Freelance PHP Developer
is a PHP developer dedicated to writing the most awesome liraries for his fellow devs :) enjoy

Latest posts by Luke Rotherfield (see all)

Leave a Comment