April 7, 2007

Javascript day timetable using JQuery

Filed under: Javascript — Marcus @ 11:55 pm

This had been lying around for a while, but when I first tried JQuery I was able to clean it up a bit.

Example

JS source

April 5, 2007

Validating a model using Zend_Validate_Interface

Filed under: PHP — Marcus @ 10:23 am

One thing I like about Grails (being based on rails, I assume it stems from there…) is that it allows quite easy definition of constraints that the model classes should adhere to. Taking that idea, consider the following;

Code (php)
  1.  
  2. class SampleModel{
  3.   public $variable;
  4.   public $othervariable;
  5.   public $integer;
  6.   public $constraints = array(
  7.     ‘variable’ => ‘Zend_Validate_Alnum’,
  8.     ‘othervariable’ => ‘Zend_Validate_EmailAddress’,
  9.   );
  10.  
  11.   public function __construct()
  12.   {
  13.     $this->constraints[‘integer’] = new Zend_Validate_Between(4, 8);
  14.   }
  15. }

The idea is that you can define simple validators by supplying the class name, or more complex validators by declaring them in the constructor. This allows for custom validator declarations, or chained validators using Zend_Validate.

The next part to this is actually doing something with the constraints, and here’s where mixins would be ultra handy. The current implementations of mixins don’t really float my boat, so instead I’ll use a separate validator;

Code (php)
  1.  
  2. class ModelValidator implements Zend_Validate_Interface
  3. {
  4.   /**
  5.    * Array of validation failure messages
  6.    *
  7.    * @var array
  8.    */
  9.   private $messages = array();
  10.   /**
  11.    * Is the model valid?
  12.    * @return boolean
  13.    */
  14.   public function isValid($model)
  15.   {
  16.     // see if the user has defined any constraints at all
  17.     if (!isset($model->constraints)) {
  18.       return true;
  19.     }
  20.     $isValid = true;
  21.     $this->messages = array();
  22.     // Process the constraints otherwise
  23.     foreach ($model->constraints as $property => $constraint) {
  24.       // If the constraint is a string, we create an instance
  25.       // of the class it describes. If it’s an object, we
  26.       // use that instead
  27.       if (is_string($constraint)) {
  28.         $constraint = new $constraint;
  29.       }
  30.     if (!$constraint->isValid($model->$property)) {
  31.         $this->messages[$property] = $constraint->getMessages();
  32.         $isValid = false;
  33.       }
  34.     }
  35.     return $isValid;
  36.   }
  37.  
  38.  /**
  39.   * Get validation erro messages
  40.   *
  41.   * @return array
  42.   */
  43.   public function getMessages()
  44.   {
  45.     return $this->messages;
  46.   }
  47. }

So that will run through all the defined constraints and let us know if we’re valid or not. Also, being an implementor of Zend_Validator_Interface, it can be chained along with other validator calls. Useful… for me anyway.

April 4, 2007

Selecting no fields of a table joined in a Zend_Db_Select

Filed under: PHP — Marcus @ 1:00 pm

I want to be able to use joins to limit the result set I get from a query, but I don’t want to actually select any values from the table I’m joining on, merely use it for making sure what I get back is what I asked for. However it seems there’s no way of saying “don’t select ANY columns from this joined table”.

Code (php)
  1.  
  2. $select->from(‘registereduser’, ‘*’)
  3. ->joinInner(’sessionbooking’, ‘registereduser.id=sessionbooking.userid’,
  4. new Zend_Db_Expr(’sessionbooking.status as status’))
  5. ->joinInner(’sessiontime’, ’sessiontime.id=sessionbooking.sessionid’, )
  6. ->where(’sessiontime.id=?’);
  7.  

So far I’ve resorted to adding something into Zend_Db_Select::_tableCols() to check if the $cols is empty or null and returning if that’s the case. Am I just going about constructing my query incorrectly?

Paymate integration - setting the buyer’s state?

Filed under: Web — Marcus @ 12:01 am

I’ve been integrating Paymate for a friend’s site (which has been very bloody easy actually… I’m just waiting to hear horror stories though), and have come across a problem where I can’t actually send through the buyer’s State value at all, which is strange because it’s a required field on their payment form. The user can obviously select it themselves, but it’d be nice to automatically select it for them… I can set address lines, postcode, suburb etc, but NOT their state. I wonder if there’s an option I’m missing somewhere.

March 28, 2007

PHP5 WSDL Auto-generation

Filed under: PHP, Downloads — Marcus @ 3:39 pm

So it doesn’t get lost, I’m putting up my old WSDL auto-generation code again… It’s now found its way into PRADO 3.1 as a new service type, though having looked around, I can see another generator that seems as good if not better, as it looks like it provides a few more features than what mine does.

PHP5 WSDL Autogen

« Previous PageNext Page »
.

Powered by WordPress