<li> <span class="dummy"></span> <?php echo form_input($fld_name . '[options][]', set_value($fld_name . '[options][]'), 'class="val_opts" size="50"'); ?> <a class="delete_btn" href="#">X</a> </li> <?php } ?> <a class="add_btn add_option" href="#">Add Option</a> </ul> <ul class="validation"> <li><?php echo form_hidden($fld_name . '[validation]', set_value($fld_name . '[validation]')); ?> </li> <?php $options = new FieldOptions(set_value($fld_name . '[validation]')); ?> <li class="pretty_rules"><?php echo $options->getPrettyRules(); ?> </li> <li><?php echo anchor('admin/validation', 'Set Validation', 'class="add_btn add_validation"'); ?> </li> </ul> </li>
public function validation() { $phoneFormats = array('1 (123) 456-7890', '1 123 456 7890', '1-123-456-7890', '11234567890'); $charVType = 'char'; $validationTypes = array($charVType => 'Specific characters', 'alpha' => 'Alphabet', 'alpha_numeric' => 'Alphanumeric', 'valid_email' => 'E-mail', 'integer' => 'Number', 'phone_format' => 'Phone Number (US)'); $this->load->helper('form'); $this->load->library('form_validation'); $this->load->library('formsdb'); // set defaults if unset $defaults = array('vtype' => $charVType, 'chars' => 'disallow', 'phone_format' => 0); foreach ($defaults as $fname => $value) { if (!isset($_POST[$fname])) { $_POST[$fname] = $value; } } // rules with 1 parameter $paramedRules = array('min_length', 'max_length', 'greater_than', 'less_than', 'phone_format'); // parse validation rules, retrieve already submitted rules if ($this->input->post('rules') !== FALSE) { $rulesStr = $this->input->post('rules'); foreach ($paramedRules as $ruleName) { if (preg_match('/' . $ruleName . '\\[([^\\]]*)\\]/', $rulesStr, $match)) { $_POST[$ruleName] = $match[1]; } } $rules = explode(RULE_SEPARATOR, $rulesStr); foreach ($validationTypes as $ruleName => $niceName) { if ($ruleName != $charVType && array_search($ruleName, $rules) !== FALSE) { $_POST['vtype'] = $ruleName; break; } } } $paramedRuleRules = array('min_length' => 'is_natural', 'max_length' => 'is_natural', 'greater_than' => 'numeric', 'less_than' => 'numeric', 'phone_format' => ''); // set rules foreach ($paramedRules as $ruleName) { $ruleValue = $this->input->post($ruleName); if ($ruleValue != '') { //If there is a value for the rule, ensure it's valid $this->form_validation->set_rules($ruleName, $ruleName, $paramedRuleRules[$ruleName]); } else { //Rules are optional, so if has no value, don't check for additional rule $this->form_validation->set_rules($ruleName, $ruleName, ''); } //Creating a special post field just for validation purposes $_POST['paramedRules'][$ruleName] = $this->input->post($ruleName); } $this->form_validation->set_rules('vtype', 'validation type', ''); $this->form_validation->set_rules('phone_format', 'character allowance/disallowance', ''); $this->form_validation->set_rules('chars', 'character allowance/disallowance', ''); $this->form_validation->set_rules('chars_spec', 'character allowance/disallowance', ''); //Checks relationships between rules, so need the array of rules as a parameter $this->form_validation->set_rules('paramedRules', 'Rule parameters', 'callback__paramedRulesCheck'); if ($this->form_validation->run() !== FALSE && $this->input->post('rules') === FALSE) { $newRuleSet = array(); $lengthValidation = $this->_collapseParamedRules(array('min_length', 'max_length')); if ($lengthValidation !== "") { $newRuleSet[] = $lengthValidation; } switch ($this->input->post('vtype')) { case $charVType: $charList = str_replace(' ', OPT_SEPARATOR, $this->input->post('char_specs')); $niceList = str_replace(' ', '", "', $this->input->post('char_specs')); switch ($this->input->post('chars')) { case 'allow': $subRules[] = 'contains_only' . '[' . $this->input->post($ruleName) . ']'; break; case 'disallow': $subRules[] = 'restricted_chars' . '[' . $this->input->post($ruleName) . ']'; break; } break; case 'integer': $newRuleSet[] = 'integer'; $newRuleSet[] = $this->_collapseParamedRules(array('greater_than', 'less_than')); break; case 'phone_format': $newRuleSet[] = $this->_collapseParamedRules(array('phone_format')); break; default: $asIsRules = array('alpha' => 'Can only contain the alphabet. ', 'alpha_numeric' => 'Can only contain alphanumeric characters. ', 'valid_email' => 'Must be a valid email. '); if (array_search($this->input->post('vtype'), array_keys($asIsRules)) !== FALSE) { $newRuleSet[] = $this->input->post('vtype'); } } $options = new FieldOptions($newRuleSet); $uglyRuleString = $options->getSerialized(); $prettyRuleString = $options->getPrettyRules(); printf('{"rules": "%s", "pretty": "%s"}', $uglyRuleString, $prettyRuleString); return; } $this->load->view('validation_form', array('phoneFormats' => $phoneFormats, 'validationTypes' => $validationTypes)); }