I have an action/view that is going to be used for reporting purposes, no saving of data involved.
I want some form elements to be valid/invalid and if invalid, some errors to be shown.
What is the most CakePHPish way for showing errors in form fields that are not based on a model?
Even if you're not getting or saving your data from a database table, you still need a model for validation rules, that's the proper MVC way. You can set
var $useTable = false;
In your model if you're not planning on saving/reading anything. You can then use your model to just set your validation rules and messages.
You can validate your data without saving, within the controller like so:
$this->ModelName->validates()
For more information, please refer here: Validating Data from the Controller
Related
I have a form with a ViewModel and several (maybe prefilled) fields. One of the fields, a checkbox, is only accessable by the admin and not present for normal users. I create my fields with #Html.TextboxFor(...).
When a normal user submits the form all data will be passed correctly, but the value for the admin-field in the Model has the default value (false). If the value was true before the action, the value will be changed,but the user shouldn't be able to do this.
How can I submit the value from the given ViewModel, which the Controller sends to the View for prefilling, if there is no Checkbox?
Note:
I don't want to create two forms for normal users and admins.
A hidden field for the checkbox value also can be changed by the user.
Without code, it's difficult to say exactly where your problem lies, but in general, if you're handling the post data properly, you won't have any issues.
You mention a view model, but if you are in fact using a view model, it's unclear why you're having problems. If you don't want the boolean reset by the post data, then simply don't map that property over to your entity class. If you're using some sort of automatic mapping that does this, then you should set that property to be ignored, so that it's not included.
Additionally, you should make sure you're starting with the existing entity, pulled fresh from the database, which would in this case have the boolean set to true. Then, when you map over the values from your view model (excluding this boolean property), it will remain true. If you're creating some new instance based on the view model, there's no way to know it was initially true.
I have a very long order form that enables saving drafts. If saved as draft, only order name is required but when actually placing an order a more thorough validation is required. I implemented this by using different validation groups. When editing the order I display two buttons: "Save draft" and "Place order". Each of them performs validation using a different validation group.
But now I would like to make a button on the list of orders which enables to change order status from 'draft' to 'placed' directly. To do so, validation must be performed without displaying edit form and submitting it. I would just like to validate the entity that is already in the database. I can use the validator service and everything is simple as long as the data is valid. But in case data isn't valid, I would like to redirect user to the edit form with fields with missing data highlighted. The idea seems to load data from database into the form and run validation as if that data were sent using a browser but execution of this doesn't seem to be trivial because Symfony2 triggers validation on form only when binding the request.
I was going through the Symfony source code and found s class called Symfony\Component\Form\Extension\Validator\EventListener\ValidationListener. It seems to attach itself on the FormEvents::POST_SUBMIT event. Is there a way to trigger this event manually from the controller without request binding? Or are there any alternative approaches to my problem?
Just to point out the correct answer already given by Matjaž Drolc in the comments:
If you want to validate a form without getting the data from the request, you have to call the form->submit() function, because Symfony does not validate the fields if they are not marked as submitted, which is done by this function.
Call the function like this
$form->submit(array(), false);
With an empty array as the submitted data and not clearing the missing fields.
Is it possible in Codeigniter to run a multi-step Form validation, such that, when an error has been found, the form validation will stop (not validating the other Form validation rules)?
With the set_rules() and run() method of the Form_validation class, all the rules are checked even if it does not pass the first one.
Or is it better to validate those criteria in my model in the particular order (such that it stops on a validation error) and pass the single error message to my view?
Ralph, I am not sure you can do it with standard CI validation library.. You may need to extend/overwrite it in order to achieve what you want. You can display a first error message individually, but in the background the full data check would run.
Depending on how complex the data validation you need, you could also get some jQuery plugin to roughly check the data before the form is submitted. This will reduce the risk of bad data being sent and may reduce the overhead on server side.
I'm a little bit confused about Filtering and Validation, particularly the sequence that should be used.
When you are processing user generated data to be stored in Database, do you filter first, then validate, or the other way around?
Filtering may correct the error that would make data valid, so if you filter first, the invalid data may become valid but it differs from the original user input.
If you validate first, and you know data is valid for sure, then why even bother filtering it?
First you have to use validate all data and then save it to your database.
So assume I have a class that has an init method that does something like... grabs some data off the net in xml format and parses it to initialize some of its properties. My concern is how should I handle the case where the network is down or the xml data my object receives is bad?
Normally in C i would use return values to indicate an error and what kind and then that would get propagated back till I could report it to the user. I don't really think that will work in this situation.
Use asynchronous network requests.
Create the UI and show it with
either dummy replacement for the
actual values (like pictures) or no
data (for example empty table).
Then create and send the request for
data and register handler that gets
called with data.
When you receive data your handler
gets called with them.
You parse the data and update the
UI. In case of data being invalid
you can now update UI to inform the
user.
You can use timeouts to cancel
requests in case of network problem
and functions not returning with
data within specific time.
There was an example in last year Stanford's CS193p class (iPhone programming but the same applies to desktop apps) with showing empty user interface and updating it when data coming back. You can probably find references to it on net or otherwise there'll be new example this year.
For network down you have a few options
Alert the user you cannot retrieve the needed data
Show stale (last loaded, maybe not stale?) data
For Bad Data:
Alert the user
Try again
Show old data
Try to fix the data (missing a closing tag? etc)
Show a subset of the data (maybe you can extract something that is usable?)
As far as error codes, you can do:
Return codes ie bad_data -1, no_network -2, etc.
You can throw exceptions, catch them and map them to user friendly display messages