Validation within workflow in Joomla component with MVC pattern - php

I'm writing a custom component for the Joomla CMS using the MVC pattern. I'm implementing a simple workflow, where I want the user to enter some data into a view, validate this data (on server side, this is not about client side/JavaScript validation) and redirect the user to another view, if the validation succeeded. If the data is invalid, the first view should be displayed again with the invalid fields marked.
Now I'm not sure, where to place the validation code: My first thought was to place it in the controller, since this one has to decide whether to redirect to the second view or not. But I found a couple of advices telling me not to place validation code in the controller.
Placing it into the model of the first view would also be an option. But since in Joomla there's a 1-to-1 relationship between model and view, I'm not sure if it's ok to only create a model class (because in case of successful validation, the first view is not shown again).
Is there any standard way to do this? Or is this kind of behaviour implemented in some standard component, so I could look for the source code? Any ideas (some pseudo-code would be nice and sufficient...)?
Thanks for any help!

In my projects, I put them in the Form model (using HTML_QuickForm2).
Semi code:
$form = new HTML_QuickForm2();
$form->addElement('text')->addRule('email');
if ($form->validate()) {
echo "all fine, storing in database";
} else {
//error, let's try it again
echo $form;
}
This way, you have all the rules and validations in your form object. Move the form initialization code into an own class extending HTML_QuickForm2, and you're set.

Related

Validation Typo3 Flow

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

PHP MVC Form Submission

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

MVC PHP Design Registration Form

I am building a simple user registration form using the MVC design pattern, in PHP.
Can someone please advise if I have selected the correct areas for code implementation, as after some fair amount of reading I understand people often interpret the docs in different ways.
I understand there may be no wright / wrong answer here, but am just trying to get some feedback on how the majority would implement it.
Many thanks.
Model
Function containing PDO mysql insert query
View
HTML form markup with PHP self action and controller include
Controller
Data validation and php include of model function
yup there are different approaches to MVC... your concepts look correct except for the view which should not contain the controller include. It is the controller that includes the model and view. Furthermore, i would have put validations in the model itself.
Model : validation functions(return true or false if error) and insert query
View : html form only with data passed from controller
Controller: main entry point/file (e.g. registration.php)
The controller is the entry point which is where you include your view and model.
Basically in the controller,
you check if form is submitted or not.
If not submitted, display the view (i.e. the form)
if data submitted, do validation using model's validation functions.
If validation ok, execute sql in your model and redirect to success page or display a success message.
If validation is not correct, display your view and fill in your form with the submitted data.
But as i said, there are different approaches to MVC; e.g. you can do all validations in the controller itself instead of the model like you proposed.

MVC - error messages

I'm creating MVC web application.
In which part of MVC (model-view-controller), I should save my error messages, that can appear where something goes wrong and user should read them?
Situation at the moment:
Model:
function f()
{
$data[error] = "Error message"
return $data;
}
View:
echo $error
My opinion would be to have them at the model's end. For me, the controller just acts as a middleman, and the view should not have to do much thinking..
The VIEW in my code simply prepares the front end output, after being given the data it needs to display in the page. It doesn't have to do any thinking, and all validations are in the model already, so it just has to focus on displaying results, forms, information, help, hint, etc.
The CONTROLLER in my code just does the communication for the user (client's browser requests), the model (which does all the thinking) and the view (which does all the 'showing'). It wouldn't know that there are errors in the input coming from the user; it merely passes the input to the model. When the model's done, the controller just passes that output to the view.
The MODEL in my code is responsible for doing all the thinking. It receives the input from the user (via the controller) and does what it needs to do with it. For any or all results and/or errors it generates, it just passes it to the view (again, via the controller) and expects that the view knows what to do: to display the data.
You could possible throw an exception in your controller, and then catch them in your view handler.
A generic pattern would be:
Define in Model.
Generate in Controller.
Display in View.
You could store the error messages in session or pass them directly from controller to view.

What's an elegant way to handle an invalid form in a MVC application controller?

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.

Categories