php - Prevent Propel from inserting empty strings -


how can prevent propel orm inserting empty strings when column not set?

create table user (     uid integer primary key auto_increment,     email varchar(255) not null unique,  -- no default value   ...   ) engine innodb ... ;   

propel allows $user = new user(); $user->save();. have tried setting sql_mode doesn't help.

the correct way validator in schema , check using validate() method in code. here's example:

<database ...>   <table ...>     <!-- "required" attribute here sets db property -->     <column name="email" type="varchar" required="true" />     ...     <!-- adds unique index in db (but nothing in php code!) -->     <unique>       <unique-column name="email" />     </unique>     ...     <validator column="email">       <!-- validator rule makes $obj->validate() method fail on null -->       <rule name="required" message="the email required!" />       <!-- validator rule makes $obj->validate() method fail on empty string -->       <rule name="minlength" value="1" message="the email cannot blank!" />       <!-- add regular expression match email addresses here -->       <rule name="match" value="/regular expression/" message="please enter valid email address!" />       <!-- adds validation field unique before trying update db -->       <rule name="unique" message="that email address not unique!" />     </validator>   </table> </database> 

then in presave() code this:

class user extends baseuser {   ...   public function presave(propelpdo $con = null) {     // object pass validations?     if (!$this->validate()) {       $errors = array();       // failed, go through each failure , capture message:       foreach ($this->getvalidationfailures() $failure) {         $errors[] = $failure->getmessage();       }       // throwing exception stop save() occurring       throw new invalidargumentexception(implode("||", $errors));     }      return true; // if here, go ahead , save   } } 

in script call save() so:

... $user = new user(); try {   // try save (could fail)   $user->save();  } catch (invalidargumentexception $e) {   // have errors, split exception message each 1 separately   $errormessages = preg_split(/\|\|/, $e->getmessage());   // handle messages need } 

read more validators in propel documentation.


Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -