Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Setting validation details dynamically using orm and $_properties
  • This question touches on orm, validation, possibly fieldsets. It has kind of been asked in the past, but I can't find any definitive answers that help. Here is the scenario. I have an orm object containing fields to submit multiple address details. Each address field has a complex list of validation rules - required, max_length, trim, match_pattern etc. The one that is problematic is match_pattern. The matching regular expressions are quite involved, and are different for different types of field. There is one pattern shared by first name and surname. There is another for postal codes, another for address lines 1 to 3, and another for the city fields. In all there are half a dozen RE patterns to be used over twenty fields. Now, the issue. It is easy enough to copy the REs to each field in the _properties array in the orm object, and that works. However, I want to define each RE pattern just once, and refer to it for each field that needs it. The REs will need tweaking, and fixing, so keeping them defined in one place is the way to go. So - how would I do this? Each field that needs an RE pattern set for validation needs to be set programatically, so what is the best place to do it? I need them set on every single instance of the orm object, both for validating forms and for validating before saving to the database. I have tried a number of places, none of which work in all instances. I want to keep the code adding these rules out of the controller, as it is really a part of the orm model, but then gets used by the fieldset/forms/validation class etc. -- Jason
  • How can I do this for 'unique' validation for just a single field in a model? I can't figure what others were doing.
  • There's an example of a unique validation rule in the docs: http://docs.fuelphp.com/classes/validation/validation.html#/extending_validation In case of ORM there's also a third option: you can define the validation rule in the model itself. The rules are the same as for option 2, you have to define the class (in this case your model) as callable: http://docs.fuelphp.com/classes/validation/methods.html#/method_add_callable
  • Yup, I saw that. I actually ended up adding the _validation_method in the model. I understand the add_model method of the Fieldset class will right away add_callable the Model
  • Yes, you are correct.
  • Just in case this helps, ideally each pattern will have its own unique error message when it fails validation. FuelPHP 1.x does not support that - an single error message applies to a whole rule in a form or fieldset validation action, if I understand correctly. So if the best approach to solving this issue is to create a custom rule for each regex pattern, which would then allow me to give each rule its own message, then that could solve two problems in one go. -- JJ
  • Ah, creating a custom rule in an orm model is a lot more straight-forward than the documentation makes it out to be. Since these rules I'm applying are self-contained (don't need to look at anything else, such as the database) and are not shared with any other model, it makes sense to put them all within the model:
        public static $_MyValidationRuleXRegex = '/^[a-z\p{L} +\'\/\\&:,.\n\r-]*$/iu';
    
        public static function _validation_my_rule_x($val)
        {
            return $val === '' || preg_match(static::$_MyValidationRuleXRegex, $val);
        }
       ...
    

    Then any field that needs the validation to be applied simply has this kind of entry in the $_properties array:
            ...
            'MyFieldY' => 
            array (
                'data_type' => 'varchar',
                'label' => 'Field Y Label',
                'null' => true,
                'validation' => 
                array (
                    'required',
                    'max_length' => array(20), // etc.
                    'my_rule_x', // Apply the rule defined higher up in this model.
                ),
                'form' => ...
            ),
            ...    
    

    If this rule fails validation on this field, then the following [defaukt] message is generated:
    Validation rule my_rule_x failed for Field Y Label
    

    When I find where to put my own custom message, so it gets used when validation fails anywhere (form, saving etc), I'll post back here. -- JJ
  • The final part to the solution is the error message. For the above rule, the error message can be placed here: app/lang/en/validation.php
    <?php
    
    return array(
        'my_rule_x' => '":label" contains invalid characters.'
            . 'You may use letters, spaces and special characters /, \\, &, \', - and a full stop/period',
    );
    

    That's my custom rules solved, for this use-case at least. It would have been too easy to follow the documentation and end up going down a much more complicated path. This actually turns out to be a really simple solution, the kind I like best ;-) -- JJ
  • One final thing I need to solve here: how to highlight the fields that are in error. All the validation error messages appear at the top of the form, and the relevant form items below that are not highlighted in any way. I'm guessing there is a way to throw the validation object "at" the form or fieldset to tell it some of those form items are in error and need correcting? That is, some way in a couple of lines of code.
  • I was hoping I could somehow dynamically pull a lang string based on the pattern but ended up doing it your way. Simply brilliant, Jason!

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion