/** * Tests customforms::validate_custom_form_fields() * * @dataProvider providerTestValidateCustomFormFields */ public function testValidateCustomFormFields($valid_data, $invalid_data) { // Setup validation objects for the valid custom forms data $valid_validator = Validation::factory($valid_data)->pre_filter('trim', TRUE); // Get the return value for validation of valid date $errors = customforms::validate_custom_form_fields($valid_validator); // Assert that validation of the valid data returns no errors $this->assertEquals(0, count($errors), "Some errors have been found" . Kohana::debug($errors)); // Set up validation for the invalid custom forms data $invalid_validator = Validation::factory($invalid_data)->pre_filter('trim', TRUE); // Get the return value for validation of invalid data $errors = customforms::validate_custom_form_fields($invalid_validator); // Assert that the validation of the invalid data returns some errors $this->assertEquals(TRUE, count($errors) > 0, "Expected to encounter errors. None found: " . count($errors)); // Garbage collection unset($valid_validator, $invalid_validator, $errors); }
/** * Validation of form fields * * @param array $post Values to be validated * @param bool $admin_section Whether the validation is for the admin section */ public static function validate(array &$post, $admin_section = FALSE) { // Exception handling if (!isset($post) or !is_array($post)) { return FALSE; } // Create validation object $post = Validation::factory($post)->pre_filter('trim', TRUE); $post->add_rules('incident_title', 'required', 'length[3,200]'); $post->add_rules('incident_description', 'required'); $post->add_rules('incident_date', 'required', 'date_mmddyyyy'); $post->add_rules('incident_hour', 'required', 'between[1,12]'); $post->add_rules('incident_minute', 'required', 'between[0,59]'); if ($post->incident_ampm != "am" and $post->incident_ampm != "pm") { $post->add_error('incident_ampm', 'values'); } // Validate for maximum and minimum latitude values $post->add_rules('latitude', 'required', 'between[-90,90]'); // Validate for maximum and minimum longitude values $post->add_rules('longitude', 'required', 'between[-180,180]'); $post->add_rules('location_name', 'required', 'length[3,200]'); //XXX: Hack to validate for no checkboxes checked if (!isset($post->incident_category)) { $post->incident_category = ""; $post->add_error('incident_category', 'required'); } else { $post->add_rules('incident_category.*', 'required', 'numeric'); } // Validate only the fields that are filled in if (!empty($post->incident_news)) { foreach ($post->incident_news as $key => $url) { if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { $post->add_error('incident_news', 'url'); } } } // Validate only the fields that are filled in if (!empty($post->incident_video)) { foreach ($post->incident_video as $key => $url) { if (!empty($url) and !(bool) filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED)) { $post->add_error('incident_video', 'url'); } } } // If deployment is a single country deployment, check that the location mapped is in the default country if (!Kohana::config('settings.multi_country')) { $country = Country_Model::get_country_by_name($post->country_name); if ($country and $country->id != Kohana::config('settings.default_country')) { $post->add_error('country_name', 'single_country'); } } // Validate photo uploads $post->add_rules('incident_photo', 'upload::valid', 'upload::type[gif,jpg,png]', 'upload::size[2M]'); // Validate Personal Information if (!empty($post->person_first)) { $post->add_rules('person_first', 'length[3,100]'); } if (!empty($post->person_last)) { $post->add_rules('person_last', 'length[2,100]'); } if (!empty($post->person_email)) { $post->add_rules('person_email', 'email', 'length[3,100]'); } // Extra validation rules for the admin section if ($admin_section) { $post->add_rules('location_id', 'numeric'); $post->add_rules('message_id', 'numeric'); $post->add_rules('incident_active', 'required', 'between[0,1]'); $post->add_rules('incident_verified', 'required', 'between[0,1]'); $post->add_rules('incident_zoom', 'numeric'); } // Custom form fields validation $errors = customforms::validate_custom_form_fields($post); // Check if any errors have been returned if (count($errors) > 0) { foreach ($errors as $field_name => $error) { $post->add_error($field_name, $error); } } //> END custom form fields validation // Return return $post->validate(); }
/** * Validation of form fields * * @param array $post Values to be validated */ public static function validate(array &$post) { // Exception handling if (!isset($post) or !is_array($post)) { return FALSE; } // Create validation object $post = Validation::factory($post)->pre_filter('trim', TRUE)->add_rules('incident_title', 'required', 'length[3,200]')->add_rules('incident_description', 'required')->add_rules('incident_date', 'required', 'date_mmddyyyy')->add_rules('incident_hour', 'required', 'between[1,12]')->add_rules('incident_minute', 'required', 'between[0,59]')->add_rules('incident_ampm', 'required'); if (isset($post->incident_ampm) and $post->incident_ampm != "am" and $post->incident_ampm != "pm") { $post->add_error('incident_ampm', 'values'); } // Validate for maximum and minimum latitude values //$post->add_rules('latitude','required','between[-90,90]'); // Validate for maximum and minimum longitude values //$post->add_rules('longitude','required','between[-180,180]'); $post->add_rules('location_name', 'required', 'length[3,200]'); //XXX: Hack to validate for no checkboxes checked if (!isset($post->incident_category)) { $post->incident_category = ""; $post->add_error('incident_category', 'required'); } else { $post->add_rules('incident_category.*', 'required', 'numeric'); } // Validate only the fields that are filled in if (!empty($post->incident_news)) { foreach ($post->incident_news as $key => $url) { if (!empty($url) and !valid::url($url)) { $post->add_error('incident_news', 'url'); } } } // Validate only the fields that are filled in if (!empty($post->incident_video)) { foreach ($post->incident_video as $key => $url) { if (!empty($url) and !valid::url($url)) { $post->add_error('incident_video', 'url'); } } } // If deployment is a single country deployment, check that the location mapped is in the default country if (!Kohana::config('settings.multi_country') and isset($post->country_name)) { $country = Country_Model::get_country_by_name($post->country_name); if ($country and $country->id != Kohana::config('settings.default_country')) { $post->add_error('country_name', 'single_country', array(ORM::factory('country', Kohana::config('settings.default_country'))->country)); } } // Validate photo uploads $max_upload_size = Kohana::config('settings.max_upload_size'); $post->add_rules('incident_photo', 'upload::valid', 'upload::type[gif,jpg,png,jpeg]', "upload::size[" . $max_upload_size . "M]"); // Validate Personal Information if (!empty($post->person_first)) { $post->add_rules('person_first', 'length[2,100]'); } else { $post->person_first = ''; } if (!empty($post->person_last)) { $post->add_rules('person_last', 'length[2,100]'); } else { $post->person_last = ''; } if (!empty($post->person_email)) { $post->add_rules('person_email', 'email', 'length[3,100]'); } else { $post->person_email = ''; } $post->add_rules('location_id', 'numeric'); $post->add_rules('incident_active', 'between[0,1]'); $post->add_rules('incident_verified', 'between[0,1]'); $post->add_rules('incident_zoom', 'numeric'); // Custom form fields validation customforms::validate_custom_form_fields($post); //> END custom form fields validation // Return return $post->validate(); }