1

I am relatively new to Craft CMS and have been using the formBuilder2 plugin to create forms. No problem this far. I have been able to use ElementApi to output the forms into Json. Still no problem. My issue starts when I have to add validation to my form fields. I have read the "The Definitive Guide to Craft Validation for Plugin Developers (The Book)" and I am still very much confused as to how I actually implement validation on specific field in my form. In a nutshell, how can I add a email validator to an field that I named email. Also, how do I implement this to the fieldset if all I have to go with is:

        {% set fields = tab.getFields() %}
        <fieldset class="{{ tab.name |lower|replace(' ', '-') }}">
            <legend>{{ tab.name }}</legend>
            {% for field in fields %}
            {% set input = craft.formBuilder2.getInputHtml(field) %}
            {{ input | raw }}
            {% endfor %}
        </fieldset>
        {% endfor %}

Not to sound silly but why, why, why why???? Please can someone help me....

1 Answer 1

1

The controller you're posting to is responsible for the validation. In the case of the entries/saveEntry controller for example there's an Entry model populated and then run through the validation. Each model attribute and all the fields (content) have to give their ok for the complete model to validate correctly.

For the fields it's the field type's attribute type and an optional validate method that are responsible. Have a look at this plugin by Marion Newlevant, it provides a simple field type with configurable validation and is probably a good example for what you're trying to achieve.

https://github.com/marionnewlevant/craft-mn_matchinput

5
  • Hi CarlCS, thanks for guiding me. I installed the MNmatchInput plugin and the concept is what I am looking for. It has now added a new level of complexities. The plugin works. I am unsure of how to get the new fieldtype to display on my front-end form. When I use the codeBlock from my previous message all the other fields display except the "matchInput" fieldType. If I call it up directly using something like this "fieldLayout.getFieldLayout().getFields()" I lose all validation and the form sends the email with a blank email field. If you could please assist It would deeply be appreciated. Commented May 19, 2016 at 11:25
  • Sorry, can't really assist as I've never used a plugin like formbuilder and always build the forms manually, similar to how it's described in the Craft documentation craftcms.com/docs/templating/entry-form
    – carlcs
    Commented May 19, 2016 at 11:48
  • Thanks for assisting me tho. I've actually just gone ahead and created my forms manually. Read through most of the documentation that you suggested and understand have a better understanding of how to build the forms from scratch. I've discovered that this gives me a better understanding of the mechanics of craft. Who knows, maybe in the future I might built a plugin to simplify building forms for craft. Commented May 20, 2016 at 8:57
  • That's great, Gevann. Do your fields use the validation rules and return error messages how you'd like them to do? Everything solved (except your custom plugin part, hehe)?
    – carlcs
    Commented May 20, 2016 at 9:02
  • For now the fields are using basic validation rules and it returns error messages that I can deal with. I would like to get more into detailed validation as time goes on but I am happy with the results I have for now. It takes up a bit of my time as I am still learning craft but I want to get this project over and done with. When I have some free time I can do all the nitty gritty things. Commented May 20, 2016 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.