Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added add_rule_set method to the form_validation library #312

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
49 changes: 45 additions & 4 deletions system/libraries/Form_validation.php
Expand Up @@ -35,6 +35,7 @@ class CI_Form_validation {
protected $_error_suffix = '</p>';
protected $error_string = '';
protected $_safe_form_data = FALSE;
protected $_rules_sets = array();

/**
* Constructor
Expand Down Expand Up @@ -196,6 +197,31 @@ public function set_error_delimiters($prefix = '<p>', $suffix = '</p>')

// --------------------------------------------------------------------

/**
* Add Rule Set
*
* This method lets you add a custom rule set to attending the validations
* defined from your rules (callback), it is useful when you need use a rule
* from a library or another object that isn't instance of CI_Controller,
* in other words, you could keep off or use callback rules that are not
* directly in your controllers.
*
* @access public
* @param object
* @return this
*/
public function add_rule_set($object = NULL)
{
if (is_object($object))
{
$this->_rules_sets[] = $object;
}

return $this;
}

// --------------------------------------------------------------------

/**
* Get Error Message
*
Expand Down Expand Up @@ -586,11 +612,26 @@ protected function _execute($row, $rules, $postdata = NULL, $cycles = 0)
{
if ( ! method_exists($this->CI, $rule))
{
continue;
}
foreach ($this->_rules_sets as $rule_set)
{
if (method_exists($rule_set, $rule))
{
$result = $rule_set->$rule($postdata, $param);

break;
}
}

// Run the function and grab the result
$result = $this->CI->$rule($postdata, $param);
if ( ! isset($result))
{
continue;
}
}
else
{
// Run the function and grab the result
$result = $this->CI->$rule($postdata, $param);
}

// Re-assign the result to the master data array
if ($_in_array == TRUE)
Expand Down
3 changes: 2 additions & 1 deletion user_guide/changelog.html
Expand Up @@ -96,7 +96,8 @@ <h2>Version 2.1.0 (planned)</h2>
<li>Driver children can be located in any package path.</li>
<li>Added max_filename_increment config setting for Upload library.</li>
<li><samp>CI_Loader::_ci_autoloader()</samp> is now a protected method.</li>
<li>Added <kbd>is_unique</kbd> to the <a href="libraries/form_validation.html">Form Validation library</a>.</li>
<li>Added <kbd>is_unique</kbd> method to the <a href="libraries/form_validation.html">Form Validation library</a>.</li>
<li>Added <kbd>add_rule_set</kbd> method to the <a href="libraries/form_validation.html">Form Validation library</a>.</li>
</ul>
</li>
<li>Core
Expand Down
59 changes: 59 additions & 0 deletions user_guide/libraries/form_validation.html
Expand Up @@ -561,6 +561,62 @@ <h2>Callbacks: Your own Validation Functions</h2>
<p><strong>Note:</strong> You can also process the form data that is passed to your callback and return it. If your callback returns anything other than a boolean TRUE/FALSE
it is assumed that the data is your newly processed form data.</p>

<p>You can also keep off or use callback rules that are not directly in your controllers. As in the following sample:</p>

<p>In your model:</p>

<textarea class="textarea" style="width:100%" cols="50" rows="20">&lt;?php

class User_m extends CI_Model {

public function username_check($str)
{
if ($str == 'test')
{
$this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
return FALSE;
}
else
{
return TRUE;
}
}

}
?></textarea>

<p>In your controller:</p>

<textarea class="textarea" style="width:100%" cols="50" rows="30">&lt;?php

class Form extends CI_Controller {

public function index()
{
$this->load->helper(array('form', 'url'));

$this->load->library('form_validation');

$this->load->model('user_m');
$this->form_validation->add_rule_set($this->user_m);

$this->form_validation->set_rules('username', 'Username', 'callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
$this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');

if ($this->form_validation->run() == FALSE)
{
$this->load->view('myform');
}
else
{
$this->load->view('formsuccess');
}
}
}
?></textarea>

<a name="settingerrors"></a>
<h2>Setting Error Messages</h2>

Expand Down Expand Up @@ -1166,6 +1222,9 @@ <h2>$this->form_validation->set_message();</h2>

<p>Permits you to set custom error messages. See <a href="#settingerrors">Setting Error Messages</a> above.</p>

<h2>$this->form_validation->add_rule_set();</h2>

<p>This method lets you add a custom rule set to attending the validations defined from your rules (callback). See <a href="#callbacks">Callbacks</a> above.</p>

<p>&nbsp;</p>

Expand Down