2

I have a quick question that's killing my head.

I'm trying to make a Form Validation System with Method Chaining in PHP

What I want to do is to be able to call for example (please check the code comments):

$firstname = $OBJECT->Forms->Field("First Name", "firstname"); //This one doesn't validate, but just puts what's on firstname field on to $firstname. But this way doesn't work for me, because I have to return the object so it can be chainable and not the variable of the POST. How can I do this?
$firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate(); //this one validates if the field is not empty and if it's empty it'll insert the first parameter ("First Name") onto an array to display the errors.
$email = $OBJECT->Forms->Field("Email", "email")->Validate()->Email(); //This one does the same as above but validates Email and inserts the value of the email field onto $email
but I prefer the next one...
$email = $OBJECT->Forms->Field("Email", "email")->Validate->Email(); //I'd rather prefer this method but I don't know how to do it without using the parenthesis on the Validate method.

I can only make it work like this

    $firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate();
and
    $firstname = $OBJECT->Forms->Field("First Name", "firstname")->Validate()->Email();

Without ->Validate(); I can't seem to make it work (Like this: $firstname = $OBJECT->Forms->Field("First Name", "firstname");)

The code is kinda mess to share. But the code is simple... I have a forms.class.php and a validate.class.php. The forms.class.php creates an instance of Validate class from validate.class.php and the Forms Object is passed through the Validate class on the constructor.

I want to be able to do:

$OBJECT->Forms->Field();
$OBJECT->Forms->Field()->Validate();
$OBJECT->Forms->Field()->Validate()->Email;
$OBJECT->Forms->Field()->Validate()->Telephone;

or this preferebly:

$OBJECT->Forms->Field();
$OBJECT->Forms->Field()->Validate;
$OBJECT->Forms->Field()->Validate->Email;
$OBJECT->Forms->Field()->Validate->Telephone;

Only figured out:

$OBJECT->Forms->Field()->Validate();
$OBJECT->Forms->Field()->Validate()->Email();
$OBJECT->Forms->Field()->Validate()->Telephone();

But any form is OK

Thank you.

luffyff
  • 31
  • 4

1 Answers1

2

See if this is what you are trying to do:

<?php

    class   FormValidate
        {
            protected   $args;
            public      $valid;

            public  function Forms()
                {
                    // Don't know what this function is supposed to do....
                    return $this;
                }

            public  function Validate()
                {
                    $numargs    =   func_num_args();
                    $this->args =   array();

                    if($numargs == 2) {
                            $vals       =   func_get_args();
                            $this->args[$vals[1]]   =   $vals[0];
                            $this->valid            =   true;
                        }
                    else
                        $this->valid    =   false;

                    if(isset($this->args['firstname']) && !empty($this->args['firstname']))
                        return true;

                    return  $this;
                }

            public  function Email()
                {
                    if(isset($this->args['email'])) {
                            if(filter_var($this->args['email'],FILTER_VALIDATE_EMAIL))
                                return $this->valid     =   $this->args['email'];
                        }

                    return $this->valid =   false;
                }

            public  function Telephone()
                {
                    if(isset($this->args['telephone'])) {
                            if(preg_match('/^[0-9]{3}-[0-9]{3}-[0-9]{4}$/',$this->args['telephone']))
                                return $this->valid     =   $this->args['telephone'];
                        }

                    return $this->valid =   false;
                }
        }

        $test   =   new FormValidate();
        // These will throw a fatal error on the base Validate('First Name','firstname')
        // if you add another method to the chain like so: ->Validate('First Name','firstname')->Email();
        echo $test->Forms()->Validate('123-876-0987','telephone')->Telephone();

?>
Rasclatt
  • 12,498
  • 3
  • 25
  • 33
  • Actually what I want to do is: $telephone = $test->Forms("444445555")->Telephone();, then $telephone will have the value of 444445555 and it has to validate the Telephone();, if ->Telephone(); is not present: then assign 444445555 to $telephone without validating using Telephone(); – luffyff Jun 19 '15 at 03:41
  • So you don't need the `Field` or `Validate` methods?? – Rasclatt Jun 19 '15 at 03:48
  • Well, it would be $test->Forms->Field("First Name", "firstname")->Validate(); whereas firstname on Field is the form input name. And then Validate() would validate if it's empty or not, but it should return the value of the input of first name, and Validate() would add to another special variable if the field is empty. Same as $test->Forms->Field("Telephone", "telephone")->ValidateTelephone();, it should return the telephone if it's valid or invalid, but if it's invalid it would be stored in another variable. – luffyff Jun 19 '15 at 04:13
  • And $test->Forms->Field("First Name", "firstname"); (without ->Validate();) should return the input field value but not validate it. So the structure should be $test->Forms->Field("First Name", "firstname"); //returns firstname input field value $test->Forms->Field("First Name", "firstname")->Validate(); //returns firstname input field value and adds to a special variable if firstname is empty. $test->Forms->Field("Telephone", "telephone")->ValidateTelephone(); – luffyff Jun 19 '15 at 04:13
  • Actually what I want to do is to be able to do something like this $test->a()->b()->c(); $test->a()->b(); $test->a()->c(); $test->a(); – luffyff Jun 19 '15 at 04:18