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

[eluser]jbreitweiser[/eluser]
My cut and paste error. Call the function something else like get_page_vars or what ever makes sense to you. The $this->get_content call inside the function is calling the function you already have to read the information from the database. I was writing it as if it was moved to the content_model class.

And yes, you do need to hit the database each time the page is submitted unless you write all the values to a hidden field. I would read it from the database each time to avoid any security issues with cross site scripting.
#12

[eluser]123wesweat[/eluser]
[quote author="jbreitweiser" date="1266577016"] The $this->get_content call inside the function is calling the function you already have to read the information from the database. [/quote]

hmm, but that's exactly what function get_content already does.

I am also not sure how i would get the right $content_id again? Sould i add this as a hidden field in the form???

Maybe i could do this in the controller
Code:
$data['showContent'] = $this->content_model->get_content($options);;
//view also from db
$this->load->view($showContent["content_tmpl"], $data);

instead of

Code:
$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);

But how would i echo the vars which haven't got the same name in View, like
Code:
titletag != content_title
#13

[eluser]jbreitweiser[/eluser]
Your redone function is good. I would do it that way as well. Less code is better. You could add the code below to copy the strings you need to a new key in the array. I don't know your app but I would keep this bit of code in a separete function because you need to call it from both the show and send functions. No need to have the same code twice if you don't have to.

Code:
$data = $this->content_model->get_content($options);
$data['titletag'] = $data["content_title"];
$data['cssXtra'] => $data['contentCategory'],
//view also from db
$this->load->view($data["content_tmpl"], $data);

And you do need to have a hidden field on your form to pass the content ID back or bake it into the url.
#14

[eluser]123wesweat[/eluser]
hi,

i am slowly, slowly getting it. I have started another thread related to this as i am not sure how to "stay" on the same url after a form validation.

In my example i indeed have a hidden field.
city2go.net/test/banksy <- id1
city2go.net/test/futura <- id2
#15

[eluser]jbreitweiser[/eluser]
Make you post tag have action="city2go.net/test/banksy/1". Use the URI class to get that segment. So in this example test is the class and banksy is the method you are calling. URI is a default class so you do not need to load it. refer to http://ellislab.com/codeigniter/user-gui...s/uri.html for how to write urls with parameters.

Code:
class Test extends Controller{
    function banksey(){
        $content_id = $this->uri->segment(3, 1);  //  The second parameter is the default value if the uri segment is not found
        ..do something here with $content_id
    }
}

As for staying on the same url, I am assuming your form is posting back to the same url you called in the first section. If not I would think about doing it that way. Its easier to display to initial page and deal with the form submission in the same controller. Otherwise you will have messy redirects or other work arounds.
#16

[eluser]123wesweat[/eluser]
hmm not quit how i have set it up

/test/banksy is a seo url
in routes.php i have
Code:
$route['test/banksy'] = "testForm/show_content/1";

So in fact i have a testForm class with a show_content method
and i get the hidden content id just by
Code:
function show_content($content_id = 1) {
        $data['hiddenContentID'] = $content_id;

}

About the staying on the same url, i am NOT (Sad) and yes i am looking into redirects.

I am a bit confused when i give the form the same action i.e. test/banksy then testForm/show_content will be called each time.

Should i make an conditional in method show_content to see if isset($_POST["submit"])???

the thing is as i am planning to have more forms i thought about using a form controller, now i am not sure.
#17

[eluser]123wesweat[/eluser]
hmm, didn't know form validation could give me so much !#@$13.

Anyhow still in my test site i do have a sort of solution but it doesn't look pretty

in my testForm controller i have
Code:
function show_content($content_id = 1) {
        
        $options = array ('content_id' => $content_id);
        $data['showContent'] = $this->test_formc_model->get_content($options);
        $data['hiddenContentID'] = $content_id;
        if(!isset($_POST['submit'])) {
            $this->load->view('testfolder/view_test_mainD', $data);
            } else {
            $this->_runForm();
            }
        }
        function _runForm(){
            $this->load->helper(array('form','url'));
            $this->load->library("form_validation");
            $this->load->model('forms/test_formc_model');
            
            $content_id = $this->input->post('hidden_content_id');
            $from_url = $this->input->post('from_url');//i don't use this one
            $options = array ('content_id' => $content_id);
            $data['showContent'] = $this->test_formc_model->get_content($options);
            $data['hiddenContentID'] = $content_id;
            
            $val = $this->form_validation;
    
            $val->set_rules('fname', 'fname', 'trim|required');
            $val->set_rules('lname', 'lname', 'trim|required');
            $val->set_rules('item', 'item', 'trim|required');
            $val->set_rules('email', 'email', 'email|required|valid_email');
    
            if ($val->run() == FALSE) {
                //show errors
                $this->load->view('testfolder/view_test_mainD', $data);
            } else {
                    //put in our DB
                    $res = $this->test_formc_model->insert_data();
                    if($res == 1) {
                        
                       $test = $this->load->view('success','',true);
                       $data['msg'] = "success".$test;
                    
                       $this->load->view('testfolder/view_test_mainD', $data);
                        //return false;
                        
                    } else {
                        $msg = 'hmm, db off.';
                    }
            }
        }

