0

Im currently, exploring the validation library that is offered by ci.. and im currently having some trouble. ive tried to do some workarounds but to my disappoint it further messed up my codes. So ill just ask the pro's what i should do.

My view:

<?php echo validation_errors(); ?>
                            <?php echo form_open('bookstore/validateinsert');?>
                                <table cellpadding='4' align='center'>
                                <th>Title</th>
                                    <tr>
                                        <td><input type="text" name="bkname" value="<?php echo set_value('bkname');?>"/></td>
                                    </tr>
                                    <th>Author</th>
                                    <tr>
                                        <td><input type="text" name="bkauthor" value="<?php echo set_value('bkauthor'); ?>"/></td>
                                    </tr>
                                    <th>Released Year</th>
                                    <tr>
                                        <td><input type="text" name="bkyear" value="<?php echo set_value('bkyear'); ?>"/></td>
                                    </tr>
                                    <th>ISBN</th>
                                    <tr>
                                        <td><input type="text" name="bkisbn" value="<?php echo set_value('bkisbn'); ?>"/></td>
                                    </tr>

                                    <tr>
                                        <td><input type='submit' value='insert'/></td>
                                    </tr>
                                    <?php echo form_close();?>
                                </table>

My controller:

public function validateinsert()
    {
    $this->load->library('form_validation');

    $this->form_validation->set_rules('bkname', 'Book Name', 'required|is_unique[books.book_name]');
    $this->form_validation->set_rules('bkauthor', 'Book Author', 'required');
    $this->form_validation->set_rules('bkyear', 'Year Published', 'required|max_length[4]');
    $this->form_validation->set_rules('bkisbn', 'Book ISBN', 'required');

    if ($this->form_validation->run() == FALSE)
        {
            $this->insertbook();
        }
        else
        {
            $this->load->view('showbooks');
        }

    }


    public function insertbook()
    {
        if($_POST)
        {
            $data = array(
                'book_id' => null,
                'book_name' => $_POST['bkname'],
                'book_author' => $_POST['bkauthor'],
                'book_year' => $_POST['bkyear'],
                'book_isbn' => $_POST['bkisbn']
            );


            $duplicate = $this->_searchbook('book_name',$data['book_name']);

            if(!$duplicate)
            {
                $insertid = $this->books_model->insert_books($data);

                if($insertid)
                {
                    $data['success'] = TRUE;
                    $data['message'] = "The book title was successfully added.";
                }

                else
                {
                    $data['success'] = FALSE;
                    $data['message'] = "An error was encountered.";
                }
            }

            else
            {
                $data['success'] = FALSE;
                $data['message'] = "The book title is already in the system";
            }
        }

        $this->load->view('book_entry');
    }

I think that is all there is a need to know about..

The validations work perfect, although i have a question, what is the syntax for all numbers only as i want it included on my isbn and bkyear rules.(P.S. I still prefer javascript with its onblur feature)

THE PROBLEM:

The validations work as i said above, but my insert does not. If there are no rules broken, i calIed the insertbook function but it inserted nothing, as i assumed when i first made the validationfunction as the recipient of the form and i was right, the insertbook probably did not recieve the data in the form. What should i do? P.S. I have an idea in mind that i should just merge those 2 functions together but that would make a mess(or does it matter?). Ill just consult ur opinion on this.

1
  • This will pass the values. You can check by adding a print_r($_POST) in the insert function. May be you have some issues with duplicate check and it doesn't insert. First put the db insert outside the duplicate check and try. Commented Aug 19, 2013 at 6:20

3 Answers 3

3

Your php logic is wrong:

  if ($this->form_validation->run() == FALSE)
    {
        // If validation fails load view
        // $this->insertbook();
        $this->load->view('showbooks');
    }
    else
    {
        //If validation pass insert book
        // $this->load->view('showbooks');
        $this->insertbook();
    }

Also this $duplicate = $this->_searchbook('book_name',$data['book_name']); you can replace with validation rule:

$this->form_validation->set_rules('book_name', 'Name of book', 'is_unique[table.column]');

For your "only numbers problem" ->

$this->form_validation->set_rules('bkyear', 'Year', 'numeric');

Furthermore you can find a lot of build-in rules here: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#rulereference If you need some custom rule you can write your own function. See manual here: http://ellislab.com/codeigniter/user-guide/libraries/form_validation.html#callbacks

Sign up to request clarification or add additional context in comments.

Comments

0
    try this 



        public function validateinsert()
        {
            $this->load->library('form_validation');


        if($this->input->post()) {

            $this->form_validation->set_rules('bkname', 'Book Name', 'required|is_unique[books.book_name]');
            $this->form_validation->set_rules('bkauthor', 'Book Author', 'required');
            $this->form_validation->set_rules('bkyear', 'Year Published', 'required|max_length[4]');
            $this->form_validation->set_rules('bkisbn', 'Book ISBN', 'required');

            if ($this->form_validation->run() != FALSE)
            {
                    $data = array(
                        'book_id' => null,
                        'book_name' => $this->input->post('bkname'),
                        'book_author' => $this->input->post('bkauthor'),
                        'book_year' => $this->input->post('bkyear'),
                        'book_isbn' => $this->input->post('bkisbn')
                    );

                    $this->insertbook($data);
             }
        }
        else
        {
            $this->load->view('showbooks');
        }

    }




public function insertbook($data)
{

        $duplicate = $this->_searchbook('book_name',$data['book_name']);

        if(!$duplicate)
        {
            $insertid = $this->books_model->insert_books($data);

            if($insertid)
            {
                $data['success'] = TRUE;
                $data['message'] = "The book title was successfully added.";
            }

            else
            {
                $data['success'] = FALSE;
                $data['message'] = "An error was encountered.";
            }
        }

        else
        {
            $data['success'] = FALSE;
            $data['message'] = "The book title is already in the system";
        }


    $this->load->view('book_entry');
}

Comments

0

you should look at the other answers because they have pointed out a number of mistakes. if you are doing a database operation - you always have to error check. databases are volatile. its also very easy to make a mistake so you want to isolate database methods.

just wrap an IF around it. whenever possible check for the negative condition first.

if ( $this->insertbook() == false ) 
{ echo 'omg there is an error something is wrong with my insert code' ;
 $this->showError() ;
}
else
{ // no problem
 $this->showSuccess() ;  }

another way to do this is to return the id of the newly created record.

 if ( ! $id = $this->insertbook() ) { echo 'insert failure' ; } 
 else { echo 'my new id is:' . $id ; }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.