public function _upload_image(Validate $array, $input) { if ($array->errors()) { // Don't bother uploading return; } // Get the image from the array $image = $array[$input]; if (!Upload::valid($image) or !Upload::not_empty($image)) { // No need to do anything right now return; } if (Upload::valid($image) and Upload::type($image, $this->types)) { $filename = strtolower(Text::random('alnum', 20)) . '.jpg'; if ($file = Upload::save($image, NULL, $this->directory)) { Image::factory($file)->resize($this->width, $this->height, $this->resize)->save($this->directory . $filename); // Update the image filename $array[$input] = $filename; // Delete the temporary file unlink($file); } else { $array->error('image', 'failed'); } } else { $array->error('image', 'valid'); } }
public function password_validate(Validate $array, $field) { if ($this->loaded() && !array_key_exists($field, $this->_changed)) { return TRUE; } if (!Validate::min_length($array[$field], 3)) { $array->error($field, 'min_length', array(5)); return FALSE; } if (!Validate::max_length($array[$field], 50)) { $array->error($field, 'max_length', array(50)); return FALSE; } return TRUE; }
public function shop_got_item(Validate $array, $field) { $amount = $array[$field]; $cost = $this->item->price * $amount; // Check if shop got enought items if ($this->item->amount < $amount) { $array->error($field, 'not_enough'); return false; } // Check if user can afford it if ($this->character->money < $cost) { $array->error($field, 'expensive'); return false; } }
/** * Verifica que la persona exista. * * Se utiliza para otorgar las constancias. * * @param Validate $array * @param <type> $campo */ public function existe(Validate $array, $campo) { if (ORM::factory("persona")->where("cedula", "=", $array[$campo])->count_all() == 0) { $array->error($campo, "no_existe"); } return $array; }
/** * Username callback to check if username is valid */ public function check_username(Validate $array, $field) { $exists = (bool) DB::select(array('COUNT("*")', 'total_count'))->from('users')->where('username', '=', $array[$field])->execute()->get('total_count'); if (!$exists) { $array->error($field, 'not_found', array($array[$field])); } }
/** * Does the reverse of unique_key_exists() by triggering error if username exists * Validation Rule * * @param Validate $array validate object * @param string $field field name * @param array $errors current validation errors * @return array */ public function slug_available(Validate $array, $field) { //die($field); if ($this->unique_key_exists($field, $array[$field])) { $array->error($field, 'slug_available', array($array[$field])); } }
/** * Validation callback to check that this user has a unique email * * @param Validate $array the validate object to use * @param string $field the field name to check * * @return null */ public function check_unique_email(Validate $array, $field) { $user = new Model_Vendo_User($array[$field]); // Only error if this is a new or different object if ($user->id and $user->id != $this->id) { $array->error($field, 'not_unique'); } }
public function has_category(Validate $array, $field) { $photo = (bool) DB::select(array('COUNT("*")', 'records_found'))->from('photos')->where('user_id', '=', Auth::instance()->get_user()->id)->where('category_id', '=', $array[$field])->execute($this->_db)->get('records_found'); if ($photo) { $array->error($field, 'category', array($array[$field])); return FALSE; } }
/** * Validate callback wrapper for checking password match * @param Validate $array * @param string $field * @return void */ public static function _check_password_matches(Validate $array, $field) { $auth = Auth::instance(); if ($array['password'] !== $array[$field]) { // Re-use the error messge from the 'matches' rule in Validate $array->error($field, 'matches', array('param1' => 'password')); } }
/** * Check username for profanity. * * @param Validate Validate object * @param string field name * @return void */ public function censor(Validate $array, $field) { $censor = Censor::factory($array[$field]); if ($censor->is_offensive()) { $array->error($field, 'censor', array($array[$field])); return FALSE; } }
/** * Validate callback wrapper for checking password match * @param Validate $array * @param string $field * @return void */ public function _check_password_matches(Validate $array, $field) { $auth = Auth::instance(); $salt = $auth->find_salt($array['password']); if ($array['password'] !== $auth->hash_password($array[$field], $salt)) { // Re-use the error message from the 'matches' rule in Validate $array->error($field, 'matches', array('param1' => 'password')); } }
public function _unique_email(Validate $array, $field) { // check the database for existing records $email_exists = (bool) ORM::factory('user')->where('email', '=', $array[$field])->where('id', '!=', $this->id)->count_all(); if ($email_exists) { // add error to validation object $array->error($field, 'email_exists', array($array[$field])); } }
/** * Check to see if the email address is unique * @param $array * @param $field * @param $errors * @return unknown_type */ public function do_unique_email(Validate $array, $field) { if (isset($array['u_email'])) { if ($this->current_user->email_exists($array['u_email'])) { // Email is not unique, so they have already registered with that email $array->error($field, 'unique_email', array($array[$field])); } } }
/** * Tests if a username exists in the database * * @param Validation * @param string field to check * @return array */ public function username_available(Validate $array, $field) { if ($this->loaded() and !$this->changed($field)) { return TRUE; } // The value is unchanged if (Sprig::factory($this->_user_model, array($field => $array[$field]))->load(null, FALSE)->count()) { $array->error($field, 'username_available'); } }
public function validate(Validate $array, $field) { if (empty($this->_file_errors)) { return TRUE; } foreach ($this->_file_errors as $error) { $array->error($field, $error); } return FALSE; }
/** * Tests if a username exists in the database. This can be used as a * Valdidation callback. * * @param object Validate object * @param string Field * @param array Array with errors * @return array (Updated) array with errors */ public function username_available(Validate $array, $field) { if ($this->loaded() and $this->_object[$field] === $array[$field]) { // This value is unchanged return TRUE; } if (ORM::factory($this->_user_model)->where($field, '=', $array[$field])->find_all(1)->count()) { $array->error($field, 'username_available'); } }
/** * Validation callback to verify that the entered username * does not already exist in the site database * @param Validate Validation object * @param string data field to validate against * @return void */ public function check_username(Validate $validate, $field) { if (!isset($this->_changed['username'])) { return; } $username_exists = KMS::instance('site')->users->where('username', '=', $validate[$field])->find(); if ($username_exists->loaded()) { $validate->error($field, 'already_exists', array($validate[$field])); } }
public function _time_constraint(Validate $data, $field) { $timeSpans = array(); for ($span = 0; $span <= $data['length']; $span++) { $timeSpans[] = $data['time'] + $span * 30; } $conflictTime = (bool) DB::select(array('COUNT("*")', 'total_count'))->from($this->_table_name)->where('dayId', '=', $data['dayId'])->where('roomId', '=', $data['dayId'])->where('time', 'IN', $timeSpans)->where($this->_primary_key, '!=', $this->pk())->execute($this->_db)->get('total_count'); if ($conflictTime) { $data->error($field, 'conflict_time', array($data[$field])); } }
public function validate_latlong_required_as_pair(Validate $validate, $field) { // if we are already invalid, just return if (array_key_exists($field, $validate->errors())) { return; } $other_field = $field == 'lat' ? 'long' : 'lat'; if (!empty($validate[$field]) && empty($validate[$other_field])) { $validate->error($other_field, 'required_if_other_given', array($field)); } }
/** * Login with a Validate callback: * * // Create an empty user * $user = Sprig::factory('user'); * * // Create a Validate instance * $post = Validate::factory($_POST) * ->rules('username', $user->field('username')->rules) * ->rules('password', $user->field('password')->rules) * ->callback('usernane', array($user, '_login')); * * // Check the POST and login * if ($post->check()) * { * URL::redirect($uri); * } * * @param object Validate instance * @param string field name * @return void */ public function _login(Validate $array, $field) { if ($array['username'] and $array['password']) { $this->username = $array['username']; $this->password = $array['password']; // Attempt to load the user by username and password $this->load(); if ($this->loaded()) { // Update the last login time $this->last_login = time(); $this->update(); Cookie::set('authorized', $this->username); } else { $array->error('username', 'invalid'); } } }
/** * Does the reverse of unique_key_exists() by triggering error if email exists. * Validation callback. * * @param Validate Validate object * @param string field name * @return void */ public function email_available(Validate $array, $field) { if ($this->unique_key_exists($array[$field])) { $array->error($field, 'email_available', array($array[$field])); } }
/** * Triggers an error if the email does not exist. * Validation callback. * * @param object Validate * @param string field name * @return void */ public function email_not_available(Validate $data, $field) { if ( ! $this->unique_key_exists($data[$field], 'email')) { $data->error($field, 'email_not_available', array($data[$field])); } }
public function password_length(Validate $array, $field) { if (strlen($array[$field]) < 6 or strlen($array[$field]) > 50) { $array->error($field, 'password_length', array($array[$field])); } }
/** * Validate callback wrapper for checking email uniqueness * * @static * @param Validate $array * @param string $field */ public static function _unique(Validate $array, $field) { if (Model_User::find_user($array[$field])) { $array->error($field, 'unique', array('param1' => $field)); } }
/** * Callback for validating unique fields. * * @param object Validate array * @param string field name * @return void */ public function _unique_field(Validate $array, $field) { if ($array[$field]) { $query = DB::select($this->_fields[$this->_primary_key]->column)->from($this->_table)->where($this->_fields[$field]->column, '=', $this->_fields[$field]->_database_wrap($array[$field]))->execute($this->_db); if (count($query)) { $array->error($field, 'unique'); } } }
/** * Validate callback wrapper for checking password match * * @static * @param Validate $array * @param string $field */ public static function _check_password_matches(Validate $array, $field) { if (empty($array['password']) || $array['password'] !== $array[$field]) { $array->error($field, 'matches', array('param1' => 'password')); } }
public function display_name_available(Validate $array, $field) { if (!empty($array[$field])) { if ($array[$field] != $this->display_name) { $available = (bool) DB::select(array('COUNT("*")', 'total_count'))->from($this->_table_name)->where('display_name', '=', $array[$field])->execute($this->_db)->get('total_count'); if ($available) { $array->error($field, 'available', array($array[$field])); } } } }
/** * Callback to check if a given file name is already used * in the current directory * * @param Validate validation object * @param string field name */ public function filename_available(Validate $array, $field) { $ext = $this->get_ext($array['file']['name']); $name = $array[$field] . '.' . $ext; $path = $this->path . '/' . $name; Kohana::$log->add(Kohana::DEBUG, 'Validating new file name ' . $path); $exists = is_file($path); if (!empty($array[$field]) and $exists) { Kohana::$log->add(Kohana::INFO, 'Upload filename given already exists. ' . $path); $array->error($field, 'filename_available', array($array[$field])); } }
/** * Options: * - type: The type of field (text, textarea, select, checkbox, radio, file, submit, button) */ public function build($fieldsets, $action = null, $method = 'post', $enctype = false) { $formId = md5(uniqid()); // Used to determine form being posted when validating feilds $formName = preg_replace('/[0-9]+/', '', $formId); // Used to set the id/name of the form tag $formData = array(); $html = Html::form()->open($action, $method, $enctype, array('id' => $formName, 'name' => $formName)); // Insert form id as hidden field $html .= '<input type="hidden" name="form_id" value="' . $formId . '" />'; foreach ($fieldsets as $fieldsetData) { $html .= '<ul>'; foreach ($fieldsetData['fields'] as $fieldName => $fieldData) { // Check for validation, add to session under form id it present if (isset($fieldData['validate'])) { $formData[$fieldName] = $fieldData; } // Add form id for js submit buttons $fieldData['form_name'] = $formName; $html .= '<li'; if (isset($fieldData['class'])) { $html .= sprintf(' class="%s"', $fieldData['class']); } else { $type = $fieldData['type'] == 'password' ? 'text' : $fieldData['type']; $html .= sprintf(' class="%s"', $type); } $html .= '>'; if (isset($fieldData['title']) && !is_null($fieldData['title'])) { $html .= '<label>' . $fieldData['title'] . '</label>'; } $html .= call_user_func(array('self', '_' . $fieldData['type']), $fieldName, $fieldData); if (isset($fieldData['content'])) { $html .= $fieldData['content']; } $html .= Validate::error($fieldName); $html .= '</li>'; } $html .= '</li>'; } $html .= Html::form()->close(); // Cache validation fields, if set if ($formData) { $this->_cache($formId, $formData); } return $html; }
/** * Callback for validating that a field is unique. * * @param Validate $data * @param string $field * @return void * @author Woody Gilk */ public function _is_unique(Validate $data, $field) { if ($data[$field]) { $count = Jelly::select($this->model)->where($field, '=', $data[$field]); // Exclude unique key value from check if this is a lazy save if (isset($data[':unique_key'])) { $count->where(':unique_key', '!=', $data[':unique_key']); } if ($count->count()) { $data->error($field, 'unique'); } } }