How to get validation errors in codeigniter?












0















Hello I am inserting data in database. When I insert both category and description then data in inserting but when I don't insert in the category and description input and click on create then no error showing with blank page admin/category/ register_category, I want to show that category and description field should not be empty.



category.php view page is below :



<?php if(isset($_SESSION['success'])){ ?>
<div class="alert alert-success"><?php echo $_SESSION['success']; ?>
</div>
<?php } ?>
<?php echo validation_errors('<div class="alert alert-danger">','</div>'); ?>

<form class="form" action="<?php echo site_url('admin/category/register_category') ?>" method="POST">
<label for="contactinput5">Category Name</label>
<input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
<label for="contactinput5">Discription</label>
<textarea class="form-control border-primary" type="text" placeholder="discription" name="discription" id="contactemail5"></textarea>
<button type="submit" name="create" class="btn btn-primary">


and my controller Category.php page is:



<?php
class Category extends CI_Controller {
function index() {
$this->load->view('admin/category');
}

function register_category() {
$this->form_validation->set_rules('category', 'Category', 'required');
$this->form_validation->set_rules('discription', 'Discription', 'required');
if($this->form_validation->run() == TRUE){
echo "form validate";
$this->load->model('categories');
$insert_category = $this->categories->validate();
if($insert_category){
$this->session->set_flashdata("success","Your data has been added");
redirect("admin/category","refresh");
}
else{
redirect('admin/category');
}
}
}
}
?>


model categories page:



<?php
class Categories extends CI_Model
{
function validate()
{
$arr['categoryname'] = $this->input->post('category');
$arr['discription'] = $this->input->post('discription');
return $this->db->insert('category',$arr);
}
}
?>









