Kristian Lunde

www.klunde.net

Archive for the ‘Misc’ Category

Zend_Input_Filter and the Alnum Validator

without comments

The Zend_Input_Filter is a very useful tool when you need to validate and filter the input to your application. It allows you to both filter and validate the input without a lot of hassle. One of the cool features it has is that it allows you to add the validators you need and meta commands to each validator. For instance you can set an Alnum validator to allow empty fields, set a default text and so on.

I came over this annoying issue the other day when I tried to setup the Alnum validator to allow white spaces and have a few meta commands attached to the validator chain. The manual says that you can do this:

  1. $validators = array(
  2.     'month'   => array(
  3.         'Digits',                // string
  4.         new Zend_Validate_Int(), // object instance
  5.         array('Between', 1, 12)  // string with constructor arguments
  6.     )
  7. );

Which I assumed would also work like this:

  1. $validators = array(
  2.     'name'   => array(
  3.         'Alnum',                
  4.         new Zend_Validate_Alnum(true), //allow whitespaces
  5.        'default' => '',  //meta command 1
  6.        'presence' => 'required', //meta command 2
  7.     )
  8. );

This does not work though. You need to remove the validator type string and replace it with an instance of the Zend_Validate_Alnum validator to get it to accept whitespaces and meta commands. This is the right way to do it:

  1. $validators = array(
  2.     'name'   => array(            
  3.         new Zend_Validate_Alnum(true), //allow whitespaces
  4.        'default' => '', //meta command 1
  5.        'presence' => 'required', //meta command 2
  6.     )
  7. );

The entire script ends up looking like this:

  1.  
  2. $filters = array('name' => 'StringTrim');
  3. $validators =  array(
  4.     'name'   => array(            
  5.         new Zend_Validate_Alnum(true), //allow whitespaces
  6.        'default' => '',
  7.        'presence' => 'required',
  8.     )
  9. );
  10.  
  11. $input = new Zend_Input_Filter($filter, $input, $this->getRequest()->getParams());

Written by Kristian Lunde

July 18th, 2010 at 9:00 pm

Klunde.net moved server

with 2 comments

Tonight (Friday 06.11.2009) klunde.net was moved from the servetheworld.net server to my new server provider Webfaction.co.uk. The move seemed to go painlessly, even the email accounts seem to work properly.

Klunde.net has been with servetheworld for approx. 5 years now. Webfaction had a much better solution to offer than Servetheworld and it was therefore an easy choice.

Farewell Servetheworld.

Written by Kristian Lunde

November 6th, 2009 at 11:39 pm

Posted in Misc

Recurring payments with Paypal and PHP

without comments

I have spent the past few days working with online payment solutions for mysapient. Yesterday I came across a bit of a troublesome issue when setting up recurring payments with paypal. There is no secret that the quality of the paypal documentation varies, so I had some troubles finding any proper information about this issue. Luckily there was a lot of other developers struggling with this same issue.

A quick google search gives you a lot of results about this topic and it can take you a while to get through them all, but at least here is the forum post that helped me getting this solved:

http://www.pdncommunity.com/pdn/board/message?board.id=nvp&view=by_date_ascending&message.id=5463

It seems like the setExpressCheckout needs the following parameters to work properly:

  1. [L_BILLINGTYPE0] => RecurringPayments
  2. [L_BILLINGAGREEMENTDESCRIPTION0] => Basic Plan

It is also important to add the “desc” field to the createRecurringPaymentsProfile call.

  1. [DESC] => Basic Plan

Written by Kristian Lunde

October 2nd, 2009 at 9:59 am

Posted in Misc

Amazon Frenzy

without comments

The other day I had a frenzy at amazon and ordered quite a few books I have had on my shopping list. The books are of course all computer and web related. The books I ordered was:

I am halfway through the building scalable web sites and it is really good, even if you are a seasoned web developer I think you can learn quite a bit from it. I have also started to read the MySQL book and can’t wait to learn more about database replication, that is something I really want to find out more about. The “Don’t make me think” book will hopefully assist me on building more user friendly web sites, and hopefully the javascript book(still in the mail) will help me to brush up my js skills.

The Cocoa book was on sale, and you never know when you have to do a bit of programming for a mac :P

