Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

set_multiselect in form_helper and Form_validation #1799

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+88 −0
Split
@@ -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']))
@cryode

cryode Oct 13, 2012

Contributor

These should be AND instead of OR. You can add both as parameters to one isset() as the other methods have (keeps it consistent).

+ {
+ 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