Welcome Guest, Not a member yet? Register   Sign In
Form Validation ARGH!!!!
#1

[eluser]Adam Griffiths[/eluser]
Firstly, let me show you my controller code.

Code:
<?php

class Admin extends Controller
{
    function Admin()
    {
        parent::Controller();
        $this->template->type('back');
        $this->template->set('admin');
        $this->template->module('admin');
    }
    
    function add()
    {
        // The below line sets the body_id like <body id="pages"> to allow me to use a nice nav
        $data = array('body_id' => 'pages');
        $this->template->load('header', $data);
        
        $this->form_validation->set_rules('link', 'URL Link', 'callback_link_check');
        $this->form_validation->set_rules('title', 'Page Title', 'trim|required|xssclean');
        $this->form_validation->set_rules('content', 'Content', 'trim|required|xssclean');
        
        if($this->form_validation->run() == FALSE)
        {
            // Set a bunch of variables to send along to the add view
            $add_data = array(
                'headline' => "Hey there, it's great to see you extending your website!",
                'byline' => "Sidenote: The page editor works in much the  same way as your desktop word processor",
                'title' => "Add a Page",
                'action' => "pages/admin/add"
                );

            $this->scaff->set('fresh_pages'); // Set the DB table for my CRUD
            $this->scaff->add($add_data); // Load the add view from the CRUD library with database properties etc...
        } // if
        else
        {
            $data = array(
                'title' => 'Added Page!',
                'message' => 'Congratulations, the page has now been added!'
                );
                
            $this->template->load('message', $data);
        } // else
        
    } // add()
    
    function link_check($str)
    {
        $this->db->where('link', $str);
        $query = $this->db->get('pages');
        
        if($query->num_rows > 0)
        {
            return FALSE;
        }
        else
        {
            return TRUE;
        }
    }
} // admin

/* End of file settings.php */
/* Location: application/modules/admin/controllers/settings.php */

And my form code...

Code:
<div id="top">
    <strong>&lt;?php echo $data['headline']; ?&gt;</strong>
    <p>&lt;?php echo $data['byline']; ?&gt;</p>
</div>

<div class="title">
    
    &lt;?php echo $data['title']; ?&gt;
    
    &lt;?php admin_pages(); ?&gt;
    
</div>

<div class="box">
&lt;?php
echo validation_errors();
echo form_open($data['action'])."\n";
?&gt;
<table cellspacing="2" cellpadding="4">
&lt;?php

foreach($fresh as $fields => $var)
{
    if(strstr($var->name, 'id'))
    {
        
    }
    elseif($var->type === 'blob')
    {
        $data = array(
                        'name'            => $var->name,
                        'value'            => set_value($var->name),
                        'class'            => 'blob'
                    );
        echo('<tr><td valign="top">');
        echo(ucfirst($var->name)."</td><td>".form_textarea($data).echo form_error($var->name)."</td></tr>\n");
    }
    elseif($var->type === 'int' || 'string')
    {
        $data = array(
                        'name'            => $var->name,
                        'value'            => set_value($var->name),
                        'class'            => 'form'
                    );
        echo("<tr><td>");
        echo(ucfirst($var->name)."</td><td>".form_input($data).echo form_error($var->name)."</td></tr>\n");
    }
}

?&gt;
<tr><td colspan="2">&lt;?php echo form_submit('addpage', 'Add Page'); ?&gt;</td></tr>
</table>
&lt;/form&gt;
</div>

<div class="box_bottom"></div>

Ok so the problem is that the form isn't shown on the page, it's blank where the form is supposed to be. I believe I followed the user guide correctly, and cannot see any errors in the code, any ideas?
#2

[eluser]dbashyal[/eluser]
did you turn on error reporting?

by the way isn't it suppose to be $action inplace of $data['action'] in form view and so on...
#3

[eluser]Randy Casburn[/eluser]
Hi Adam,

If what you have your submit button show up but nothing else, it appears to me that your array $fresh is empty. So the foreach() loop never executes and your form elements are never built. That would appear to mean only the form header and submit button are put up there.

Just a guess really. Hope that's helpful.

Randy
#4

[eluser]Adam Griffiths[/eluser]
[quote author="dbashyal" date="1225015422"]did you turn on error reporting?

by the way isn't it suppose to be $action inplace of $data['action'] in form view and so on...[/quote]

It is supposed to be $data['action'] because I pass it through my crud library, it's a little complex to explain without the code but anyways thanks for the input.

[quote author="Randy Casburn"]Hi Adam,

If what you have your submit button show up but nothing else, it appears to me that your array $fresh is empty. So the foreach() loop never executes and your form elements are never built. That would appear to mean only the form header and submit button are put up there.

Just a guess really. Hope that’s helpful.

Randy[/quote]

I just dumped that variable and nothing was outputting from the view, but inside the library it was fine. So I went through my view and took out the parts that fill in the form again if something goes wrong and the variables that show the error and now the form shows. Step 1 complete.

