/** * Default validation field * if validation failed method * throw validation exception */ public function validate($value) { $this->validation = new Validation(array($this->get_name() => $value)); //$this->validation->label($this->get_name(), $this->get_name()); if ($this->item['notnull']) { if ($value == null) { $this->validation->rule($this->get_name(), 'not_empty'); } } if (!$this->validation->check()) { throw new Validation_Exception($this->validation); } return $this->validation; }
public function validate($data) { $val = new Validation($data); foreach ($this->validator as $key => $rules) { foreach ($rules as $rule => $value) { if (is_numeric($rule)) { $val->rule($key, $value); } else { $val->rule($key, $rule, $value); } } } $val->check(); return $val->errors(); }
public function action_edit_field() { $field_id = $this->request->param('options'); xml::to_XML(array('field' => array('@id' => $field_id, '$content' => User::get_data_field_name($field_id))), $this->xml_content); if (count($_POST) && isset($_POST['field_name'])) { $post = new Validation($_POST); $post->filter('trim'); $post->rule('Valid::not_empty', 'field_name'); if ($post->validate()) { $post_values = $post->as_array(); if ($post_values['field_name'] != User::get_data_field_name($field_id) && !User::field_name_available($post_values['field_name'])) { $post->add_error('field_name', 'User::field_name_available'); } } // Retry if ($post->validate()) { $post_values = $post->as_array(); User::update_field($field_id, $post_values['field_name']); $this->add_message('Field ' . $post_values['field_name'] . ' updated'); $this->set_formdata(array('field_name' => $post_values['field_name'])); } else { $this->add_error('Fix errors and try again'); $this->add_form_errors($post->errors()); $this->set_formdata(array_intersect_key($post->as_array(), $_POST)); } } else { $this->set_formdata(array('field_name' => User::get_data_field_name($field_id))); } }
public function action_show() { $arr = []; //Получаем данные из формы if (isset($_POST['submit'])) { //Проверяем введенные данные на корректность $post = new Validation($_POST); $post->rule('prime', 'not_empty')->rule('prime', 'numeric'); if ($post->check()) { $max = $_POST['prime']; $arr = Controller_PrimeNumber::getPrime($max); Controller_DataArchive::saveDB($max, $arr); } else { $errors = $post->errors('comments'); } } //Подготавливаем данные для вида $view = View::factory('index'); if (isset($errors)) { $view->err = $errors; } else { if (!empty($arr)) { $view->arr = $arr; } } $this->response->body($view); }
public function action_entry() { // Set employees node $employees_node = $this->xml_content->appendChild($this->dom->createElement('employees')); $employees = array('0option' => array('@value' => '0', 'None')); $counter = 1; foreach (Employees::get() as $employee) { $employees[$counter . 'option'] = array('@value' => $employee['id'], $employee['lastname'] . ', ' . $employee['firstname']); $counter++; } xml::to_XML($employees, $employees_node); // This is for the select box if (count($_POST)) { $post = new Validation($_POST); $post->filter('trim'); $post->filter('floatval', 'sum'); $post->filter('floatval', 'vat'); $post->rule('strtotime', 'accounting_date'); $post->rule('strtotime', 'transfer_date'); $post->rule('Valid::not_empty', 'description'); if ($post->Validate()) { $new_transaction_data = array('accounting_date' => $post->get('accounting_date'), 'transfer_date' => $post->get('transfer_date'), 'description' => $post->get('description'), 'journal_id' => $post->get('journal_id'), 'vat' => $post->get('vat'), 'sum' => $post->get('sum'), 'employee_id' => $post->get('employee_id')); if (!isset($_GET['id'])) { $transaction = new Transaction(NULL, $new_transaction_data); $this->add_message('Transaction ' . $transaction->get_id() . ' added'); } else { $transaction = new Transaction($_GET['id']); $transaction->set($new_transaction_data); $this->add_message('Transaction ' . $transaction->get_id() . ' updated'); $this->set_formdata($transaction->get()); } } else { $this->add_form_errors($post->errors()); $this->set_formdata($post->as_array()); } } elseif (isset($_GET['id'])) { $transaction = new Transaction($_GET['id']); $this->set_formdata($transaction->get()); } else { $this->set_formdata(array('accounting_date' => date('Y-m-d', time()), 'transfer_date' => date('Y-m-d', time()))); } }
public function action_upload() { $md5 = md5($_FILES['image']['tmp_name']); $file = $md5 . '_' . time() . '.' . pathinfo($_FILES['image']['name'])['extension']; $fileValidation = new Validation($_FILES); $fileValidation->rule('image', 'upload::valid'); $fileValidation->rule('image', 'upload::type', array(':value', array('jpg', 'png'))); if ($fileValidation->check()) { if ($path = Upload::save($_FILES['image'], $file, DIR_IMAGE)) { ORM::factory('File')->set('sid', $this->siteId)->set('name', $_FILES['image']['name'])->set('path', $file)->set('md5', $md5)->set('types', Model_File::FILE_TYPES_IMG)->set('created', time())->set('updated', time())->save(); $site = ORM::factory('Site')->where('id', '=', $this->siteId)->where('uid', '=', $this->user['id'])->find(); $site->set('logo', $file)->set('updated', time())->save(); jsonReturn(1001, '上传成功!', '/media/image/data/' . $file); } else { jsonReturn(4444, '图片保存失败'); } } else { jsonReturn(4444, '图片上传失败'); } }
public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc) { if ($this->min !== NULL or $this->max !== NULL) { $min = $this->min !== NULL ? $this->min : -99999999999; $max = $this->max !== NULL ? $this->max : 99999999999; $validation->rule($this->name, 'range', array(':value', $min, $max)); } if (!empty($this->_props['regexp'])) { if (strpos($this->regexp, '::') !== FALSE) { list($class, $method) = explode('::', $this->regexp); } else { $class = 'Valid'; $method = $this->regexp; } if (method_exists($class, $method)) { $validation->rule($this->name, array($class, $method)); } else { $validation->rule($this->name, 'regex', array(':value', $this->regexp)); } } return parent::onValidateDocument($validation, $doc); }
public function action_uploadavatar() { $filename = time() . '_' . $_FILES['image']['name']; $file_validation = new Validation($_FILES); $file_validation->rule('image', 'upload::valid'); $file_validation->rule('image', 'upload::type', array(':value', array('jpg', 'png', 'gif', 'jpeg'))); if ($file_validation->check()) { if ($path = Upload::save($_FILES['image'], $filename, DIR_IMAGE)) { $image = CacheImage::instance(); $src = $image->resize($filename, 100, 100); $user = Auth::instance()->get_user(); $user->avatar = $filename; $user->save(); $json = array('success' => 1, 'image' => $src); } else { $json = array('success' => 0, 'errors' => array('image' => 'The file is not a valid Image')); } } else { $json = array('success' => 0, 'errors' => (array) $file_validation->errors('profile')); } echo json_encode($json); exit; }
public function action_index() { $installation_status = Kohana::$config->load('install.status'); switch ($installation_status) { case NOT_INSTALLED: if (!$this->check_installation_parameters()) { //$data['title']=__('Installation: something wrong'); // $this->data=Arr::merge($this->data, Helper::get_db_settings()); $this->data = Arr::merge($this->data, get_object_vars($this->install_config)); $res = View::factory('install_form', $this->data)->render(); //Model::factory('model_name')->model_method( $data ); } else { $_post = Arr::map('trim', $_POST); $post = new Validation($_post); $post->rule('db_path', 'not_empty')->rule('db_name', 'not_empty')->rule('db_login', 'not_empty')->rule('installer_login', 'not_empty')->rule('installer_password', 'not_empty'); if ($post->check()) { Helper::save_install_settings($post); if (!Model::factory('install')->install($err_list)) { Helper::set_installation_status(NOT_INSTALLED); foreach ($err_list as $e) { $this->data['errors'][] = $e['error']; } $res = View::factory('installing', $this->data)->render(); } else { $res = View::factory('installing_ok', $this->data)->render(); } } else { // Кажется что-то случилось //$data['title']=__('Installation: something wrong'); $this->data['errors'] = $post->errors('validation'); $res = View::factory('install_form', $this->data)->render(); } } break; case INSTALLING: $res = View::factory('installing')->render(); break; case INSTALLED: // $res = View::factory('/')->render(); $this->redirect('/'); break; default: // $res = View::factory('/')->render(); $this->redirect('/'); break; } // Save result / Сохраняем результат $this->_result = $res; }
/** * Edit Tags * if id is set, instanciate an edit function * if not instanciate an add tag function. */ public function action_role() { $this->xml_content_types = $this->xml_content->appendChild($this->dom->createElement('roles')); xml::to_XML(Uvtag::get_tags(), $this->xml_content_types, 'role'); if (!empty($_POST)) { $post = new Validation($_POST); $post->filter('trim'); $post->rule('Valid::not_empty', 'role'); $post->rule('Valid::not_empty', 'uri'); if (isset($role)) { $tag->update($post->as_array()); $this->add_message('Role name updated'); } else { if (Uvtag::add($post->get('role'), $post->get('uri'))) { $this->add_message('Role "' . $post->get('name') . '" was added'); } else { $this->add_message('Role "' . $post->get('name') . '" could not be added'); } } } elseif (isset($tag)) { // Set the form input to the tag name. $this->set_formdata($tag->get()); } }
public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc) { $validation->rule($this->name, 'color'); return parent::onValidateDocument($validation, $doc); }
public function action_payout() { // Set employees node $employees_node = $this->xml_content->appendChild($this->dom->createElement('employees')); $employees = array(); $counter = 0; $employees_from_model = Employees::get(); foreach ($employees_from_model as $employee) { $employees[$counter . 'option'] = array('@value' => $employee['id'], $employee['lastname'] . ', ' . $employee['firstname']); $counter++; } xml::to_XML($employees, $employees_node); // This is for the select box xml::to_XML($employees_from_model, $employees_node, 'employee', 'id'); if (isset($_GET['id'])) { if (count($_POST)) { $post = new Validation($_POST); $post->filter('trim'); $salary->set($post->as_array()); } $this->set_formdata($salary->get()); xml::to_XML(array('statuses' => array('1option' => array('@value' => 'active', 'Active'), '2option' => array('@value' => 'inactive', 'Inactive'))), $this->xml_content); xml::to_XML($employee->get(), $this->xml_content->appendChild($this->dom->createElement('employee')), NULL, 'id'); $this->add_message('Employee ' . $_GET['id'] . ' information updated'); } elseif (count($_POST)) { // Add new payout $post = new Validation($_POST); $post->filter('trim'); $post->rule('Valid::digit', 'amount'); $post->rule('strtotime', 'date'); if ($post->validate()) { $post_array = $post->as_array(); $transaction_data = array('accounting_date' => date('Y-m-d', strtotime($post_array['date'])), 'transfer_date' => date('Y-m-d', strtotime($post_array['date'])), 'description' => 'Salary payout', 'journal_id' => NULL, 'vat' => 0, 'sum' => -$post_array['amount'], 'employee_id' => $post_array['employee_id']); $transaction = new Transaction(NULL, $transaction_data); if ($id = $transaction->get_id()) { $this->add_message('New transaction added (ID ' . $id . ')'); } else { $this->add_error('Something f****d up'); } } else { $this->set_formdata($post->as_array()); $errors = $post->errors(); $this->add_form_errors($errors); if (isset($errors['date'])) { $this->add_form_errors(array('date' => 'Invalid date format')); } if (isset($errors['amount'])) { $this->add_form_errors(array('amount' => 'Must be numbers ONLY')); } } } }
/** * Validate the data, returning array of errors (if any). * * @return array */ public function validate() { $validation = new Validation($this->_data); foreach ($this->_fields as $fieldName => $fieldOptions) { foreach (Arr::get($fieldOptions, 'rules', array()) as $fieldRule) { if (is_array($fieldRule)) { // Take the 1st param as the name, the rest as params. $ruleName = array_shift($fieldRule); $ruleParams = $fieldRule; } else { // Just use the rule as passed with no params. $ruleName = $fieldRule; $ruleParams = NULL; } // Check if the named function is actually a method on this object. if (!function_exists($ruleName) && method_exists($this, $ruleName)) { $ruleName = array($this, $ruleName); } $validation->rule($fieldName, $ruleName, $ruleParams); $validation->label($fieldName, Arr::get($fieldOptions, 'label', $fieldName)); } } if ($validation->check()) { $this->_errors = array(); return TRUE; } else { if (!(bool) ($errorMessageFile = $this->_errorMessageFile)) { // Konform_Contact => 'konform/contact' $errorMessageFile = strtolower(str_replace('_', '/', get_class($this))); } $this->_errors = $validation->errors($errorMessageFile); return FALSE; } }
/** * * @param Validation $validation * @param DataSource_Hybrid_Document $doc * @return Validation */ public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc) { $file = NULL; $types = $this->types; if ($validation->offsetExists($this->name)) { $file = $validation->offsetGet($this->name); } if ($this->isreq === TRUE) { if (is_array($file)) { $validation->rules($this->name, array(array('Upload::not_empty'))); } else { $validation->rules($this->name, array(array('not_empty'))); } } if (is_array($file)) { $validation->rule($this->name, 'Upload::valid')->rule($this->name, 'Upload::size', array(':value', $this->max_size)); if (!empty($types)) { $validation->rule($this->name, 'Upload::type', array(':value', $this->types)); } } return parent::onValidateDocument($validation, $doc); }
public function action_user() { $formdata = array(); if (isset($_GET['id'])) { $user = new User($_GET['id'], FALSE, FALSE, 'default', FALSE); if (!$user->logged_in()) { $this->redirect(); } } $this->list_available_data_fields(); if (!empty($_POST) && isset($_POST['username']) && isset($_POST['password'])) { $post = new Validation($_POST); $post->filter('trim'); $post->filter('strtolower', 'username'); $post->rule('Valid::not_empty', 'username'); if (isset($user)) { if ($_POST['username'] != $user->get_username()) { $post->rule('User::username_available', 'username'); } } else { $post->rule('User::username_available', 'username'); } if (!isset($user)) { $post->rule('Valid::not_empty', 'password'); } if (isset($_POST['do_add_field'])) { // Add another user data field and save no data, but repopulate the form fields if (!isset($_SESSION['detail_fields'])) { $_SESSION['detail_fields'] = array(); } $_SESSION['detail_fields'][] = $_POST['add_field']; // Reconstruct the form data to repopulate the form $formdata = array(); $counter = 0; $post_values = $post->as_array(); foreach ($post_values as $field => $data) { if (substr($field, 0, 8) == 'fieldid_') { foreach ($data as $data_piece) { $counter++; $formdata['field_' . substr($field, 8) . '_' . $counter] = trim($data_piece); } } elseif ($field == 'username') { $formdata[$field] = $post_values[$field]; } } } else { // Check for form errors if ($post->validate()) { // No form errors, add the user! $post_values = $post->as_array(); // Erase the empty data fields foreach ($post_values as $key => $value) { if (substr($key, 0, 8) == 'fieldid_' && is_array($value)) { foreach ($value as $nr => $value_piece) { if ($value_piece == '') { unset($post_values[$key][$nr]); } } } } // Organize the field data and set the session fields $fields = $_SESSION['detail_fields'] = array(); foreach ($post_values as $key => $value) { if (substr($key, 0, 6) == 'field_') { list($foobar, $field_id, $field_nr) = explode('_', $key); $fields[User::get_data_field_name($field_id)][] = $value; } } if (!isset($_GET['id'])) { // Actually add the user User::new_user($post_values['username'], $post_values['password'], $fields); $this->add_message('User ' . $post_values['username'] . ' added'); } elseif (isset($user)) { $user->set_user_data(array_merge($fields, array('username' => $post_values['username'], 'password' => $post_values['password'])), TRUE); $this->add_message('User data saved'); } } else { // Form errors detected! $this->add_error('Fix errors and try again'); $this->add_form_errors($post->errors()); $formdata = array(); $counter = 0; $post_values = $post->as_array(); foreach ($post_values as $field => $data) { if (substr($field, 0, 8) == 'fieldid_') { foreach ($data as $data_piece) { $counter++; $formdata['field_' . substr($field, 8) . '_' . $counter] = trim($data_piece); } } elseif ($field == 'username') { $formdata[$field] = $post_values[$field]; } } } } } if (isset($user)) { $formdata = array('username' => $user->get_username()); $counter = 0; foreach ($user->get_user_data() as $field => $data) { foreach ($data as $data_piece) { $counter++; $formdata['field_' . User::get_data_field_id($field) . '_' . $counter] = $data_piece; } } } if (!empty($_SESSION['detail_fields'])) { foreach ($_SESSION['detail_fields'] as $field_id) { $counter = 1; while (isset($formdata['field_' . $field_id . '_' . $counter])) { $counter++; } $formdata['field_' . $field_id . '_' . $counter] = ''; } } $this->set_formdata($formdata); }
/** * Add validation rules to validation object * * @param Validation Validation object * @return array */ protected function _check(Validation $data) { foreach ($this->_fields as $name => $field) { if ($field['type'] === 'email') { $data->rule($name, 'email'); } if (Arr::get($field, 'required')) { $data->rule($name, 'required'); } if (Arr::get($field, 'unique')) { $data->rule($name, array(':model', '_is_unique'), array(':validation', $name)); } foreach (array('min_value', 'max_value', 'min_length', 'max_length') as $rule) { if (Arr::get($field, $rule) !== NULL) { $data->rule($name, $rule, array(':value', $field[$rule])); } } if (isset($field['rules'])) { $data->rules($name, $field['rules']); } if (isset($field['label'])) { $data->label($name, $field['label']); } } return $data; }
public function action_edit_image() { $name = $this->request->param('options'); if ($content_image = new Content_Image($name)) { $short_name = substr($name, 0, strlen($name) - 4); $this->xml_content_image = $this->xml_content->appendChild($this->dom->createElement('image')); $this->xml_content_image->setAttribute('name', $name); $this->xml_content_image->appendChild($this->dom->createElement('URL', 'user_content/images/' . $name)); $tags_node = $this->xml_content_image->appendChild($this->dom->createElement('tags')); if (count($_POST)) { $_POST['name'] = URL::title($_POST['name'], '-', TRUE); $post = new Validation($_POST); $post->filter('trim'); $post->rule('Valid::not_empty', 'name'); $form_data = $post->as_array(); if ($form_data['name'] != $short_name) { $post->rule('Content_Image::image_name_available', 'name'); } // Check for form errors if ($post->validate()) { // No form errors, edit image $new_image_data = array(); foreach ($form_data['tag'] as $nr => $tag_name) { if (!isset($new_image_data[$tag_name])) { $new_image_data[$tag_name] = array(); } $new_image_data[$tag_name][] = $form_data['tag_value'][$nr]; } $content_image->set_data(array_merge($new_image_data, array('name' => $form_data['name'] . '.jpg'))); if ($form_data['name'] != $short_name) { // If the image name have changed, we need to change the URL also // Save the message for the new URL $_SESSION['content']['image']['message'] = 'Image data saved'; // Redirect to the new name $this->redirect('/admin/images/edit_image/' . $form_data['name'] . '.jpg'); } $this->add_message('Image data saved'); $this->set_formdata(array('name' => $short_name)); $image_data = $content_image->get_data(); foreach ($image_data as $tag_name => $tag_values) { foreach ($tag_values as $tag_value) { $tag_node = $tags_node->appendChild($this->dom->createElement('tag', $tag_value)); $tag_node->setAttribute('name', $tag_name); } if (!count($tag_values)) { $tag_node = $tags_node->appendChild($this->dom->createElement('tag')); $tag_node->setAttribute('name', $tag_name); } } } else { // Something is wrong. Fill form with unsaved data and push error $this->set_formdata(array('name' => $form_data['name'])); foreach ($form_data['tag'] as $nr => $tag_name) { if ($tag_name != '') { $tag_node = $tags_node->appendChild($this->dom->createElement('tag', $form_data['tag_value'][$nr])); $tag_node->setAttribute('name', $tag_name); } } $this->add_form_errors($post->errors()); } } else { $this->set_formdata(array('name' => $short_name)); $image_data = $content_image->get_data(); foreach ($image_data as $tag_name => $tag_values) { foreach ($tag_values as $tag_value) { $tag_node = $tags_node->appendChild($this->dom->createElement('tag', $tag_value)); $tag_node->setAttribute('name', $tag_name); } if (!count($tag_values)) { $tag_node = $tags_node->appendChild($this->dom->createElement('tag')); $tag_node->setAttribute('name', $tag_name); } } } if (isset($_SESSION['content']['image']['message'])) { $this->add_message($_SESSION['content']['image']['message']); unset($_SESSION['content']['image']['message']); } } else { $this->redirect(); } }
/** * Tests Validation::check() * * @test * @covers Validation::check */ public function test_check_stops_when_error_added_by_callback() { $validation = new Validation(array('foo' => 'foo')); $validation->rule('foo', array($this, '_validation_callback'), array(':validation'))->rule('foo', 'min_length', array(':value', 20)); $validation->check(); $errors = $validation->errors(); $expected = array('foo' => array(0 => '_validation_callback', 1 => NULL)); $this->assertSame($errors, $expected); }
/** * Register with code * * @param Model_Invitation $invitation */ public function _join(Model_Invitation $invitation) { $user = new Model_User(); $user->email = $invitation->email; // Handle post $errors = array(); if ($_POST && !Arr::get($_POST, 'signup')) { $post = Arr::extract($_POST, array('username', 'password', 'password_confirm')); $validation = new Validation($post); $validation->rule('password_confirm', 'matches', array(':validation', 'password', 'password_confirm')); try { $user->username = $post['username']; $user->password = $post['password']; $user->created = time(); $user->save($validation); // Delete used invitation $invitation->delete(); // Login user $user->add_role('login'); Visitor::instance()->login($user, $_POST['password']); $this->request->redirect(URL::user($user)); } catch (Validation_Exception $e) { $user->password = $user->password_confirm = null; $errors = $e->array->errors('validation'); } } $this->view->add(View_Page::COLUMN_MAIN, $this->section_register($user, $errors, $invitation->code)); }
public function action_uploadlinkimg() { $filename = 'link_' . time() . '_' . $_FILES['image']['name']; $file_validation = new Validation($_FILES); $file_validation->rule('image', 'upload::valid'); $file_validation->rule('image', 'upload::type', array(':value', array('jpg', 'png', 'gif', 'jpeg'))); if ($file_validation->check()) { if ($path = Upload::save($_FILES['image'], $filename, DIR_IMAGE)) { $images = CacheImage::instance(); $src = $images->resize($filename, 400, 200); $json = array('success' => 1, 'image' => $src, 'filename' => $filename); } else { $json = array('success' => 0, 'errors' => array('image' => 'The file is not a valid Image')); } } else { $json = array('success' => 0, 'errors' => (array) $file_validation->errors('profile')); } echo json_encode($json); exit; }
/** * Check to ensure the user "owns" the gallery * * @param Validation $validation */ public function validate($validation) { $validation->rule('avatar-gallery[id]', array($this, 'user_has_avatar'), array(':validaiton', ':field', ':value')); }
/** * Adds any validation rules/labels for validating _options * * public function build_validation(Validation $validation) * { * return parent::build_validation($validation) * ->rule('paramname', 'not_empty'); // Require this param * } * * @param Validation the validation object to add rules to * * @return Validation */ public function build_validation(Validation $validation) { // Add a rule to each key making sure it's in the task foreach ($validation->data() as $key => $value) { $validation->rule($key, array($this, 'valid_option'), array(':validation', ':field')); } return $validation; }
public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc) { $options = $this->get_options(); if ($this->empty_value === TRUE) { $options = array(0) + $options; } $validation->rule($this->name, 'array_key_exists', array(':value', $options)); return parent::onValidateDocument($validation, $doc); }
/** * Tests Validation::check() * * @test * @covers Validation::check * @covers Validation::rule * @covers Validation::rules * @covers Validation::errors * @covers Validation::error * @dataProvider provider_check * @param array $array The array of data * @param array $rules The array of rules * @param array $labels The array of labels * @param boolean $expected Is it valid? * @param boolean $expected_errors Array of expected errors */ public function test_check($array, $rules, $labels, $expected, $expected_errors) { $validation = new Validation($array); foreach ($labels as $field => $label) { $validation->label($field, $label); } foreach ($rules as $field => $field_rules) { foreach ($field_rules as $rule) { $validation->rule($field, $rule[0], $rule[1]); } } $status = $validation->check(); $errors = $validation->errors(TRUE); $this->assertSame($expected, $status); $this->assertSame($expected_errors, $errors); $validation = new validation($array); foreach ($rules as $field => $rules) { $validation->rules($field, $rules); } $validation->labels($labels); $this->assertSame($expected, $validation->check()); }
/** * Правила валидации значения поля документа. * При сохранении документа происходит валидация значений его полей. * Каждое поле прогоняется в цикле и происходит вызов этого метода. * * @see DataSource_Hybrid_Document::validate() * * @param Validation $validation * @param DataSource_Hybrid_Document * @return \Validation */ public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc) { if ($this->isreq === TRUE and $this->is_required()) { $validation->rule($this->name, 'not_empty'); } if ($this->unique === TRUE) { $validation->rule($this->name, array($this, 'check_unique'), array(':value', $doc)); } return $validation->label($this->name, $this->header); }
public function action_edit_page() { $id = $this->request->param('options'); $content_page = new Content_Page($id); if ($content_page->get_page_id()) { $this->xml_content_page = $this->xml_content->appendChild($this->dom->createElement('page')); // Get all tags associated with pages and images $this->xml_content_tags = $this->xml_content->appendChild($this->dom->createElement('tags')); $tags = array(); foreach (Content_Page::get_tags() as $tag) { $tags[] = $tag; } foreach (Content_Image::get_tags() as $tag) { foreach ($tags as $tag_to_check) { if ($tag_to_check['name'] == $tag['name']) { break 2; } } $tags[] = $tag; } foreach ($tags as $tag) { $tag_node = $this->xml_content_tags->appendChild($this->dom->createElement('tag', $tag['name'])); $tag_node->setAttribute('id', $tag['id']); } if (count($_POST) && isset($_POST['URI']) && isset($_POST['name'])) { if ($_POST['URI'] == '') { $_POST['URI'] = $_POST['name']; } $_POST['URI'] = URL::title($_POST['URI'], '-', TRUE); $post = new Validation($_POST); $post->filter('trim'); $post->rule('Valid::not_empty', 'name'); if ($post->validate()) { $post_values = $post->as_array(); $current_page_data = $content_page->get_page_data(); if ($post_values['name'] != $current_page_data['name'] && !Content_Page::page_name_available($post_values['name'])) { $post->add_error('name', 'Content_Page::page_name_available'); } if ($post_values['URI'] != $current_page_data['URI'] && !Content_Page::page_URI_available($post_values['URI'])) { $post->add_error('URI', 'Content_Page::page_URI_available'); } } // Retry if ($post->validate()) { $tags = array(); foreach ($post_values['template_position'] as $nr => $template_position) { if ($post_values['tag_id'][$nr] > 0) { if (!isset($tags[$template_position])) { $tags[$template_position] = array(); } $tags[$template_position][] = $post_values['tag_id'][$nr]; } } $content_page->update_page_data($post_values['name'], $post_values['URI'], $tags); $this->add_message('Page "' . $post_values['name'] . '" updated'); $page_data = $content_page->get_page_data(); unset($page_data['tag_ids']); $this->set_formdata($page_data); } else { $this->add_error('Fix errors and try again'); $this->add_form_errors($post->errors()); // Fix template position data $tmp_node = $this->xml_content->appendChild($this->dom->createElement('tmp')); foreach ($post_values['template_position'] as $nr => $template_position) { $template_field_node = $tmp_node->appendChild($this->dom->createElement('template_field')); $template_field_node->setAttribute('id', $template_position); if ($post_values['tag_id'][$nr] > 0) { $tag_node = $template_field_node->appendChild($this->dom->createElement('tag')); $tag_node->setAttribute('id', $post_values['tag_id'][$nr]); } } unset($post_values['template_position'], $post_values['tag_id']); $this->set_formdata($post_values); } } else { $page_data = $content_page->get_page_data(); unset($page_data['tag_ids']); $this->set_formdata($page_data); } /** * Put the page data to the XML * */ $page_data = $content_page->get_page_data(); $page_data['template_fields'] = array(); foreach ($page_data['tag_ids'] as $template_field_id => $tag_ids) { $page_data['template_fields'][$template_field_id . 'template_field'] = array('@id' => $template_field_id); foreach ($tag_ids as $tag_id) { $page_data['template_fields'][$template_field_id . 'template_field'][$tag_id . 'tag'] = array('@id' => $tag_id); } } // Unset this, or it will cludge our XML unset($page_data['tag_ids']); // Set the page data to the page node xml::to_XML($page_data, $this->xml_content_page, NULL, 'id'); } else { $this->redirect(); } }
public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc) { $validation->rule($this->name, array($this, 'is_valid_documents_ids')); return parent::onValidateDocument($validation, $doc); }