Written by Kristian Lunde

September 24th, 2009 at 8:29 pm

On the move

without comments

I am currently switching both domain host and web host, and in that case this site might be down for a little while. I am about to replicate the site to my new web host so hopefully it will be a minimum of down time.

I have chosen to leave servetheworld.net which has been my web host since 2003, they have done a great job, but the time has now come to move to a web host which can provide me with some more advanced features.

I have chosen webfaction.com as my new web host. I chose them because they offer a multitude of different applications, for instance svn, python, ruby on rails and of course PHP. They also offer ssh access.

I chose 123-reg.com to host my domain names, that was done out of convenience, they are well known, large and hopefully know what they are doing :P

Written by Kristian Lunde

June 3rd, 2009 at 8:00 pm

Posted in Misc,web

Tagged with , ,

My 7 things

with 2 comments

The 7 things have been all around the PHP community for a while now, and I got tagged by Christer a while ago. So I guess I should go around and publish my seven things:

  1. I am an assisting nurse graduate
  2. I have a 3 year old daughter
  3. Expired dairy products are some of the most disgusting things I know
  4. I dream of some day having a small cabin by the lake, without any internet or television?!?
  5. I often think about how it would be like to be the only person alive on the earth. (Something similar to the “I am legend” movie, without the zombies).
  6. I took dance lessons when I was a kid, however I really suck at dancing.
  7. I drink way to much Pepsi

Those seven things were not really interesting at all, but it was all I could get together for now. Who knows someday I just might publish: “the top 1000 things you did not know about me” :P

My lucky seven bloggers which I tag are:

Here are some rules that must be followed by anyone who decide to follow up:

* Link your original tagger(s), and list these rules on your blog.
* Share seven facts about yourself in the post – some random, some wierd.
* Tag seven people at the end of your post by leaving their names and the links to their blogs.
* Let them know they’ve been tagged by leaving a comment on their blogs and/or Twitter.

Written by Kristian Lunde

January 22nd, 2009 at 1:34 am

Posted in Linux,Misc

Merry Christmas

without comments

I wish you all a very merry christmas :)

Written by Kristian Lunde

December 24th, 2008 at 11:54 am

Posted in Misc

Hello Standard ML (of New Jersey)

with 3 comments

Back when I was at the university we had a course called “Programming languages”, there we learned a little something about a lot of known and less known programming languages. One of these languages was Standard ML, and I remember that I was quite fascinated with that language, it was so different from everything else we had learned. Since then the functional languages have become a bit more common, with Erlang, F#, Clojure, Scala and Haskell as some examples (more functional languages can be found here: http://en.wikipedia.org/wiki/Category:Functional_languages). I have not used any functional languages since then and I thought I would just briefly refresh my memory by installing Standard ML and try the most basics of the language.

Installation

I am currently using a Mac so installing sml-nj was really simple. I downloaded the mac .dmg package from http://www.smlnj.org/ and ran the installer. the system was installed by default at /usr/local/smlnj-110.68. I added the path to my .profile file, which enables me to start sml without being in the sml bin root folder.

When that was done I could start the sml shell (just type sml in your shell):

  1. kristian-lundes-macbook-pro:sml kristianlunde$ sml
  2. Standard ML of New Jersey v110.68 [built: Thu Sep  4 16:23:20 2008]

Example 1: Start standard ML

(To exit the sml shell use key combination: CTRL + D).

First touch – Hello World

I always do a Hello World programming in a new language. Even though I have been playing around with Standard ML before I do not remember a hole lot of it, so I start from scratch.

  1. - "Hello World";
  2. val it = "Hello World" : string

Example 2: Print “Hello World”

The – is the sml prompt, to end a expression the semi-colon is used. So to write Hello World all you have to do is to apply quotes around the text and end it with ; and hit enter. The result gives us the text and the data type.

The first function

To do a Hello world proved to be quite simple, so lets take it a bit further and write a function which prints the hello world text.

  1. 1. – fun hello ():string = "Hello World";
  2. 2. val hello = fn : unit -> string
  3. 3. – hello();
  4. 4. val it = "Hello World" : string

Example 3: The first function

