Prevent from changing the page when there are form validation errors - codeigniter

I have few forms with several validation rules in a separated file named form_validation.php. One of them is an edit form that has the URL /usuario/painel/editar that receives the user id from the session, populating the form without the need to pass the id by GET, for security reasons.
The problem is that when I submit the form, the action is different, and when there are any errors, the URL changes to the action, making several errors to show when rendering the form, since the view is different and there are no id to receive.
<form action="/auto/usuario/edit/<?php echo $id_usuario; ?>" method="POST" id="editar_formulario_fisica_1" enctype="multipart/form-data">...</form>
For example: localhost:8000/auto/usuario/edit/8
Is there a way to refresh the same page and the same form, and show the errors? Or perhaps to prevent the URL from changing when there are errors?

put the id in a hidden form field
echo form_hidden('userid',$id_usuario);
then get the user id in your controller or model
$userid = $this->input->post( 'userid') ;
edit ======
yes the URL can now be the same, because you are not adding the user id to the end of it. you don't need to because the user id is being passed in a hidden form field.
the form itself is in a view file. to show the form, you are first getting the user id, then you are calling/loading the form view file. in a similar way, if the form validation fails, then you are showing the form view file again. this all happens in your controller. also if you use codeigniter form helper, and form validation, it makes showing the error messages, etc, much easier.

Related

PowerApps Portal--> After Record is created (Insert Form) show that record as Edit Form

I have a Insert form for Account Entity.
Once user create Record, I want to move to another page and show same record as Edit Form.
Does anyone have idea way to achieve this?
You should be able to redirect the user to desired page using OnSuccess of form submission.
On success: Select one of the following options:
Show success message: Requires a message to be displayed to the
user on successful submission of the form. You can also select Hide
form on success to hide the form upon successful submission.
Redirect to webpage: Redirects the user to the selected webpage
in the portal. You must select a webpage from the Redirect to webpage
list.
Redirect to URL: Redirects the user to the specified URL. You
must enter a URL in the Redirect to URL field.
Read more
Out of Box configuration helps!!!
There is a field on "Entity Form" records named "Append Record ID to Query String". This is to configure passing the id of the record to the page after form submission. This screenshot shows it in the Redirect Section in the classic interface
You can do this several ways. What I do, is use Navigate() to go to the new screen, and I pass in a context variable to the new edit screen.
Assuming you're using OnSelect() on a button to call SubmitForm(), you can do the Navigate() call via the OnSuccess() property of the form.
And within Navigate(), you just pass whatever data you need to in order for the next screen to properly bind it to the Edit Form. This is the UpdateContextRecord optional param. You'd have the Edit form take its values from a Context Variable.
Passing variables between screens has been discussed here, as well.

After submitting the form, I want to stay at the same page and display the data

When I fill my employee form and performance details and click "to submit" button, I want it to show all the data on the same page and I want the form to become empty so that I can refill the form for the same employee again.
screenshot
Use redirect_to :back in your controller to, what for it, redirect back to the previous page (or more accurately HTTP_REFERER)
To ensure the form data is blank, the model it is rendered on should have blank fields with something like #performance_report = PerformanceReport.new.
If you want to show both the old data and the blank form, you will need your controller to create two instance variables, one showing previous data, and one empty.

asp.net mvc - Data lost on post back

I have a form that only has a checkbox. If the checkbox is not checked then the page won't be valid and won't submit the data. On the view I have out putted some fields from the model object, just to let the user know what they are deleting. When I click the Submit button and the model state is not valid the page will show the validation error but the data from the model is no longer there!
Does anyone know how I can persist this data without making another call to the database to populate the model object?
Thanks
ASP.Net MVC will not magically persist model info.
You need to either include it in hidden fields in the form (which an attacker can modify), or re-fetch it from the database.
If you don't repopulate the Model from the DB, you have to postback all the data, using hidden fields for example.
Anyway, I don't understand how the model validation could fail, being just a checkbox :/.

CodeIgniter validation errors specific to a form with multiple forms on a page

I have a set of tabbed forms with validation and shared fields like title, description and tags that submit to the same controller method. I have separate validation and all that worked out and the tab with the form you submitted is active when you come back.
Here is the trick, right now since CI errors are a global function when I print them out with "echo form_error('title');" it shows the error on all the tab forms even though you only submitted one. For usability I want to limit the error to just the form submitted.
One way would be to use whatever logic that you're using to show the active tab to hide the errors that are appearing on the fields in the inactive tab.
Another way would be to prepend the rules and field names with a identifier of the form that submitted them. Something like...
in controller...
$this->form_validation->set_rules("{$tab}_title", 'Title', 'required|trim');
in view...
<?= form_open("{$tab}_title", set_value($this->input->post("{$tab}_title"))) ?>
<?= form_error("{$tab}_title") ?>

How to disable the display of validation errors in view (Cakephp)

I have a view which is containing 5 forms submitting separately but to the same model. The aim of this is allow the user to fill all the forms in the same page and be able to quickly compare the values he puts in for each form. Each form has most of the fields in common with another one. If you want a idea of the stuff, imagine that you are a soccer trainer and you want to be able on the same page to set up your team tactics for each half time. So I'll have a HalfTime model with two forms on the same page, one for each half time
So when I submit Halftime 1 form and that the validation fails, the validation errors of HalfTime1 is displayed in HalfTime2 fields too. I would like to be able to disable the validation of one form according if it wasn't submited.
My idea was to send a variable from the controller to the view containing the name/id of the current half time and from this variable, displaying validation errors only in the ccorrect form.
Do you have any idea on how I can disable the display of the validation errors in one field?
Thank you a lot!
Set the error option to false when you use form input. ie:
echo $this->input('Halftime2.title', array('error' => false));

Resources