It looks like you're new here. If you want to get involved, click one of these buttons!
FuelCMS documentation regarding construction of forms using form_builder is extensive, but it is not quite clear how to add validation to the form in the proper way (so that the error messages be included in the generated form code).
Could someone provide/point to an example of how it is to be done? Can the Codeigniter form validation class be used, or only the fuelCMS's own validator class? (I know CI form validation class can be used in the standard codeigniter way, but then the error messages are produced outsite of the form_builder functionality (and therefore need a separate piece of code to be shown in the view)).
Comments
You can use CI's own form validation, however, Form_builder has a "set_validator" and "get_validator" methods on it that return FUEL's Validator class instance. You can do something like the following:
http://docs.getfuelcms.com/libraries/validator
So in your
The form_builder will automatically highlight each field with an error and print the errors at the top of the page if the page doesn't redirect and is posted to itself with errors.
Thank, you, I will try it out tonight. I understand there is no way to use the default CI form validation class and get the form_builder functionality of "highlighting each field with an error" at the same time?
Hm, I get an error with the suggested code:
Fatal error: Call to a member function add_rule() on null
What could be wrong? I did load the Validator library, though the error does not change if I do not load it (so I guess the Form_builder loads it for me).
Try loading the validator library before that:
This does not help. In my controller function create() that I run I currently have this:
and if I add $this->load->library('validator'); before var_dump, the output does not change at all - it includes numerous references to the validator object, see here: https://pastebin.com/u6K0Czgq
Here is the complete class: https://pastebin.com/Dax6ht9F
Try loading the validator first. I'm wondering if it's an ordering thing. Form_builder uses the Form class which on initialization will grab a reference to the validator object if it's loaded, but if it's not then it will be null.
If I raise
above
I get a blank page (and I have
error_reporting(-1);
at the top of index.php file.
Could it be that the form_builder functionality is sort of abandoned since there is module able to do more than the form_builder?
The form module actually uses the form_builder library. Do you have the following set too?
yes, I have ini_set('display_errors', 1) in case of development environment, and echo ini_get('display_errors'); echoes 1
Anyway, I have reduced the controller class to bare minimum, with no dependencies in the code, and still get the same error. Could you try it out and see what you get? It drives me nuts... (on pastebin (https://pastebin.com/pH7NxsPp) or below):
You need to move
$this->load->library('validator');
above$this->load->library('form_builder');
. The reason you are seeing a blank screen after that is because the code is working. You are checking for a $_POST variable which will be empty if you are just browsing to that page. You will need to submit a form that has a method of POST for that controller method to echo out anything.Thank you, really so.
I finally figured out how validator works (outside of the form_builder thus far). Here is a pure validator example, without dependencies on any other code. Would you consider to include an example like this in the documentation? I think it would make things so much clearer for the beginners of programming.
By the way, the http://docs.getfuelcms.com/libraries/validator definition of add_rule incorrectly explains (mixes up) the parameters thus:
Below is a working example of a form with validation. I got the errors to display above the fields with $this->form_builder->display_errors = TRUE;
The only thing I have not yet figured out is - how do I get the form to highlight the fields with errors?
The controller file:
The view file:
Is it perhaps a styling thing and are there classes being applied to the fields already and it just needs to be styled?
Thank you, it really is a styling thing: the fields with errors have a span element with class error_highlight. Once you add css
the fields get highlighted.