But now when I submit the form nothing happens, it comes back to the form, no errors are shown at the top or anything. Here's the new view code.

Code:
<div id="top">
    <strong>&lt;?php echo $data['headline']; ?&gt;</strong>
    <p>&lt;?php echo $data['byline']; ?&gt;</p>
</div>

<div class="title">
    
    &lt;?php echo $data['title']; ?&gt;
    
    &lt;?php admin_pages(); ?&gt;
    
</div>

<div class="box">
&lt;?php
echo validation_errors();
echo form_open($data['action'])."\n";
?&gt;
<table cellspacing="2" cellpadding="4">
&lt;?php

foreach($fresh as $fields => $var)
{
    if(strstr($var->name, 'id'))
    {
        
    }
    elseif($var->type === 'blob')
    {
        $data = array(
                        'name'            => $var->name,
                        'class'            => 'blob'
                    );
        echo('<tr><td valign="top">');
        echo(ucfirst($var->name)."</td><td>".form_textarea($data)."</td></tr>\n");
    }
    elseif($var->type === 'int' || 'string')
    {
        $data = array(
                        'name'            => $var->name,
                        'class'            => 'form'
                    );
        echo("<tr><td>");
        echo(ucfirst($var->name)."</td><td>".form_input($data)."</td></tr>\n");
    }
}

?&gt;
<tr><td colspan="2">&lt;?php echo form_submit('addpage', 'Add Page'); ?&gt;</td></tr>
</table>
&lt;/form&gt;
</div>

<div class="box_bottom"></div>

Thanks for the input guys it really is appreciated.
#5

[eluser]Randy Casburn[/eluser]
[quote author="Adam Griffiths" date="1225041820"]
But now when I submit the form nothing happens, it comes back to the form, no errors are shown at the top or anything. Here's the new view code.[/quote]

I don't see anything glaring out at me that is wrong with your work now. So it sounds like one of two things are happening then.

1) The form is posting but the $_POST array is not handled or recognized properly for some reason.
-- To check this you can print_r($_POST) or echo $this->input->post('whatever')
-- Do this just above your call to validation->run()

2) The form isn't validating, and the "echo validation_errors();" statement is not displaying the errors for some strange reason.

I would investigate those.

And you're very welcome for the help.

Randy
#6

[eluser]Adam Griffiths[/eluser]
Ok print_r($_POST_; return this.

Quote:Array
(
[link] =>
[title] =>
[content] =>
[addpage] => Add Page
)

Which is the form without anything in it. So it must be that the errors aren't showing, I will investigate.

Hmmm, I have looked at the code, I've changed the fields to be just HTML instead of using the form helper, added the set_value() option aswell as the errors separately next to each field element and on the top of the form and it still won't show any errors.

Code:
<div id="top">
    <strong>&lt;?php echo $data['headline']; ?&gt;</strong>
    <p>&lt;?php echo $data['byline']; ?&gt;</p>
</div>

<div class="title">
    
    &lt;?php echo $data['title']; ?&gt;
    
    &lt;?php admin_pages(); ?&gt;
    
</div>

<div class="box">
    &lt;?php echo validation_errors(); ?&gt;

    &lt;?php echo form_open($data['action'])."\n"; ?&gt;
<table cellspacing="2" cellpadding="4">
&lt;?php

foreach($fresh as $fields => $var)
{
    if(strstr($var->name, 'id'))
    {
        
    }
    elseif($var->type === 'blob')
    {
        echo("<tr><td valign=\"top\">".ucfirst($var->name)."</td><td>&lt;textarea rows=\"12\" cols=\"12\" name=".$var-&gt;name." class=\"blob\">".set_value($var->name)."&lt;/textarea&gt;".form_error($var->name)."</td></tr>\n");
    }
    elseif($var->type === 'int' || 'string')
    {
        echo("<tr><td>".ucfirst($var->name)."</td><td>&lt;input type=\"text\" name=".$var-&gt;name." value='".set_value($var->name)."' />".form_error($var->name)."</td></tr>\n");
    }
}

?&gt;
<tr><td colspan="2">&lt;?php echo form_submit('addpage', 'Add Page'); ?&gt;</td></tr>
</table>
&lt;/form&gt;
</div>

<div class="box_bottom"></div>

This is such a weird error.
#7

[eluser]Randy Casburn[/eluser]
Yea,

At this point you may want to search for 1.7.0 Validation Class problems. There have been a lot of posts about the new class. This is likely beyond my reach as I don't use the validation class too much and haven't looked at the new version at all. I think this is bigger than your code. From what I see you've done everything by the book.

I hope I've helped you some up to this point at any rate.

Randy
#8

[eluser]Adam Griffiths[/eluser]
Ok no problems. Yeah thanks Randy you've been a big help.




Theme © iAndrew 2016 - Forum software by © MyBB