Welcome Guest, Not a member yet? Register   Sign In
Show validation errors, well not today????
#1

[eluser]123wesweat[/eluser]
pff, overlooking something

in my view form i have a div
Code:
<div id="errors">
&lt;?php echo validation_errors(); ?&gt;
</div>

and in education controller i have

Code:
if(isset($_POST['addEducation'])) {
    //validate $_POST
    $val = $this->form_validation;
    $val->set_rules('educationName', 'educationName', 'trim|required|xxs_clean');
    $val->set_rules('educationLevel', 'educationLevel', 'trim|required|xxs_clean');

    $this->form_validation->set_error_delimiters('', '<br/>');
    if ($val->run() == FALSE) {
         //echo 'not good';
         redirect('education/form/add');
     } else {
save to db works
}

What am i overlooking/??
#2

[eluser]danmontgomery[/eluser]
You're overlooking the need to provide details about your problem, such as what's happening vs. what you're expecting to happen
#3

[eluser]123wesweat[/eluser]
@noctrum, yep you are right again.

Ok, i will try to explain my little problem

- User clicks on [add education]
- form is displayed
- if it doesn't validate show erros and form again (with previous values)
- else go to overview education

Now in the education controller i have a form function which loads the form view. I have created this function so i wouldn't have to repeat some general data
Code:
function form($action) {
            if ( ! $this->dx_auth->is_logged_in()) {
                $data['msg'] = 'No access';
            } else {
                $data['user_name'] = $this->dx_auth->get_username('username');
                $data['user_id'] = $this->dx_auth->get_user_id('username');
                $data['profile_data']->education_dropdown = $this->user_profile_features->get_education_dropdown();
                $data['action'] = $action;
                $this->load->view('education/view_education_form', $data);
            }
        }
btw: for this form function and all other functions in the education controller i have
Code:
if ( ! $this->dx_auth->is_logged_in()) {
                $data['msg'] = 'No access';
            } else {
}
Can i use a better logic?



I seem to have a fix for my original question by using
Code:
if ($val->run() == FALSE) {
                         echo 'No good';
                        // redirect('education/form/add');
                         //$this->load->view('education/view_education_form');
                         $this->form('add');
                     } else {
#4

[eluser]jbreitweiser[/eluser]
EDIT: You already figured this out.

In the first section your using a redirect when the validation fails. You should be showing the view again with set_value for all the form values so it is the same as when they submitted it. Since you redirect the form is reset and no errors are shown.
#5

[eluser]jbreitweiser[/eluser]
Your solution looks good. I am not sure why you pass $action to the form function because you are not using it. Also, I would check the login before doing anything else. It really should be the first thing you do so no actions are taken by chance. I seems the form function is the last thing you call, so you have done all you actions by that point. Also, you might what to change the

$data['msg'] = 'No access';

to a redirect to a login page. No reason to even show them the page if they don't have access.
#6

[eluser]123wesweat[/eluser]
hmm, still having problems with the correct way to handle a form.

The problem i have now, is the following

From main.php i load a view file (view_book.php) with another view a form view (form_a.php).

Here's a part of view_book code
Code:
<div id="errors">
                                &lt;?php echo validation_errors(); ?&gt;
                                </div>

<div id="form">
&lt;?php
$this->load->view('forms/form_a');
?&gt;
</div>
The form view has a form action to a form controller with a send function

Code:
&lt;form action="/form/send" &gt;

now the send function is just
Code:
function send() {
              //Check if the form has been submitted
        if(isset($_POST['submit'])) {
                $val = $this->form_validation;
                $val->set_rules('name', 'name', 'trim|required');
                if ($val->run() == FALSE) {
                                    //show errors
echo 'errors';  
                                    
                                   $this->load->view('forms/form_a');
//or should i load view_book php ??
                } else {
                    //put in our DB
                                        echo "save in db";
                                        die();
                }
    }
        redirect("/get_book");
        
        }


1/ How do i show the errors and form again in book_view
I am again not getting the error_message

2/can routing be an issue??

3/main controller loads all data from db to parse view_book $data, how can i make sure this $data is being parsed again when i submit the form and view_book is reloaded again???

btw just a simple form (not within a view or nested view) goes ok.
still learing CI
#7

[eluser]jbreitweiser[/eluser]
I think you need to remember that when calling the load->view function, you are basically #including the file at the current position. So the way you have the view set up is the view book with a form view included as well, it is still one page. Its not a seperate page within a page. So what you have to do when the form is submitted is load->viw("view_book.php");. This view will then include the form_a view as well. Since you are only reloading the form_a view, you are not getting the part where the errors are displayed.
#8

[eluser]123wesweat[/eluser]
@jbreitweiser, tx i will try to remember that load->view is kinda "include"

When i load-> view_book
Code:
if ($val->run() == FALSE) {
                                     //show errors
                                   $this->load->view('books/view_book');
                //$this->load->view('forms/form_a');
                } else {

i get the errors (amongst others)
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: metakeywords

Filename: incs/meta.php

Line Number: 3
" />
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: metadescription

Filename: incs/meta.php

Line Number: 4
" />
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: titletag

Filename: incs/meta.php

Line Number: 5
" />

These variables are set in main.php with function show_content();

Function show_content loads the meta, descr, content and VIEW file etc from the db and passed as an array so the vars can be used in the nested views in view_book.php

In view_book.php i got another load view
Code:
<!DOCTYPE html>
&lt;html&gt;
&lt;head&gt;
&lt;?php
$this->load->view('incs/meta');
?&gt;
&lt;/head&gt;
&lt;body&gt;

This my simplied show_content function
Code:
function show_content($content_id = 1){
        $this->load->helper(array('form','url'));
        $this->load->library("form_validation");
        $this->load->model('content/content_model','');
        $options = array ('content_id' => $content_id);
        $showContent = $this->content_model->get_content($options);
        
        $this->data = array (
            'titletag'         => $showContent["content_title"],
            'metadescription'       => $showContent["metaDescr"],
            'metakeywords'          => $showContent["metaKeywords"],
            'imgSRC' =>$showContent["imgSRC"],
            'imgAlt' => $showContent['imgAlt'],
            'imgTitle' => $showContent['imgTitle'],
            'cssXtra' => $showContent['contentCategory'],
        );
        //view also from db
        $this->load->view($showContent["content_tmpl"], $this->data);
    }

and $showContent["content_tmpl"] = 'view_book'

Now i don't have the
Code:
$showContent = $this->content_model->get_content($options);
etc
in form.php, but i thought (like you said) load->view was an include and i wouldn't have to get the data from the db again????

Should i use ob_start() etc

Please, advice

oh btw, between all the errors and the ridiculous looking page i do see the error messages.
#9

[eluser]jbreitweiser[/eluser]
You need to get all the information again from the db. Seperate out the common variables from the action specific ones. Then add the specific variables, like the name of the included view, in the action function.

Code:
function show_content($content_id = 1){
    $this->load->helper(array('form','url'));
    $this->load->library("form_validation");
    $this->load->model('content/content_model');
    $showContent = $this->content_model->get_content($content_id);
    $this->load->view($showContent["content_tmpl"], $showContent);
}

This can be put in the model as well. Keep Your controller simple.

Code:
function get_content($content_id = 1){
    
    $options = array ('content_id' => $content_id);
    $showContent = $this->get_content($options);
    $data = array (
        'titletag'         => $showContent["content_title"],
        'metadescription'       => $showContent["metaDescr"],
        'metakeywords'          => $showContent["metaKeywords"],
        'imgSRC' =>$showContent["imgSRC"],
        'imgAlt' => $showContent['imgAlt'],
        'imgTitle' => $showContent['imgTitle'],
        'cssXtra' => $showContent['contentCategory'],
        'content_tmpl' => $showContent["content_tmpl"],
    );
    return $data;
}
#10

[eluser]123wesweat[/eluser]
so this means there is a "xtra" query each time the form doesn't validate??

is this correct in the get_content function??
Code:
$options = array ('content_id' => $content_id);
$showContent = $this->get_content($options);
is it possible to call the same function within a function???[recursion and it will only call itself ones, right]
When do you think about using a recursion?


my get content function
Code:
function get_content($options=array()){
        $query = $this->db->where('id', $options['content_id'])->get('books');
        $result = $query->row_array();
        return $result;
    }

tx man, gonna try your suggestion and see if i can get it to work.
Quote:Keep Your controller simple.
I will try, i always start simple but....




Theme © iAndrew 2016 - Forum software by © MyBB