/** * Initializes validation rules, and labels * * @return void */ protected function _validation() { // Build the validation object with its rules $this->_validation = Validation::factory($this->_object)->bind(':model', $this)->bind(':original_values', $this->_original_values)->bind(':changed', $this->_changed); foreach ($this->rules() as $field => $rules) { $this->_validation->rules($field, $rules); } // Use column names by default for labels $columns = array_keys($this->_table_columns); // Merge user-defined labels $labels = array_merge(array_combine($columns, $columns), $this->labels()); foreach ($labels as $field => $label) { $this->_validation->label($field, $label); } }
/** * Add rules to the validation object * * @access protected * @param mixed Formo_Container $field. (default: NULL) * @return void */ protected function _add_rules(Validation $validation) { if (!($rules = $this->get('rules'))) { // Only do anything if the field has rules return; } $validation->label($this->alias(), $this->view()->label()); $validation->rules($this->alias(), $rules); }
/** * * @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); }
/** * Add rules to a validation object. * * @access protected * @param Validation $validation * @return void */ protected function _add_rules_to_validation(Validation $validation) { $validation->rules($this->alias(), $this->_rules); }
/** * 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; }
/** * * @param Validation $validation * @param DataSource_Hybrid_Document $doc * @return Validation */ public function onValidateDocument(Validation $validation, DataSource_Hybrid_Document $doc) { $image = $doc->get($this->name); $url = $doc->get($this->name . '_url'); $from_url = FALSE; if (Valid::url($url)) { $from_url = TRUE; } if ($this->isreq === TRUE and $from_url === FALSE) { if (is_array($image)) { $validation->rules($this->name, array(array('Upload::not_empty'))); } else { $validation->rules($this->name, array(array('not_empty'))); } } elseif ($this->isreq === TRUE and $from_url === TRUE) { $validation->rules($this->name . '_url', array(array('not_empty'))); } if ($from_url === FALSE and is_array($image)) { $validation->rules($this->name, array(array('Upload::valid'), array('Upload::type', array(':value', $this->types)), array('Upload::size', array(':value', $this->max_size)))); } else { if ($from_url === TRUE) { $ext = strtolower(pathinfo($url, PATHINFO_EXTENSION)); $validation->rules($this->name . '_url', array(array('url'), array('in_array', array($ext, $this->types)))); } } return $validation->label($this->name . '_url', $this->header)->label($this->name, $this->header); }
public function action_import() { $pass_id = -1; $import_success = array(); //Store reg_id's that were successfully imported. $import_failure = array(); //Store reg_id's that were not imported (error). $reg_ids = array(); if ($post = $this->request->post()) { $validate = new Validation($_FILES); $validate->rules('csv_file', array(array('Upload::not_empty', NULL), array('Upload::valid', NULL), array('Upload::size', array(':value', '1M')), array('Upload::type', array(':value', array('csv'))))); $pass_id = empty($post['pass_id']) ? -1 : $post['pass_id']; $pass = $this->loadPass($post); if ($validate->check() && isset($_FILES['csv_file']) && $pass) { $handle = fopen($_FILES['csv_file']['tmp_name'], 'rb'); $line = fgetcsv($handle); while ($line = fgetcsv($handle)) { $line[0] = trim($line[0]); //In the event of a blank row, there is only one element. if (empty($line[0])) { continue; } $reg = $this->generateReg($pass, $line); if (!$reg->pickupStatus) { $reg->pickupStatus = 0; } $values = array('email' => $reg->email, 'phone' => $reg->phone); try { if ($reg->reserveTickets()) { $reg->save($this->validateEmailOrPhone($values)); if (!isset($import_success[$reg->email])) { $import_success[$reg->email] = array(); } array_push($import_success[$reg->email], $reg); $reg->finalizeTickets(); if (@$_POST['email_on_completion']) { array_push($reg_ids, $reg->id); } } else { array_push($import_failure, array('reg' => $reg, 'errors' => array('No more tickets available.'))); } } catch (ORM_Validation_Exception $e) { $reg->releaseTickets(); array_push($import_failure, array('reg' => $reg, 'errors' => $e->errors(''))); } } fclose($handle); /* FIXME: Send an email per index. */ } else { if (!$pass) { $this->addError('Not a valid ticket! Please select a valid ticket.'); } else { $this->addError('Not a valid file. Please try again.'); } } } if (!empty($import_success) || !empty($import_failure)) { $this->template->content = new View('admin/UploadResults', array('import_success' => $import_success, 'import_failure' => $import_failure)); if (!empty($reg_ids) && @$_POST['email_on_completion']) { $config = Kohana::config('ecmproject'); $email = Email::factory($config['registration_subject']); $email->from($config['outgoing_email_address'], $config['outgoing_email_name']); $regs = ORM::Factory('Registration')->where('registrations.id', 'IN', $reg_ids)->with('convention')->where('email', '!=', '')->order_by('email')->find_all(); $email = ''; $data = array(); foreach ($regs as $reg) { if ($email != $reg->email) { if (count($data)) { $this->email_imported_regs($email, $data); } $data = array(); } $email = $reg->email; $data['registrations'][$reg->convention->name][] = $reg; $data['name'] = $reg->gname . ' ' . $reg->sname; } if (count($data)) { $this->email_imported_regs($email, $data); } } } else { $passes = ORM::Factory('pass')->with('convention')->find_all(); $this->template->content = new View('admin/Upload', array('passes' => $passes, 'pass_id' => $pass_id)); } }
/** * Fetch a validation object from a field or form * * @access public * @param array $array (default: NULL) * @return Validation obj */ public function validation(array $array = NULL) { $this->driver('pre_validate'); if ($array !== NULL) { $validation = new Validation($array); $validation->rules($this->alias(), $this->_rules); foreach ($this->_fields as $field) { $field->driver('pre_validate'); $validation->rules($field->alias(), $field->get('rules')); } } else { $values = $this->driver('get_validation_values'); $validation = new Validation($values); $this->_add_rules_to_validation($validation); } $parent = $this->parent(TRUE); $validation->bind(':formo', $this); $validation->bind(':form_val', $parent->val()); $validation->bind(':form', $parent); return $validation; }
/** * Runs business logic validations on this model. * * You can pass an existing validation object into this method. * This will let you add some application specific validations to * run on the model. Password validation is a good use case for * this. * * You can use the :model binding in your validation rules to * access this model object. * * @param Validation $validation a previously filled validation obj * * @return array */ public function valid(Validation $validation = NULL, Validation $default_validation = NULL) { $data = $validation instanceof Validation ? $validation->copy($validation->data() + $this->as_array()) : $default_validation; if (!$default_validation) { $data = new Validation($this->as_array()); } $data->bind(':model', $this); foreach ($this->_rules as $field => $rules) { $data->rules($field, $rules); } if ($data->check(TRUE)) { return TRUE; } else { throw new Validation_Exception($data); } }