fORMValidation with regex / Help Please!

fORMValidation with regex

Is there anyway to use fORMValidationaddValidValuesRule with a regex? If not any clues on the best alternative? Many thanks, Kris.

  • Message #483

    you can use fORM::registerHookCallback() to do this if you'd like. for example, modifying the example from the docs, say you have a User class with a phone_number field and you want it to match the format (###)###-####…

    class User extends fActiveRecord
    {
        protected function configure()
        {
            fORM::registerHookCallback($this, 'post::validate()', 'User::validatePhoneNumber');
        }
     
        static public function validatePhoneNumber($object, &$values, &$old_values, &$related_records, &$cache, &$validation_messages)
        {
            if (preg_match('/\(\d{3}\)\d{3}\-\d{4}/', $values['phone_number']) === 0) {
                $validation_messages[] = 'Phone Number: The value entered is not in the correct (###)###-#### format';
            }     
        }
    }
    
    • Message #484

      hello vena. i didn't use till now this hook callback, please give an example, after i write this function, how i can call it, before populate and store method?

      • Message #485

        check out the docs under fORM for Adding Functionality to fActiveRecord. by setting the second parameter (the hook) of registerHookCallback to 'post::validate()', the function will automatically fire at the end of the validate() step, adding the message you've defined to $validation_messages and causing the fActiveRecord object to throw an exception. validate() is automatically done when you attempt to store() the record.

        • Message #486

          Tank you. i understood. was so easy :) is any solution to show this warning only if the not null default flourish warning don't show?

          • Message #487

            sorry, i'm not sure i understand. if you want it only to add the error message if there are no other errors, you can probably just check the length of the $validation_messages array. since post::validate() hooks fire after the regular flourish validation routines, if there were existing errors from that process then they would be set in the $validation_messages variable.

            if (preg_match('/\(\d{3}\)\d{3}\-\d{4}/', $values['phone_number']) === 0 && count($validation_messages) > 0) {
                        $validation_messages[] = 'Phone Number: The value entered is not in the correct (###)###-#### format';
                    }
            
            • Message #488

              i try to exaplain better. so flowrish has built in errror message for not null and number. and if a field is empty, the not null warning is show, but when the field it's fill with letter or letter and numbers, the 'Please enter a number' message show. this i want in my case. if both check rules return an error to show only first like in the built in.

              • Message #489

                ah, i see. i believe you'll have to search the $validation_messages array for the error message, then.

                if (preg_match('/\(\d{3}\)\d{3}\-\d{4}/', $values['phone_number']) === 0) {
                    $key = array_search($validation_messages, 'Phone Number: Please enter a value');
                    if($key !== FALSE) $validation_messages[$key] = 'Phone Number: The value entered is not in the correct (###)###-#### format';
                }
                

                so basically, array_search finds the existing error message, and then you use its key to set the new one. this overwrites the existing message.

                you'll have to use the exact error message in the array_search for it to work, though.

                • Message #490

                  tank u for taking your time.

    • Message #491

      Works like a charm. Thanks for your help vena. Kris.