The function itself is a one liner seen on line 1 in the example above. The function is created using the fun keyword, the function is named hello and have no parameters which result in the (). The function return a result of the data type string, this is defined with the :string element, The content of the function start after the equal sign, and is just a print of the hello world text. To end the function close semi-colon.

When creating a function which is compiled we get the response seen in line 2. This tell us that hello is a function and that it return a string. I believe the unit text mean that the function does not take any parameters (I might be wrong).

To execute the function just type the name of the function, see line 3 in the example above, this result in printing out the “hello world” text as seen in line 4. Line 4 tell us that the result of the function is “Hello World” and the data type is a string.

Functions with parameters

Now we have created our first function, but a function usually need one or more parameters, so let us have a look at that. The next function is a simple echo function which just print the text to screen.

  1. 1. – fun echo (s:string):string = s;
  2. 2. val echo = fn : string -> string
  3. 3. – echo("Hello World");
  4. 4. val it = "Hello World" : string

Example 4: Function with parameter

Line 1 in example 4 define the function, it takes one parameter s as a string, the return data type is also a string and it just output the string to the shell. When the function is executed in line 3 the parameter is “Hello World” and the result is seen in line 4.

To add more than one parameter just add a comma between the parameters. Remember to define a data type on all the parameters.

Example 5 is a function which takes to parameters and sum the two parameters together before returning the result.

  1. 1. – fun sum(x:int,y:int):int = x + y;
  2. 2. val sum = fn : int * int -> int
  3. 3. – sum(10,20);
  4. 4. val it = 30 : int

Example 5: function with multiple parameters

Lambda expressions

Lambda expressions is also known as anonymous functions, and create a function without any name. This is used quite frequently in sml and are written as seen in example 6.

  1. fn() => print "Hello World";

Example 6: lambda expression.

The example 6 do not do much except print out the Hello world text when it is called. Naturally a lambda expression must be called as part of another function or code snippet.

Load a function from file

To load sml code from a file just use:

  1. - use "../sml/myfile.sml";

Where you replace ../sml/myfile.sml to point to your file.

Resources

This is the best online introductions I have found so far are:
www.pllab.riec.tohoku.ac.jp/smlsharp/smlIntroSlides.pdf
http://www.cs.cornell.edu/riccardo/prog-smlnj/notes-011001.pdf

A short FAQ:
http://www.smlnj.org//doc/FAQ/index.html

I really have not showed anything of the fancy stuff you could go about and build with Standard ML, but hopefully I am able to spend some more hours playing around with standard ML and write another blog post.

Written by Kristian Lunde

December 17th, 2008 at 12:52 am

Posted in Misc,Programming

Tagged with ,

The number is 4

with 3 comments

I got a challenge from Ramona tonight where I should answer a few questions.

1. Name 4 jobs I have had:

  • Senior developer @ Derdubor AS
  • Software engineer @ Teleplan AS
  • Salesman @ Blok Computer Services
  • Assisting nurse @ Sarpsborg psychiatric center

2. 4 movies I could watch again:

  • Fabuleux destin d’Amélie Poulain, Le
  • Lock, stock and two smoking barrels
  • Pulp Fiction
  • In China they eat dogs

3. Name 4 places I have lived:

  • Halden
  • Oslo
  • Sarpsborg

4. Name four tv shows I like:

  • The king of Queens
  • Two and a half men
  • Friends
  • Seinfeld

5. Four places I have been on vacation:

  • Alfas Del Pi (Spain)
  • Karlstad (Sweden)
  • Aalborg (Denmark)
  • Bournemouth (UK)

6. Four web sites I visit every day:

  • Google Reader
  • Google Analytics
  • Facebook
  • Twitter

I pass this on to Mats and Jan Petter and everyone else that feels like answering these questions.

Written by Kristian Lunde

November 8th, 2008 at 10:58 pm

Posted in Misc

My car is for sale

without comments

Since I am moving to the UK, I do not need a car in Norway anymore, and I am therefore selling my car.

See the full ad in finn.no (in Norwegian):

http://www.finn.no/finn/car/object?finnkode=15172038

Written by Kristian Lunde

October 16th, 2008 at 11:29 pm

Posted in Misc,Real Life

Get Adobe Flash playerPlugin by wpburn.com wordpress themes