Keith Thibodeaux Sun Sep 26 14:33:12 -0400 2010

Subject: Custom Validation

Hello all!

I'm having some issues with phpAR that I'm hoping someone here can help me with.

I have a customer model. When creating a new customer, I want at least one of three fields to have a value (hphone, cphone, or bphone).

The code I am attempting to get to function is as follows:

public function validate() {
        # At least one phone number please
        if ($this->hphone == "" && $this->cphone == "" && $this->bphone == "") {
            $this->errors->add('hphone', 'can not be empty if cell and business are empty');
            $this->errors->add('cphone', 'can not be empty if home and business are empty');
            $this->errors->add('bphone', 'can not be empty if home and cell are empty');
        }
    }

However, when checked with with:

if (!$customer->is_valid()) {
            User::show_error($customer->errors); #the show error function is in the User model
            include('templates/customer/scaffold.php');
        } else {
            $customer->save();
            include('templates/customer/add.php');
        }

...it passes with none of the three fields having a value. Note, that with validates_presence_of on fname and lname, it will throw an error to the user if one of those is blank.

What am I doing wrong?


Keith Thibodeaux Sun Sep 26 15:14:10 -0400 2010

Another note, I've never used phpAR's custom validation before.

So, I tried replacing my validation function with this one:

public function validate() {
        # At least one phone number please
        $this->errors->add('fname', 'test');
    }

... and it does not function either.

The full Customer.php model:

<?php

class Customer extends ActiveRecord\Model {

    static $validates_presence_of = array(
        array('fname'),
        array('lname')
    );

    # Custom validation
    public function validate() {
        # At least one phone number please
        $this->errors->add('fname', 'test');
    /*    if ($this->hphone == "" && $this->cphone == "" && $this->bphone == "") {
            $this->errors->add('hphone', "can not be empty if cell and business are empty");
            $this->errors->add('cphone', "can not be empty if home and business are empty");
            $this->errors->add('bphone', "can not be empty if home and cell are empty");
        }    */
    }

    public function is_company() {
        if ($this->name_type == 2) {
            return true;
        } else {
            return false;
        }
    }

    public function name($ff = FALSE) {
        if ($this->name_type == 2) {
            return $this->cname;
        } else {
            if ($ff == FALSE) {
                return $this->lname . ", " . $this->fname;
            } else {
                return $this->fname . " " . $this->lname;
            }
        }
    }

    public function othername($ff = FALSE) {
        if ($this->name_type == 1) {
            return $this->cname;
        } else {
            if ($ff == FALSE) {
                return $this->lname . ", " . $this->fname;
            } else {
                return $this->fname . " " . $this->lname;
            }
        }
    }

    public function has_mc() {
        if ($this->is_mc == 1) {
            return TRUE;
        } else {
            return FALSE;
        }
    }

    public function state_name() {
        $state_list = array('AL'=>"Alabama", 'AK'=>"Alaska", 'AZ'=>"Arizona", 'AR'=>"Arkansas", 'CA'=>"California", 'CO'=>"Colorado", 'CT'=>"Connecticut", 'DE'=>"Delaware", 'DC'=>"District Of Columbia", 'FL'=>"Florida", 'GA'=>"Georgia", 'HI'=>"Hawaii", 'ID'=>"Idaho", 'IL'=>"Illinois", 'IN'=>"Indiana", 'IA'=>"Iowa", 'KS'=>"Kansas", 'KY'=>"Kentucky", 'LA'=>"Louisiana", 'ME'=>"Maine", 'MD'=>"Maryland", 'MA'=>"Massachusetts", 'MI'=>"Michigan", 'MN'=>"Minnesota", 'MS'=>"Mississippi", 'MO'=>"Missouri", 'MT'=>"Montana", 'NE'=>"Nebraska", 'NV'=>"Nevada", 'NH'=>"New Hampshire", 'NJ'=>"New Jersey", 'NM'=>"New Mexico", 'NY'=>"New York", 'NC'=>"North Carolina", 'ND'=>"North Dakota", 'OH'=>"Ohio", 'OK'=>"Oklahoma", 'OR'=>"Oregon", 'PA'=>"Pennsylvania", 'RI'=>"Rhode Island", 'SC'=>"South Carolina", 'SD'=>"South Dakota", 'TN'=>"Tennessee", 'TX'=>"Texas", 'UT'=>"Utah", 'VT'=>"Vermont", 'VA'=>"Virginia", 'WA'=>"Washington", 'WV'=>"West Virginia", 'WI'=>"Wisconsin", 'WY'=>"Wyoming");

        return $state_list[$this->state];
    }

