Welcome Guest, Not a member yet? Register   Sign In
Form validation in same page / url
#1

[eluser]Pintossauro[/eluser]
Hi all,

In my application i have Projects that can have Tasks. I created a form to add a Task in a Project. I also configured routes.php:

Code:
$route['([0-9]+)/tasks/add'] = "task/add/$1";

(the first argument is the project id) and i have a link in the projects page:

Code:
anchor($project_id.'/tasks/add', 'Add new task')

With this the user will be redirected to my <b>add($project_id)</b> function in my Task controller which will load the 'task/add' view with the correspondent project information and the form to add the new task (the url in the browser will still be, e.g., "4/tasks/add").

Then when the user submits his information i have a function <b>"insert_task"</b> in my Task controller that validates the data.

If all OK insert data in the database and load the view with the list of tasks in the project. If not i wanted the user to see
the <b>'4/tasks/add'</b> view again with the errors but if i do <b>$this->load->view('task/add')</b> like i did in my function "add" the url will be <b>.../index.php/task/insert_task </b> and not <b>../index.php/4/tasks/add</b> as i wanted.


Maybe i'm not understanding the MVC pattern correctly or am i using two functions (add - to load the form view - and insert_task - to validate and insert the task in
the DB) when not necessary.

Does someone have an suggestion how to solve this?

Thanks for your attention,
Pintossauro.
#2

[eluser]Pintossauro[/eluser]
Maybe this is confusing. I'll try to simplify:

Basically I access a URL like <b>index.php/4/tasks/add</b> by a link with the <b> href="/4/tasks/add">Add task</b> that calls the Add function in the Task controller passing the project id as argument (due to the definition in routes.php). When a user submits a new task, if the <b>validation in insert_task() method</b> fails, i wanted to show the same URL <b>index.php/4/tasks/add</b> with the input errors but if i just load the same view in my validation function (<b>$this->load->view(task/add)</b>) the URL will be <b>index.php/task/insert_task</b> and not the <b>index.php/4/tasks/add</b> that i want.

This is just a matter of the "aspect" of the URL because the application works perfectly.

Any ideas?
#3

[eluser]danmontgomery[/eluser]
The form should be submitting to current_url(), if validation is successful, redirect, if not, show the form again with validation errors.




Theme © iAndrew 2016 - Forum software by © MyBB