I'm trying to use Codeigniter's form validation to check if the text input from a modal window is redundant with what's already in the database. I can't seem to get far enough under the hood the Codeigniter's set_rules to see what's happening. I hope one of you code animals can help me.
Ajax sends text to a function in the Controller which picks it up as $value and then in the Controller I want to check if this string is already in a table.field of the DB and if so (not) send $unique=false (true) back to the jQuery. The problem is the set_rules statement always returns true regardless of what's in the DB.
Here's the Controller:
public function change_options()
{
# Receives and sends back user-entered new option and adds it to database
# Get strings from ajax in view
$value = $_POST['new_option'];
$value_class = $_POST['new_option_class'];
//echo $value_class; die;
#Make array to send to model
$value_array = array('Options' => $value);
# Make sure option is unique
$this->load->library('form_validation');
//$this->form_validation->set_rules('Options', 'Option', 'is_unique['.$value_class.'.Options]');
$this->form_validation->set_rules('add-new-submit', 'Option', 'is_unique['.$value_class.'.Options]'); <---- This is always true
if ($this->form_validation->run() == TRUE) // Only add new option if it is unique
{
# Add new option to dropdown in database
$this->load->model('data_model');
$this->data_model->add_option($value_class, $value_array);
$unique = true;
}
else
{
# Send string back to view via ajax
$unique = false;
}
echo json_encode(array($value, $unique));
}
I've tried everything I can think of for the first slot of set_rules: 'Options', 'add-new-text', 'add-new-submit', (each of these last two are names in the modal html) $value_class (which is the id for form element in question). Here's the modal for completeness:
<!-- add-new field -->
<div class="modal small hide fade" id="add-new" tabindex="-1" role="dialog" aria-labelledby="add-new-fieldLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">√ó</button>
<h3 id="add-new-fieldLabel">Add New Field</h3>
</div>
<div class="modal-body">
<p>Would you like to add a new item?</p>
<input type="text" id="add-new-text" name="add-new-text" placeholder="Type the new option">
</div>
<div class="modal-footer">
<button type="button" class="btn btn-success" id="add-new-submit" name="add-new-submit"/>Add</button>
</div>
</div><!-- /add-new field -->
Anybody see what's going on?
I figured out how to do this and posted the code in AJAX not posting or receiving apparently . No need to use Form Validation!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.