    public function homephone() {
        return substr($this->hphone, 0, 3) . "&middot;" . substr($this->hphone, -7, -4) . "&middot;"  . substr($this->hphone, -4);
    }

    public function cellphone() {
        return substr($this->cphone, 0, 3) . "&middot;" . substr($this->cphone, -7, -4) . "&middot;"  . substr($this->cphone, -4);
    }

    public function businessphone() {
        $this->bext ? $ext = " ext. " . $this->bext : $ext = "";
        return substr($this->bphone, 0, 3) . "&middot;" . substr($this->bphone, -7, -4) . "&middot;"  . substr($this->bphone, -4) . $ext;
    }

    public function faxnumber() {
        return substr($this->fphone, 0, 3) . "&middot;" . substr($this->fphone, -7, -4) . "&middot;"  . substr($this->fphone, -4);
    }

    function partial_radio_name_type($field, $value) {
        if ($value == 1) {
            $home = "checked";
            $business = "";
        } else {
            $business = "checked";
            $home = "";
        }

        echo "<input type='radio' name='$field' value='1' $home /> Home";
        echo "&nbsp;&nbsp;";
        echo "<input type='radio' name='$field' value='2' $business /> Business";
    }

    function index() {
        $page = $_GET['page'];

        if (is_numeric($page)) {
            $offset = ($page - 1) * $_SESSION['logtech']->op_customers_per_page;
        } else {
            $offset = 0;
            $page = 1;
        }

        $customers = Customer::all(array('limit' => $_SESSION['logtech']->op_customers_per_page, 'offset' => $offset, 'order' => 'name_type ASC, cname ASC, lname ASC'));
        include('templates/customer/index.php');
    }

    function show($id) {
        $customer = Customer::find_by_id($id);

        include('templates/customer/show.php');
    }

    function count_all() {
        $customers = Customer::all();
        return count($customers);
    }

    function searchall() {
        $term = mysql_real_escape_string($_POST['term']);

        $customers = Customer::all(array('conditions' => 'fname LIKE \'%'.$term.'%\' OR lname LIKE \'%'.$term.'%\' OR cname LIKE \''.$term.'%\'', 'order' => 'name_type ASC, cname ASC, lname ASC'));
        include('templates/customer/index.php');
    }

    function homes() {
        $page = $_GET['page'];

        if (is_numeric($page)) {
            $offset = ($page - 1) * $_SESSION['logtech']->op_customers_per_page;
        } else {
            $offset = 0;
            $page = 1;
        }

        $customers = Customer::all(array('limit' => $_SESSION['logtech']->op_customers_per_page, 'offset' => $offset, 'conditions' => 'name_type = 1', 'order' => 'lname ASC'));
        include('templates/customer/homes.php');
    }

    function count_homes() {
        $customers = Customer::all(array('conditions' => 'name_type = 1'));
        return count($customers);
    }

    function searchhomes() {
        $term = mysql_real_escape_string($_POST['term']);

        $customers = Customer::all(array('conditions' => 'name_type = 1 AND fname LIKE \'%'.$term.'%\' OR lname LIKE \'%'.$term.'%\'', 'order' => 'lname ASC'));
        include('templates/customer/homes.php');
    }