share|improve this question





























    0















    Hello I am inserting data in database. When I insert both category and description then data in inserting but when I don't insert in the category and description input and click on create then no error showing with blank page admin/category/ register_category, I want to show that category and description field should not be empty.



    category.php view page is below :



    <?php if(isset($_SESSION['success'])){ ?>
    <div class="alert alert-success"><?php echo $_SESSION['success']; ?>
    </div>
    <?php } ?>
    <?php echo validation_errors('<div class="alert alert-danger">','</div>'); ?>

    <form class="form" action="<?php echo site_url('admin/category/register_category') ?>" method="POST">
    <label for="contactinput5">Category Name</label>
    <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
    <label for="contactinput5">Discription</label>
    <textarea class="form-control border-primary" type="text" placeholder="discription" name="discription" id="contactemail5"></textarea>
    <button type="submit" name="create" class="btn btn-primary">


    and my controller Category.php page is:



    <?php
    class Category extends CI_Controller {
    function index() {
    $this->load->view('admin/category');
    }

    function register_category() {
    $this->form_validation->set_rules('category', 'Category', 'required');
    $this->form_validation->set_rules('discription', 'Discription', 'required');
    if($this->form_validation->run() == TRUE){
    echo "form validate";
    $this->load->model('categories');
    $insert_category = $this->categories->validate();
    if($insert_category){
    $this->session->set_flashdata("success","Your data has been added");
    redirect("admin/category","refresh");
    }
    else{
    redirect('admin/category');
    }
    }
    }
    }
    ?>


    model categories page:



    <?php
    class Categories extends CI_Model
    {
    function validate()
    {
    $arr['categoryname'] = $this->input->post('category');
    $arr['discription'] = $this->input->post('discription');
    return $this->db->insert('category',$arr);
    }
    }
    ?>









    share|improve this question



























      0












      0








      0








      Hello I am inserting data in database. When I insert both category and description then data in inserting but when I don't insert in the category and description input and click on create then no error showing with blank page admin/category/ register_category, I want to show that category and description field should not be empty.



      category.php view page is below :



      <?php if(isset($_SESSION['success'])){ ?>
      <div class="alert alert-success"><?php echo $_SESSION['success']; ?>
      </div>
      <?php } ?>
      <?php echo validation_errors('<div class="alert alert-danger">','</div>'); ?>

      <form class="form" action="<?php echo site_url('admin/category/register_category') ?>" method="POST">
      <label for="contactinput5">Category Name</label>
      <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
      <label for="contactinput5">Discription</label>
      <textarea class="form-control border-primary" type="text" placeholder="discription" name="discription" id="contactemail5"></textarea>
      <button type="submit" name="create" class="btn btn-primary">


      and my controller Category.php page is:



      <?php
      class Category extends CI_Controller {
      function index() {
      $this->load->view('admin/category');
      }

      function register_category() {
      $this->form_validation->set_rules('category', 'Category', 'required');
      $this->form_validation->set_rules('discription', 'Discription', 'required');
      if($this->form_validation->run() == TRUE){
      echo "form validate";
      $this->load->model('categories');
      $insert_category = $this->categories->validate();
      if($insert_category){
      $this->session->set_flashdata("success","Your data has been added");
      redirect("admin/category","refresh");
      }
      else{
      redirect('admin/category');
      }
      }
      }
      }
      ?>


      model categories page:



      <?php
      class Categories extends CI_Model
      {
      function validate()
      {
      $arr['categoryname'] = $this->input->post('category');
      $arr['discription'] = $this->input->post('discription');
      return $this->db->insert('category',$arr);
      }
      }
      ?>









      share|improve this question
















      Hello I am inserting data in database. When I insert both category and description then data in inserting but when I don't insert in the category and description input and click on create then no error showing with blank page admin/category/ register_category, I want to show that category and description field should not be empty.



      category.php view page is below :



      <?php if(isset($_SESSION['success'])){ ?>
      <div class="alert alert-success"><?php echo $_SESSION['success']; ?>
      </div>
      <?php } ?>
      <?php echo validation_errors('<div class="alert alert-danger">','</div>'); ?>

      <form class="form" action="<?php echo site_url('admin/category/register_category') ?>" method="POST">
      <label for="contactinput5">Category Name</label>
      <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
      <label for="contactinput5">Discription</label>
      <textarea class="form-control border-primary" type="text" placeholder="discription" name="discription" id="contactemail5"></textarea>
      <button type="submit" name="create" class="btn btn-primary">


      and my controller Category.php page is:



      <?php
      class Category extends CI_Controller {
      function index() {
      $this->load->view('admin/category');
      }

      function register_category() {
      $this->form_validation->set_rules('category', 'Category', 'required');
      $this->form_validation->set_rules('discription', 'Discription', 'required');
      if($this->form_validation->run() == TRUE){
      echo "form validate";
      $this->load->model('categories');
      $insert_category = $this->categories->validate();
      if($insert_category){
      $this->session->set_flashdata("success","Your data has been added");
      redirect("admin/category","refresh");
      }
      else{
      redirect('admin/category');
      }
      }
      }
      }
      ?>


      model categories page:



      <?php
      class Categories extends CI_Model
      {
      function validate()
      {
      $arr['categoryname'] = $this->input->post('category');
      $arr['discription'] = $this->input->post('discription');
      return $this->db->insert('category',$arr);
      }
      }
      ?>






      php codeigniter codeigniter-3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 8:34









      barbsan

      2,38321222




      2,38321222










      asked Nov 15 '18 at 8:03









      rahulrahul

      488




      488
























          5 Answers
          5






          active

          oldest

          votes


















          1














          Hello Please update your function in the controller. There is issue in validation condition Changes TURE to FALSE. Check below code.



          function register_category()
          {
          $this->form_validation->set_rules('category', 'Category', 'required');
          $this->form_validation->set_rules('discription', 'Discription', 'required');

          if ($this->form_validation->run() == FALSE)
          {
          $this->load->view('category');
          }
          else
          {
          $this->load->model('categories');
          $insert_category = $this->categories->validate();

          if($insert_category)
          {
          $this->session->set_flashdata("success","Your data has been added");
          redirect("admin/category","refresh");
          }
          else
          {
          redirect('admin/category');
          }
          }
          }





          share|improve this answer































            1














            hey guys thanks and i got my answer just by putting this code



            if($this->form_validation->run() == FALSE)
            {
            $this->index();
            }





            share|improve this answer
























            • see my answer as to why this works, and another solution

              – Alex
              Nov 15 '18 at 8:52



















            0














            If validation result is not true, you can get errors from $this->form_validation->error_array(), loop the return array and show the error to the user.



            Hope this help.






            share|improve this answer































              0














              Get all post validation errors in controller :
              echo validation_errors();

              If you want to show at the end of textbox use this following method :

              <label for="contactinput5">Category Name</label>
              <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
              <?php echo form_error('category', '<div class="error">', '</div>'); ?>





              share|improve this answer































                0














                FYI your solution only worked because validation_errors() only applies to the current instance. When you redirect that information is lost. You would have to store it in session variable or this is common (change form action to self or leave blank):



                function index() {
                if ($_POST) {
                $this->form_validation->set_rules('category', 'Category', 'required');
                $this->form_validation->set_rules('discription', 'Discription', 'required');
                if($this->form_validation->run() == TRUE){
                $this->load->model('categories');
                $insert_category = $this->categories->validate();
                if($insert_category){
                $this->session->set_flashdata("success","Your data has been added");
                }
                }
                }
                $this->load->view('admin/category');
                }


                Of course your way works too if you are ok with the url changing.






                share|improve this answer























                  Your Answer






                  StackExchange.ifUsing("editor", function () {
                  StackExchange.using("externalEditor", function () {
                  StackExchange.using("snippets", function () {
                  StackExchange.snippets.init();
                  });
                  });
                  }, "code-snippets");

                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "1"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314834%2fhow-to-get-validation-errors-in-codeigniter%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  1














                  Hello Please update your function in the controller. There is issue in validation condition Changes TURE to FALSE. Check below code.



                  function register_category()
                  {
                  $this->form_validation->set_rules('category', 'Category', 'required');
                  $this->form_validation->set_rules('discription', 'Discription', 'required');

                  if ($this->form_validation->run() == FALSE)
                  {
                  $this->load->view('category');
                  }
                  else
                  {
                  $this->load->model('categories');
                  $insert_category = $this->categories->validate();

                  if($insert_category)
                  {
                  $this->session->set_flashdata("success","Your data has been added");
                  redirect("admin/category","refresh");
                  }
                  else
                  {
                  redirect('admin/category');
                  }
                  }
                  }





                  share|improve this answer




























                    1














                    Hello Please update your function in the controller. There is issue in validation condition Changes TURE to FALSE. Check below code.



                    function register_category()
                    {
                    $this->form_validation->set_rules('category', 'Category', 'required');
                    $this->form_validation->set_rules('discription', 'Discription', 'required');

                    if ($this->form_validation->run() == FALSE)
                    {
                    $this->load->view('category');
                    }
                    else
                    {
                    $this->load->model('categories');
                    $insert_category = $this->categories->validate();

                    if($insert_category)
                    {
                    $this->session->set_flashdata("success","Your data has been added");
                    redirect("admin/category","refresh");
                    }
                    else
                    {
                    redirect('admin/category');
                    }
                    }
                    }





                    share|improve this answer


























                      1












                      1








                      1







                      Hello Please update your function in the controller. There is issue in validation condition Changes TURE to FALSE. Check below code.



                      function register_category()
                      {
                      $this->form_validation->set_rules('category', 'Category', 'required');
                      $this->form_validation->set_rules('discription', 'Discription', 'required');

                      if ($this->form_validation->run() == FALSE)
                      {
                      $this->load->view('category');
                      }
                      else
                      {
                      $this->load->model('categories');
                      $insert_category = $this->categories->validate();

                      if($insert_category)
                      {
                      $this->session->set_flashdata("success","Your data has been added");
                      redirect("admin/category","refresh");
                      }
                      else
                      {
                      redirect('admin/category');
                      }
                      }
                      }





                      share|improve this answer













                      Hello Please update your function in the controller. There is issue in validation condition Changes TURE to FALSE. Check below code.



                      function register_category()
                      {
                      $this->form_validation->set_rules('category', 'Category', 'required');
                      $this->form_validation->set_rules('discription', 'Discription', 'required');

                      if ($this->form_validation->run() == FALSE)
                      {
                      $this->load->view('category');
                      }
                      else
                      {
                      $this->load->model('categories');
                      $insert_category = $this->categories->validate();

                      if($insert_category)
                      {
                      $this->session->set_flashdata("success","Your data has been added");
                      redirect("admin/category","refresh");
                      }
                      else
                      {
                      redirect('admin/category');
                      }
                      }
                      }






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '18 at 8:33









                      Praveen KumarPraveen Kumar

                      335213




                      335213

























                          1














                          hey guys thanks and i got my answer just by putting this code



                          if($this->form_validation->run() == FALSE)
                          {
                          $this->index();
                          }





                          share|improve this answer
























                          • see my answer as to why this works, and another solution

                            – Alex
                            Nov 15 '18 at 8:52
















                          1














                          hey guys thanks and i got my answer just by putting this code



                          if($this->form_validation->run() == FALSE)
                          {
                          $this->index();
                          }





                          share|improve this answer
























                          • see my answer as to why this works, and another solution

                            – Alex
                            Nov 15 '18 at 8:52














                          1












                          1








                          1







                          hey guys thanks and i got my answer just by putting this code



                          if($this->form_validation->run() == FALSE)
                          {
                          $this->index();
                          }





                          share|improve this answer













                          hey guys thanks and i got my answer just by putting this code



                          if($this->form_validation->run() == FALSE)
                          {
                          $this->index();
                          }






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 15 '18 at 8:29









                          rahulrahul

                          488




                          488













                          • see my answer as to why this works, and another solution

                            – Alex
                            Nov 15 '18 at 8:52



















                          • see my answer as to why this works, and another solution

                            – Alex
                            Nov 15 '18 at 8:52

















                          see my answer as to why this works, and another solution

                          – Alex
                          Nov 15 '18 at 8:52





                          see my answer as to why this works, and another solution

                          – Alex
                          Nov 15 '18 at 8:52











                          0














                          If validation result is not true, you can get errors from $this->form_validation->error_array(), loop the return array and show the error to the user.



                          Hope this help.






                          share|improve this answer




























                            0














                            If validation result is not true, you can get errors from $this->form_validation->error_array(), loop the return array and show the error to the user.



                            Hope this help.






                            share|improve this answer


























                              0












                              0








                              0







                              If validation result is not true, you can get errors from $this->form_validation->error_array(), loop the return array and show the error to the user.



                              Hope this help.






                              share|improve this answer













                              If validation result is not true, you can get errors from $this->form_validation->error_array(), loop the return array and show the error to the user.



                              Hope this help.







                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 15 '18 at 8:24









                              Van ThoVan Tho

                              846




                              846























                                  0














                                  Get all post validation errors in controller :
                                  echo validation_errors();

                                  If you want to show at the end of textbox use this following method :

                                  <label for="contactinput5">Category Name</label>
                                  <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
                                  <?php echo form_error('category', '<div class="error">', '</div>'); ?>





                                  share|improve this answer




























                                    0














                                    Get all post validation errors in controller :
                                    echo validation_errors();

                                    If you want to show at the end of textbox use this following method :

                                    <label for="contactinput5">Category Name</label>
                                    <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
                                    <?php echo form_error('category', '<div class="error">', '</div>'); ?>





                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      Get all post validation errors in controller :
                                      echo validation_errors();

                                      If you want to show at the end of textbox use this following method :

                                      <label for="contactinput5">Category Name</label>
                                      <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
                                      <?php echo form_error('category', '<div class="error">', '</div>'); ?>





                                      share|improve this answer













                                      Get all post validation errors in controller :
                                      echo validation_errors();

                                      If you want to show at the end of textbox use this following method :

                                      <label for="contactinput5">Category Name</label>
                                      <input class="form-control border-primary" type="text" placeholder="category" name="category" id="contactinput5">
                                      <?php echo form_error('category', '<div class="error">', '</div>'); ?>






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 15 '18 at 8:26









                                      ManiMani

                                      34726




                                      34726























                                          0














                                          FYI your solution only worked because validation_errors() only applies to the current instance. When you redirect that information is lost. You would have to store it in session variable or this is common (change form action to self or leave blank):



                                          function index() {
                                          if ($_POST) {
                                          $this->form_validation->set_rules('category', 'Category', 'required');
                                          $this->form_validation->set_rules('discription', 'Discription', 'required');
                                          if($this->form_validation->run() == TRUE){
                                          $this->load->model('categories');
                                          $insert_category = $this->categories->validate();
                                          if($insert_category){
                                          $this->session->set_flashdata("success","Your data has been added");
                                          }
                                          }
                                          }
                                          $this->load->view('admin/category');
                                          }


                                          Of course your way works too if you are ok with the url changing.






                                          share|improve this answer




























                                            0














                                            FYI your solution only worked because validation_errors() only applies to the current instance. When you redirect that information is lost. You would have to store it in session variable or this is common (change form action to self or leave blank):



                                            function index() {
                                            if ($_POST) {
                                            $this->form_validation->set_rules('category', 'Category', 'required');
                                            $this->form_validation->set_rules('discription', 'Discription', 'required');
                                            if($this->form_validation->run() == TRUE){
                                            $this->load->model('categories');
                                            $insert_category = $this->categories->validate();
                                            if($insert_category){
                                            $this->session->set_flashdata("success","Your data has been added");
                                            }
                                            }
                                            }
                                            $this->load->view('admin/category');
                                            }


                                            Of course your way works too if you are ok with the url changing.






                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              FYI your solution only worked because validation_errors() only applies to the current instance. When you redirect that information is lost. You would have to store it in session variable or this is common (change form action to self or leave blank):



                                              function index() {
                                              if ($_POST) {
                                              $this->form_validation->set_rules('category', 'Category', 'required');
                                              $this->form_validation->set_rules('discription', 'Discription', 'required');
                                              if($this->form_validation->run() == TRUE){
                                              $this->load->model('categories');
                                              $insert_category = $this->categories->validate();
                                              if($insert_category){
                                              $this->session->set_flashdata("success","Your data has been added");
                                              }
                                              }
                                              }
                                              $this->load->view('admin/category');
                                              }


                                              Of course your way works too if you are ok with the url changing.






                                              share|improve this answer













                                              FYI your solution only worked because validation_errors() only applies to the current instance. When you redirect that information is lost. You would have to store it in session variable or this is common (change form action to self or leave blank):



                                              function index() {
                                              if ($_POST) {
                                              $this->form_validation->set_rules('category', 'Category', 'required');
                                              $this->form_validation->set_rules('discription', 'Discription', 'required');
                                              if($this->form_validation->run() == TRUE){
                                              $this->load->model('categories');
                                              $insert_category = $this->categories->validate();
                                              if($insert_category){
                                              $this->session->set_flashdata("success","Your data has been added");
                                              }
                                              }
                                              }
                                              $this->load->view('admin/category');
                                              }


                                              Of course your way works too if you are ok with the url changing.







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 15 '18 at 8:51









                                              AlexAlex

                                              6,18042361




                                              6,18042361






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53314834%2fhow-to-get-validation-errors-in-codeigniter%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  這個網誌中的熱門文章

                                                  Tangent Lines Diagram Along Smooth Curve

                                                  Yusuf al-Mu'taman ibn Hud

                                                  Zucchini