Custom Validation (Simple Module)

edited August 2011 in Modules
When developing traditional CI projects you can add custom validation callbacks, where the rule is in the appropriate controller file.
How do you implement this when writing a simple module, does the function go in the model file?
From what I have read, you need to add into the 'on_before_validate' method, calling it like;
function on_before_validate($values) { $values['slug'] = url_title($values['name'], 'dash', TRUE); $posted = $_POST; $this->get_validation()->add_rule('icon', 'my_custom_validation', 'Where is your icon?', $posted['icon']); return $values; }

Comments

  • edited August 2011
    I have created a custom validation file in fuel/application/helpers, how do I reference it.
    Also I have a field that is of type file which has been added to my 'required' array, but the required class is not being applied to the label.
  • edited 6:32PM
    You will need to load your custom validation helper like any other helper. The add_rule method looks for a function with that name in the second parameter (or an object => method if the value is an associative array).

    With regards to making it have the asterisk for a required field, you can specify it in your form_fields method instead like so:
    $fields['icon']['required'] = TRUE
    Is the field you want required a field in the table?
  • edited 6:32PM
    Thanks, that works a treat.
Sign In or Register to comment.