I have a question about the procedure of the flow validation.
When I submit my form and then flow shows a validation error for example for the url input field without deleting all the already inserted user inputs from the fields.
How does flow keep the field filled out?
Which methods / classes are involved? Is there a way I can influence / copy this behaviour?
By default your controller extends \TYPO3\Flow\Mvc\Controller\ActionController, and by default in case there are validation errors errorAction() from this controller is called. Inside there is forwardToReferringRequest() just read the code to get it.
If you want to change this behaviour you can override errorAction() in your controller (same as any other protected/public method). You can also change $errorMethodName and handle it using different action on your controller level. Check this example https://wiki.typo3.org/Flow_Cookbook#handling_validation_errors_that_occur
I have a question related to form submission done in PHP application that's built in MVC architecture (self-written framework).
All examples that I've seen so far (including existing back-end frameworks) work this way that once form for adding record to database is submitted then certain method of controller is executed [say i.e. addRecord()], which triggers method of appropriate model. If everything goes OK then record is added and controller's method [addRecord() in this example] renders view of "index" page that displays table with records from database.
What I would like to achieve is to render view with form used to add records (the same that I used to add first record) instead of "index". Obviously I can do it easily by just rendering appropriate view from addRecord() (view with the form).
But the tricky point is when you check url you'll see the following:
The first time you enter it will be i.e.
http://project_name/my_controller/create
Once first form was submietted and you return to the view from addRecord() method then url will be:
http://project_name/my_controller/addRecord
What I would like to see is return to the original url, that is http://project_name/my_controller/create
Not sure if this is clear?
PS. Of course I could use AJAX call for form submission (that way I will stay at the same page) but perhaps it's possible to achieve the same without AJAX.
Thanks in advance,
On the controller you will want to submit to the addRecord route and do the processing. Have a check to make sure it was successful and on successful submission you can redirect back to the create route.
It is hard to give an example since you are using a custom made framework. I use slim which has a redirect method for a route. If what you have made does not have something like that then using should do the trick.
header('Location: '.$createUrl);
die(); //or exit
In all laravel tutorials, they use the POST method for forms. For searching, I thought it was better to have the FORM use GET instead, so that as soon as the the form gets submitted, the query string is appended to the URL, and the controller can directly work on that.
If its a POST method, then the form is submitted to an intermediate controller, which processes it and redirects to some result controller. And if an intermediate controller is not used, the search strings will not be available in the URL anymore. I want to have my query string in the URL for various reasons.
Is it ok if I use the GET method in laravel forms?
{{ Form::open('search', 'GET') }}
Or are there any specific benefits of using the POST method?
Here is an example application built on laravel:
http://www.bootsnipp.com
The search page does not append any query strings. I don't the search to be like this.
Based on experience for searching always use GET. and for hidden data use post for example Login.
Even google us using get method for searching.
Laravel provides the options for controllers to be RESTful. By turning RESTful on, your controller can responds to RESTful commands such as POST, GET, DELETE and PUT
Using the Form::open() method, you can pick any of the four options. If you decide to use GET - then your get_method() will be called. If you decide to use POST - then your post_method will be called.
Following general RESTful commands, you would use POST to create a record. As you are retrieving a record, GET is the appropriate choice.
I have a custom MVC application/framework where each action is a function inside of a controller class.
I have a signup action which renders a view with a form requesting user details. On submit it posts to a processSignup action that validates the data and either inserts the user into the database and redirects to a success page or needs to redisplay the original view/form with the errors.
The signup action runs several queries and hands off a variety of data to the view. E.g. it needs to get a list of available countries that the user can choose in a drop-down, retrieve a list of suggested usernames based on the name they entered on a previous form, and retrieve other data from a model.
I'm trying to find out how I can avoid repeating all these queries and the render of the view in my processSignup action in the event of an error. What's the standard way to handle these situations? The signup action receives POST data from a previous form so I can't have the signup form submit to itself and branch based on GET vs POST.
I could just call the signup function directly and have it receive an optional parameter to differentiate between normal requests and ones for invalid forms but that seems hackish. How do other frameworks handle this scenario? I'm using PHP but this is more of a high-level question than one specific to the language.
Avoiding duplicate code certainly isn't unique to MVC frameworks. The standard approach would be to simply create a private function that both signup and processSignup call before rendering the view. Something like:
private function setCommonVars()
{
// set country, username arrays, etc...
}
As far as rendering the signup view, the flexible thing about MVC is that you can reuse views. Your framework should have a way of explicitly choosing a view within a controller method (e.g., $this->view = 'signup'). That way, if an error occurs in processSignup, you can choose the signup view before rendering.
I have several questions regarding CI application design.
Q. When creating a new form and your using CI's form_helper I'm creating arrays in the controller and passing it to the view/form_input() method. Should I be doing this in the controller, the view, or a separate file?
Q. In my controller, I create a method for my form i.e., new_user() and in my view/form_open() I specify a different method in my controller to handle the action (i.e., add(), edit(), delete() ..etc) & that method handles the validation. This is the way that I perfer; however, I've had a lot of difficulty passing the data around if the validation fails. Any suggestions?
Q. I have an instance or two that when I perform form validation I need to validate against two $_POST variables. An example would be, on validation I need to query the database to determine if the entered business already exists (based off business name and zip code) then redirect back to the view and persist the post variables. So far I haven't been able to find a way to create a custom callback function to do this because you can only pass in one parameter. The only way that I've been able to get this to work is if validation passes, I then perform the database check and if the business exists I put the $_post in session/flashdata and use redirect to load the view again. The array that defines the form_input attributes calls set_value for that is where it pulls the flashdata for each record in the array.
$data['name'] = array(
'name' => 'name',
'id' => 'name',
'value' => set_value('name', $this->session->flashdata('name')),
'maxlength' => '200',
'size' => '79',
'class' => 'text'
I realize that this really comes down to preferences; however, I'm really wanting to gain some insight on what pitfalls I can expect and how others are designing their applications. I've downloaded sample applications and I've dome a good amount of searching but I really haven't found much discussion. Any suggestions are greatly appreciated.
Thank you!
I'll share my approach using CI
I create Controller as slim as possible. The controller main job will only get parameter through URI, _GET, and _POST. Then controller will pass required parameter to models, and get the result. After that, view file will be loaded and all variables required by the view will be passed.
All process logic, related with database, email sending, etc, is handled in the model. Model will get parameter, do query, process query result if needed, then return an array, resultset, boolean, or integer. Controller that get the returned value pass it directly to view, without reprocessing it.
In view, it will process the variable in order to displaying it. There will be loop to display list of data, get the column field from array then display it as a form default value, etc. View and model often developed in pair, because all needed field in view must be provided by the query in the Model.
The only 'fat' processing in Controller is the form_validation. I have answer it in your other question, how I wrote my form_validation rules and how to use it.
Below is my answers for your question above:
Q. When creating a new form and your
using CI's form_helper I'm creating
arrays in the controller and passing
it to the view/form_input() method.
Should I be doing this in the
controller, the view, or a separate
file?
I rarely using form_helper. This is because most of my view is came from fellow designer or provided by client as the HTML file. I only use form_dropdown because it's allow me to pass options as an array, instead of do foreach. For the other form element, I just use the one presented in the template file.
Q. In my controller, I create a method
for my form i.e., new_user() and in
my view/form_open() I specify a
different method in my controller to
handle the action (i.e., add(),
edit(), delete() ..etc) & that method
handles the validation. This is the
way that I perfer; however, I've had a
lot of difficulty passing the data
around if the validation fails. Any
suggestions?
When I create my application, I often only have 2 main methods in controller. admin is for displaying list and handle delete, and form to display and handle add and edit. Let me give example with a product module.
I will have product controller with these methods:
class Product extends MY_Controller {
function index()
{
//for front page, display list of product
}
function detail()
{
//for front page, display single product detail
//product id is passed as 3rd URI segment
$id = intval($this->uri->rsegment(3));
}
function admin()
{
//for admin, display product list
//receive id in _POST then do delete
//after delete, do redirect to self, best practise
}
function form()
{
//for admin, handle add and edit
$id = intval($this->uri->rsegment(3));
//if id given and product detail data can be loaded, then it in 'edit' mode
//else it in 'add' mode
//after validation success, and insert/update success, redirect to product/admin
}
}
Using this approach, I can avoid duplicate code and can maintain all code to always up to date. Almost all add & edit have same view and form field. In case add & edit form differ (such as edit user, do not allow changing username), by have $mode variable set to either add or edit, I can put simple if and display correct form, validation rules, and call appropriate model metods.
Q. I have an instance or two that when
I perform form validation I need to
validate against two $_POST variables.
An example would be, on validation I
need to query the database to
determine if the entered business
already exists (based off business
name and zip code) then redirect back
to the view and persist the post
variables. So far I haven't been able
to find a way to create a custom
callback function to do this because
you can only pass in one parameter.
The only way that I've been able to
get this to work is if validation
passes, I then perform the database
check and if the business exists I put
the $_post in session/flashdata and
use redirect to load the view again.
The array that defines the form_input
attributes calls set_value for that is
where it pulls the flashdata for each
record in the array.
You can create your own validation rules. To pass more than one parameter, you can open the file system/libraries/Form_validation.php then see the function matches($str, $field) code. Your callback can have more than 1 parameter, and function matches($str, $field) code will show you how to read and use the second parameter.
I hope this will help you in learning and using CI. Waiting great web application from you ;)
Q. When creating a new form and your using CI's form_helper I'm creating arrays in the controller and passing it to the view/form_input() method. Should I be doing this in the controller, the view, or a separate file?
A. Form_Helper should be always use in "view".
Q. In my controller, I create a method for my form i.e., new_user() and in my view/form_open() I specify a different method in my controller to handle the action (i.e., add(), edit(), delete() ..etc) & that method handles the validation. This is the way that I perfer; however, I've had a lot of difficulty passing the data around if the validation fails. Any suggestions?
A. My way is direct add/edit() to a save();, in save() method I do if else for both cases.
Q. I have an instance or two that when I perform form validation I need to validate against two $_POST variables. An example would be, on validation I need to query the database to determine if the entered business already exists (based off business name and zip code) then redirect back to the view and persist the post variables. So far I haven't been able to find a way to create a custom callback function to do this because you can only pass in one parameter. The only way that I've been able to get this to work is if validation passes, I then perform the database check and if the business exists I put the $_post in session/flashdata and use redirect to load the view again. The array that defines the form_input attributes calls set_value for that is where it pulls the flashdata for each record in the array.
A. I recommend u use $this->input->post instant of using $_POST, because CI will help u filter XSS if u enable it.
PHP didn't support is_POST like .NET, what I do is use a textbox as reference
if(isset($_POST('txt_Name')))
{
}
**or**
if($this->input->post('txt_Name'))
{
}
Hope my answer able to help you.