checkbox.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php if ( ! defined( 'ABSPATH' ) ) exit;
  2. function ninja_forms_register_field_checkbox(){
  3. $args = array(
  4. 'name' => __( 'Checkbox', 'ninja-forms' ),
  5. 'edit_options' => array(
  6. array(
  7. 'type' => 'select', //What type of input should this be?
  8. 'options' => array(
  9. array(
  10. 'name' => __( 'Unchecked', 'ninja-forms' ),
  11. 'value' => 'unchecked',
  12. ),
  13. array(
  14. 'name' => __( 'Checked', 'ninja-forms' ),
  15. 'value' => 'checked',
  16. ),
  17. ),
  18. 'name' => 'default_value', //What should it be named. This should always be a programmatic name, not a label.
  19. 'label' => __( 'Default Value', 'ninja-forms' ),
  20. 'class' => 'widefat', //Additional classes to be added to the input element.
  21. ),
  22. ),
  23. //'edit_function' => 'ninja_forms_field_checkbox_edit',
  24. 'display_function' => 'ninja_forms_field_checkbox_display',
  25. 'group' => 'standard_fields',
  26. 'edit_label' => true,
  27. 'edit_label_pos' => true,
  28. 'label_pos_options' => array(
  29. array('name' => __( 'Left of Element', 'ninja-forms' ), 'value' => 'left'),
  30. array('name' => __( 'Above Element', 'ninja-forms' ), 'value' => 'above'),
  31. array('name' => __( 'Below Element', 'ninja-forms' ), 'value' => 'below'),
  32. array('name' => __( 'Right of Element', 'ninja-forms' ), 'value' => 'right'),
  33. ),
  34. 'edit_req' => true,
  35. 'edit_custom_class' => true,
  36. 'edit_help' => true,
  37. 'edit_desc' => true,
  38. 'edit_meta' => false,
  39. 'sidebar' => 'template_fields',
  40. 'edit_conditional' => true,
  41. 'conditional' => array(
  42. 'action' => array(
  43. 'show' => array(
  44. 'name' => __( 'Show This', 'ninja-forms' ),
  45. 'js_function' => 'show',
  46. 'output' => 'show',
  47. ),
  48. 'hide' => array(
  49. 'name' => __( 'Hide This', 'ninja-forms' ),
  50. 'js_function' => 'hide',
  51. 'output' => 'hide',
  52. ),
  53. 'change_value' => array(
  54. 'name' => __( 'Change Value', 'ninja-forms' ),
  55. 'output' => 'select',
  56. 'options' => array(
  57. 'Checked' => 'checked',
  58. 'Unchecked' => 'unchecked',
  59. ),
  60. 'js_function' => 'change_value',
  61. ),
  62. ),
  63. 'value' => array(
  64. 'type' => 'select',
  65. 'options' => array(
  66. 'Checked' => 'checked',
  67. 'Unchecked' => 'unchecked',
  68. ),
  69. ),
  70. ),
  71. 'req_validation' => 'ninja_forms_field_checkbox_validation',
  72. 'edit_sub_value' => 'nf_field_checkbox_edit_sub_value',
  73. );
  74. ninja_forms_register_field('_checkbox', $args);
  75. }
  76. //Register the Checkbox field
  77. add_action('init', 'ninja_forms_register_field_checkbox');
  78. //Checkbox Display Function
  79. function ninja_forms_field_checkbox_display( $field_id, $data, $form_id = '' ){
  80. $field_class = ninja_forms_get_field_class( $field_id, $form_id );
  81. $default_value = $data['default_value'];
  82. if($default_value == 'checked' OR $default_value == 1){
  83. $checked = 'checked = "checked"';
  84. }else{
  85. $checked = '';
  86. }
  87. ?><input id="" name="ninja_forms_field_<?php echo $field_id;?>" type="hidden" value="unchecked" /><input id="ninja_forms_field_<?php echo $field_id;?>" name="ninja_forms_field_<?php echo $field_id;?>" type="checkbox" class="<?php echo $field_class;?>" value="checked" <?php echo $checked;?> rel="<?php echo $field_id;?>"/><?php
  88. }
  89. //Checkbox Pre-Processing Function
  90. function ninja_forms_field_checkbox_pre_process( $field_id, $user_value ){
  91. global $ninja_forms_processing;
  92. if( $user_value != 'checked' AND $user_value != 'unchecked' ){
  93. if( $user_value == 1 ){
  94. $user_value = 'checked';
  95. }else{
  96. $user_value = 'unchecked';
  97. }
  98. }
  99. if( $ninja_forms_processing->get_field_value( $field_id ) !== false ){
  100. $ninja_forms_processing->update_field_value( $field_id, $user_value );
  101. }
  102. }
  103. //Checkbox Validation Function
  104. function ninja_forms_field_checkbox_validation( $field_id, $user_value ){
  105. if( $user_value != 'unchecked' AND $user_value != '' ){
  106. return true;
  107. }else{
  108. return false;
  109. }
  110. }
  111. /**
  112. * Edit submission value output function
  113. *
  114. * @since 2.7
  115. * @return void
  116. */
  117. function nf_field_checkbox_edit_sub_value( $field_id, $user_value ) {
  118. if ( $user_value == 'checked' ) {
  119. $checked = 'checked="checked"';
  120. } else {
  121. $checked = '';
  122. }
  123. ?>
  124. <input type="hidden" name="fields[<?php echo $field_id; ?>]" value="unchecked">
  125. <input type="checkbox" name="fields[<?php echo $field_id; ?>]" value="checked" <?php echo $checked; ?>>
  126. <?php
  127. }