in view_test_mainD i had to add a conditional
Code:
if(!isset($msg)){
$this->load->view('forms/view_test_formD');
} else {
echo $msg;
}

and in view_test_formD i have set the form action to

Code:
&lt;?php echo form_open(current_url(), 'id="form"')?&gt;

going to have a cup of tea and come back to see if i understand it.
#18

[eluser]jbreitweiser[/eluser]
OK. Are you flexible with the database structure. I think the hard coding in the routes table is the problem here. What if you could search by the book. So instead of

$route['test/banksy'] = "testForm/show_content/1";

use

$route['test/(:any)'] = "testForm/show_content/%1";

This would translate 'test/banksy' to "testForm/show_content/banksy"

So you could search for banksy by using the uri class

$book_name = $this->uri->segment(3);

Then you could add a route for the submit form

$route['test/submit/(:any)'] = "testForm/submit/%1";
$route['test/(:any)'] = "testForm/show_content/%1";

and could validate and update from the submit function. Does this make sence.
#19

[eluser]123wesweat[/eluser]
hmm,

i see what you doing but i do have some doubts with your suggestion
mainly
1
Code:
$route[‘test/submit/(:any)’] = “testForm/submit/%1”;
$route[‘test/(:any)’] = “testForm/show_content/%1”;
Don't let me stay on the same page (/test/submit/ != /test/banksy)


2 i don't like to get the content of the db by string, i am more likely to choose for id as it's unique. also when editing the content in the db banks y != banksy !=

btw i am not creating a search form.


here's what i have so far
Code:
function show_content($content_id = 1) {
/*
    show_content method returns a view
    - gets an array of content objects used in our views
    - checks if content has a form to display
    - checks if a form is submitted    
*
* @param content_id get the content from db
*
* @return view
*/
            $options = array ('content_id' => $content_id);
            $data['showContent'] = $this->test_formc_model->get_content($options);
            $data['hiddenContentID'] = $content_id;
                        
             $contentWithForms = array('1','2');//also from a db table field
            
             if (in_array($content_id,$contentWithForms))
                  {
                        if(!isset($_POST['submit'])) {
                            $this->load->view('testfolder/view_test_mainD', $data);
                        } else {
                            $this->_processForm();
                        }
                  }    else {
                      $data['msg']= "no form";//test message
                    $this->load->view('testfolder/view_test_mainD', $data);
                  }        
        }
#20

[eluser]jbreitweiser[/eluser]
I get the unique ID. The only thing I would change is the check for content_id. Do the search and then check if you got a return unless you want to restrict it to the 2 values.

Code:
function show_content($content_id = 1) {
/*
    show_content method returns a view
    - gets an array of content objects used in our views
    - checks if content has a form to display
    - checks if a form is submitted    
*
* @param content_id get the content from db
*
* @return view
*/
            $options = array ('content_id' => $content_id);
            $data['showContent'] = $this->test_formc_model->get_content($options);
            $data['hiddenContentID'] = $content_id;
                        
             $contentWithForms = array('1','2');//also from a db table field
            
             if ($data['showContent'])
                  {
                        if(!isset($_POST['submit'])) {
                            $this->load->view('testfolder/view_test_mainD', $data);
                        } else {
                            $this->_processForm();
                        }
                  }    else {
                      $data['msg']= "no form";//test message
                    $this->load->view('testfolder/view_test_mainD', $data);
                  }        
        }

I would also separate the show_content from the form process with a route. It just makes it easier to track down problems and also keeps the different action seperate. But if what you have works for you then it is right. There is always more then one way to get work done.

Code:
$route[‘test/banksy/submit/’] = “testForm/submit/1”;
$route[‘test/banksy’] = “testForm/show_content/1”;




Theme © iAndrew 2016 - Forum software by © MyBB