    function businesses() {
        $page = $_GET['page'];

        if (is_numeric($page)) {
            $offset = ($page - 1) * $_SESSION['logtech']->op_customers_per_page;
        } else {
            $offset = 0;
            $page = 1;
        }

        $customers = Customer::all(array('limit' => $_SESSION['logtech']->op_customers_per_page, 'offset' => $offset, 'conditions' => 'name_type = 2', 'order' => 'cname ASC'));
        include('templates/customer/businesses.php');
    }

    function count_businesses() {
        $customers = Customer::all(array('conditions' => 'name_type = 2'));
        return count($customers);
    }

    function searchbusinesses() {

        $term = mysql_real_escape_string($_POST['term']);

        $customers = Customer::all(array('conditions' => 'name_type = 2 AND cname LIKE \''.$term.'%\'', 'order' => 'cname ASC'));
        include('templates/customer/businesses.php');
    }

    function scaffold() {
        $customer = new Customer;

        $customer->name_type = 1;
        $customer->hphone = "337";
        $customer->bphone = "337";
        $customer->cphone = "337";
        $customer->fphone = "337";

        include('templates/customer/scaffold.php');
    }

    function add() {
        $customer = new Customer();

        foreach ($_POST as $key => $value) {
            if ($key != "submit") {
                $customer->$key = mysql_real_escape_string($value);
            }
        }

    /*    if ($customer->hphone == "337") { $customer->hphone = ""; }
        if ($customer->bphone == "337") { $customer->bphone = ""; }
        if ($customer->cphone == "337") { $customer->cphone = ""; }
        if ($customer->fphone == "337") { $customer->fphone = ""; } */

        #Validate fields before saving
        if (!$customer->is_valid()) {
            User::show_error($customer->errors);
            include('templates/customer/scaffold.php');
        } else {
            $customer->save();
            include('templates/customer/add.php');
        }
    }

    /*
    function fixphonenumbers() {
        $customers = Customer::all();
        foreach($customers as $customer) {
            $byebye = array("-", " ", "()", ")");
            $customer->hphone = str_replace($byebye, "", $customer->hphone);
            $customer->cphone = str_replace($byebye, "", $customer->cphone);
            $customer->bphone = str_replace($byebye, "", $customer->bphone);
            $customer->fphone = str_replace($byebye, "", $customer->fphone);

            if (strlen($customer->hphone) == 7) {
                $customer->hphone = '337' . $customer->hphone;
            } else {
                $customer->hphone = "";
            }

            if (strlen($customer->cphone) == 7) {
                $customer->cphone = '337' . $customer->cphone;
            } else {
                $customer->cphone = "";
            }

            if (strlen($customer->bphone) == 7) {
                $customer->bphone = '337' . $customer->bphone;
            } else {
                $customer->bphone = "";
            }

            if (strlen($customer->fphone) == 7) {
                $customer->fphone = '337' . $customer->fphone;
            } else {
                $customer->fphone = "";
            }

            $customer->save();
        }

        echo "All done... whew!";
    }
    */

}

?>

CentOS 5.5, PHP 5.3, MySQL 5.0.77

Keith Thibodeaux Sun Sep 26 15:15:58 -0400 2010

For s&g's: the Customer table

--
-- Table structure for table 'customers'
--

CREATE TABLE customers (
  id int(10) unsigned NOT NULL auto_increment,
  fname varchar(128) default NULL,
  lname varchar(128) default NULL,
  cname varchar(255) default NULL,
  address varchar(255) NOT NULL,
  email varchar(255) default NULL,
  city varchar(255) NOT NULL,
  state varchar(2) NOT NULL,
  zip varchar(5) NOT NULL,
  hphone varchar(10) default NULL,
  cphone varchar(10) default NULL,
  fphone varchar(10) default NULL,
  bphone varchar(10) default NULL,
  bext int(5) default NULL,
  comments text,
  name_type int(11) NOT NULL,
  is_mc tinyint(4) NOT NULL default '0',
  PRIMARY KEY  (id)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1;
Jacques Fuentes Wed Sep 29 20:19:50 -0400 2010

Custom validations were added about a month ago with this commit

You will need to be sure that you have that version of PHP-AR to use a custom validation.

(1-3/3)