I have tried many different things including adding $this->load->helper(array('form', 'url')); to autoload.php, among other things.
When I fill out a value for first name and last name and nothing else and press submit, it just redirects back to the same page (not to clientRegistrationSuccess.php) as specified in the if{} else{}
Form (View)
echo validation_errors();
echo form_open('index/pages/clientcreation',$CustCreationFormAttr);
echo form_label('First Name: ','fname');
echo form_input($CustCreationFirstName);echo '<br>';
echo form_label('Last Name: ','lname');
echo form_input($CustCreationLastName);echo '<br>';
echo form_label('Title: ', 'title');
echo form_input($CustCreationClientTitle);echo '<br>';
echo form_label('Company: ', 'co');
echo form_input($CustCreationCompany);echo '<br>';
echo form_label('Email: ','email');
echo form_input($CustCreationEmail);echo '<br>';
echo form_submit($CustCreationSubmit,'Submit');
Controller
class Clientcreation extends CI_Controller
{
function index(){
####################################
$this->load->view('navigation');
$this->load->view('pages/clientcreation');
$this->load->view('footer');
####################################
$this->load->helper(array('form', 'url'));
$this->load->library( array('form_validation') );
## Set Validation
$this->form_validation->set_rules('fname', 'First Name', 'required');
$this->form_validation->set_rules('lname', 'Last Name', 'required');
## Perform Validation
if ($this->form_validation->run() == FALSE){
$this->load->view('pages/clientcreation');
}
else{
$this->load->view('pages/success/clientRegistrationSuccess');
}
}
}
Additionally, the form is loading as intended... like:
The paths match up too, because if I change $this->load->view('pages/clientcreation'); to $this->load->view('pages/hi'); it is calling this view. What am I doing wrong here?
This was a silly mistake on my part.
The reason the validation wasn't working is, I was trying to output the validation errors directly in my view, when I should have been outputting on the controller, like:
if ($this->form_validation->run() == FALSE){
echo validation_errors();
$this->load->view('pages/clientcreation');
}
on the controller end, NOT on the view.
(at the top)
Related
Hello friends i have a problem regarding checkbox in codeigniter.I want to send multiple message to different friends by clicking their checkboxes that carries their ids..
THIS IS MY VIEW
<tr>
<td><input type="checkbox" name="friend_id[]" value=<?php echo $friend->id;?>></td>
<td><?php echo $friend->friend_name;?></td>
</tr>
THIS IS MY CONTROLLER
public function message(){
$this->form_validation->set_rules('message','Message', 'required');
$this->form_validation->set_rules('friend_id','Recipients', 'required');
if($this->form_validation->run()){
$data = $this->input->post('friend_id');
echo '<pre>';
print_r($data);
echo '</pre>';
}
else{
echo validation_errors();
}
}
When i run it..i get error showing that Recipient is required while i have checked names.
If you do use an array as a field name, you must use the EXACT array
name in the Helper Functions that require the field name, and as your
Validation Rule field name.
So try:
$this->form_validation->set_rules('friend_id[]','Recipients', 'required');
Otherwise:
public function message(){
$this->form_validation->set_rules('message','Message', 'required');
$this->form_validation->set_rules('friend_id','Recipients', 'callback_arr_required');
if($this->form_validation->run()){
$data = $this->input->post('friend_id');
echo '<pre>';
print_r($data);
echo '</pre>';
}
else{
echo validation_errors();
}
}
public function arr_required($arr) {
if (is_array($arr) && count($arr) > 0) {
return true;
}
$this->form_validation->set_message('arr_required', 'Please select atleast one friend.');
return FALSE;
}
Use this code to test
$this->form_validation->set_rules('friend_id[]', 'Recipients', 'required');
https://www.codeigniter.com/userguide3/libraries/form_validation.html#using-arrays-as-field-names
In codeigniter validation, change the below line:
$this->form_validation->set_rules('friend_id','Recipients', 'required');
to
$this->form_validation->set_rules('friend_id[]','Recipients', 'required');
the issue will get resolved.
For some reason I can't get forms to post in Codeigniter 2.1.4, I honestly have no clue where I am going wrong. Can anyone see any obvious problems? No post data is returned just an empty form the vardump returns (boolean false)
public function testpost()
{
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
var_dump($this->input->post());
$this->form_validation->set_rules('testt', 'Test', 'required');
if ($this->form_validation->run() == FALSE)
{
echo validation_errors();
echo form_open('test/testpost');
echo form_input(array('type'=>'text', 'id'=>'testt'));
echo form_submit(array('value' => 'submit'));
echo form_close();
}
else
{
echo "true";
}
}
You did not create name of text field.
echo form_input(array('type'=>'text', 'id'=>'testt', 'name'=>'testt'));
I have a site develop in codeigniter.
I want to validate a form and display some message.
With my code I don't see any error if my input "name_it" is blank. Why?
This is my controller:
public function nation_create()
{
if($_POST)
{
//salvo i dati
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<p class="error">Errore: ', '</p>');
$this->form_validation->set_rules('name_it', 'name_it', 'required');
if ($this->form_validation->run() !== FALSE){
$this->Nation_model->createNation();
redirect('backend/nation/nation_list/');
}
else{
$data['errors'] = validation_errors();
$this->load->view('backend/include/header_view_logged');
$this->load->view('backend/nation_create_view',$data);
$this->load->view('backend/include/footer_view');
}
}
else{
$this->load->view('backend/include/header_view_logged');
$this->load->view('backend/nation_create_view');
$this->load->view('backend/include/footer_view');
}
}
And this is my view (I have cut more code to read well)
<form action='' method='POST'>
<?php echo validation_errors();
if (isset($errors))
echo $errors;
?>
<?php echo form_error('name_it'); ?>
<input type="text" name="name_it" />
<span id="errorsDiv_name_it"></span>
</form>
Try this:
change:
if ($_POST)
for:
if ($this-input->post())
Info: http://ellislab.com/codeigniter/user-guide/libraries/input.html
You can use the form_validation()->run() to complete this kind of task. It checks if a POST have been sent and if validation_rules returns true.
if($this->form_validation()->run() == false)
{
//load the view
// other stuff you do while POST is not yet sent or if Input from the user returns false
}else{
// post has been sent and validiation is TRUE
// Input is good. . do something
}
The problem was in the class error with attribute display:none
I have function like this:
function gi_insert()
{
if(!IS_AJAX){
$this->load->library('form_validation');
$this->form_validation->set_rules('name', 'Naslov', 'trim|required|strip_tags');
$this->form_validation->set_rules('body', 'Tekst', 'trim|required|strip_tags');
$this->form_validation->set_rules('email', 'E-mail', 'trim|required|strip_tags|valid_email');
$this->form_validation->set_rules('tag', 'Tagovi', 'trim|required|strip_tags');
if(isset($_POST['category_new'])){
$this->form_validation->set_rules('category_new', 'Kategorija', 'trim|strip_tags');
}
$p = $this->uri->segment(3);
if ($this->form_validation->run() == FALSE)
{
$errors = validation_errors();
redirect("admin/create/$p", 'location');
}
else
{
$this->gi->gi_insert();
redirect('admin/pregled/' . $this->uri->segment(3));
}
} else
{
$this->gi->gi_insert();
}
}
How can I send validation errors to admin/create controller? At the moment, this is working, but I don't get error report (page where errors should appear is containig
<?php echo validation_errors(); ?>
)
validation_errors() only works if the $_POST data is still set, since you redirect to admin/create, $_POST is empty and the function no longer works, returning nothing.
You could send the errors with a piece of flashdata to the page.
I am using form validation of CodeIgniter, it works fine but when, form validation fails, it does not display the validation errors, by using
<?php echo validation_errors();?>
I am using
function insertProduct(){
$this->load->library('form_validation');
$this->form_validation->set_rules('pname','ProductName','trimirequired');
if($this->form_validation->run()){
$this->addProduct();
}
else{
$this->load->model('inventory/stock');
}
In your view you should have something like (this example shows errors individually);
<?php echo form_error('p_name'); ?>
<label for="p_name">Product Name</label>
<input type="text" id="p_name" name="p_name" value="<?php echo set_value('p_name'); ?>" />
You need to tell the method in your controller to render a view on success/failure of the form validation.
If you change your insertProduct method to the following, it 'should' solve your issue.
function insertProduct(){
$this->load->library('form_validation');
$this->form_validation->set_rules('pname','ProductName','trimirequired');
if($this->form_validation->run()){
$this->addProduct();
$this->load->view('{name_of_your_view}');
} else{
$this->load->model('inventory/stock');
$this->load->view('{name_of_your_view}');
}
}
Where 'name_of_your_view' is the view that you've placed the validation_errors() code in.
This example from the CodeIgniter tutorial pages explains how to validated submitted data to display validation errors at the header of the form like you might expect:
http://codeigniter.com/user_guide/tutorial/create_news_items.html
The example code for the creation function looks like this:
public function create()
{
$this->load->helper('form');
$this->load->library('form_validation');
$data['title'] = 'Create a news item';
$this->form_validation->set_rules('title', 'Title', 'required');
$this->form_validation->set_rules('text', 'text', 'required');
if ($this->form_validation->run() === FALSE)
{
$this->load->view('templates/header', $data);
$this->load->view('news/create');
$this->load->view('templates/footer');
}
else
{
$this->news_model->set_news();
$this->load->view('news/success');
}
}
As others have said, though, you need to add a view to handle success and return them to the form to display errors on failure.
We can change the line containing following code:
$this->form_validation->set_rules('pname','ProductName','trimirequired');
to:
$this->form_validation->set_rules('pname','ProductName','trim|required');
if($this->form_validation->run($this) == false)
{
$this->addProduct();
}