Permalink
Browse files

set_multiselect in form_helper and Form_validation

  • Loading branch information...
1 parent cd0b93b commit 18370833a9a984f9978e81f24767fe54ef9808b2 @Zoubi committed Sep 14, 2012
Showing with 88 additions and 0 deletions.
  1. +47 −0 system/helpers/form_helper.php
  2. +41 −0 system/libraries/Form_validation.php
@@ -831,6 +831,53 @@ function set_radio($field = '', $value = '', $default = FALSE)
// ------------------------------------------------------------------------
+if ( ! function_exists('set_multiselect'))
+{
+ /**
+ * Set Multiselect
+ *
+ * Let's you set the selected value of a multiselect field via info in the POST array.
+ * If Form Validation is active it retrieves the info from the validation class
+ *
+ * @param string
+ * @param array
+ * @return array
+ */
+
+ function set_multiselect( $field = '', $values = array() )
+ {
+ $OBJ =& _get_validation_object();
+
+ if ( $OBJ === FALSE )
+ {
+ if ( ! isset( $_POST[ $field ] ) )
+ {
+ return $values;
+ }
+
+ $field = $_POST[ $field ];
+
+ if ( is_array( $field ) )
+ {
+ return $field;
+ }
+ else
+ {
+ if ( ( $field == '' OR empty( $value ) OR ( ! in_array( $field, $value ) ) ) )
+ {
+ return array();
+ }
+ }
+
+ return $values;
+ }
+
+ return $OBJ->set_multiselect( $field, $values );
+ }
+}
+
+// ------------------------------------------------------------------------
+
if ( ! function_exists('form_error'))
{
@@ -934,6 +934,47 @@ public function set_checkbox($field = '', $value = '', $default = FALSE)
// --------------------------------------------------------------------
/**
+ * Set multiselect
+ *
+ * Enables multiselect to be set to the value the user
+ * selected in the event of an error
+ *
+ * @param string
+ * @param array
+ * @return array
+ */
+ public function set_multiselect( $field = '', $values = array() )
+ {
+ if ( ! isset($this->_field_data[$field]) OR ! isset($this->_field_data[$field]['postdata']))
+ {
+ if ( count( $this->_field_data ) === 0 )
+ {
+ return $values;
+ }
+
+ return array();
+ }
+
+ $field = $this->_field_data[$field]['postdata'];
+
+ if ( is_array( $field ) )
+ {
+ return $field;
+ }
+ else
+ {
+ if ( ( $field == '' OR empty( $value ) OR ( ! in_array( $field, $value ) ) ) )
+ {
+ return array();
+ }
+ }
+
+ return $values;
+ }
+
+ // --------------------------------------------------------------------
+
+ /**
* Required
*
* @param string

0 comments on commit 1837083

Please sign in to comment.