Oct 18 2008

Unique validation rule in CakePHP 1.2

Category: CakePHP, PHPvbmendes @ 08:46

In this post I will show an example of how apply the unique validation rule in CakePHP 1.2. It’s very important to do in some cases, like saving users. You must make sure that the username is unique in your system. Lets see how it works:

var $validate = array(
	'username' => array(
		'rule' => array('isUnique'),
		'message' => 'Username already in use.'
	)
);

I wrote a custom function to validate it, but thanks to Dia now I know the built-in validation rule.

Tags: , , ,


Oct 16 2008

meioMask 1.0.2 version released!

Category: Ajax, Javascript, jQueryfabiomcosta @ 01:11

I present you meioMask 1.0.2 - a jQuery plugin for masking inputs.

Plugin page

I’m going to centralize all the comments at the page of the project, so i’m not allowing comments here but you are free and motivated to comment at the meioMask’s project page.

Plugin official page

Features

  • Accepts paste event;
  • Haves fixed and reverse mask types ( allow number mask );
  • You can still use your hot keys and others (ex: ctrl+t, ctrl+f5, TAB …);
  • Supports metadata plugin;
  • Works with iPhone;
  • Allow default values;
  • Haves callbacks for invalid inputs, valid and overflow;
  • Haves function to mask strings.

Changelog

v1.0.2

  • added input callbacks: onInvalid, onValid and onOverflow;
  • added support for default values;
  • can now be used like $().setMask({});
  • added the function $.mask.string(string,mask) that will mask a string (see demos);
  • now the value of the input is masked at the time the mask is applyed to it.

v1.0.1

  • added support for iphone;
  • removed a bug where a fixed char would be replaced by an inputed one.

v1.0

  • initial release.

Some bugs have been fixed too. I’m making a TODO list and i’m really open to add features you guys want.

You can see the plugin page here. It contains documentation and examples. Please tell me any bug, new feature, english errors on documentation…. anything! I’ll be glad to hear your feedback and make the fixes. Hope it helps you!

Tags: , , , , ,


Oct 14 2008

Pretty Truncate String Function in PHP (substr replacement)

Category: PHPvbmendes @ 15:33

In this post, I’ll show you a function to make truncate a string. I could simply use the substr function, but it’s not that good since I can break the string in the middle of a word. So I decided to write another function to do this job. It takes 4 parameters: the string you want to truncate, the length you want, the end pattern (defaults to ‘…’) and the string encoding (defaults to UTF-8). I need the encoding because substr may split in the middle of a character, if it is multi-byte, so I use the mb_substr function.

Examples

echo limit('this is just a test',10);// will print 'this is...'
echo limit('this is just a test',30);// will print 'this is just a test'
echo limit('thisIsJustABigWord',10);// will print 'thisIsJust...'

Notice that in the first case, the string will be trucated in the middle of a word(’just’), so this word was removed also. In the second case, the string is smaller then the max size I specified, so it’s returned as it is. I the third case, the string is just a big word, bigger then the max size, so the word is breaked because this is the only solution i have.

Code

function limit($string,$length,$end='...',$encoding=null){
	if(!$encoding) $encoding = 'UTF-8';
	$string = trim($string);
	$len = mb_strlen($string,$encoding);
	if($len <= $length) return $string;
	else {
		$return = mb_substr($string,0,$length,$encoding);
		return (preg_match('/^(.*[^\s])\s+[^\s]*$/', $return, $matches) ? $matches[1] : $return).$end;
	}
}

In the code, I firstly remove the spaces in the start and end of the string, then I get the lenght of my string, if it’s lower then what I want, return the string itself, otherwise, return the truncated with the end pattern.

Hope it helps.

Tags: , , , ,


Oct 13 2008

New features at HTML 5

Category: Javascriptfabiomcosta @ 00:23

I’ve found this video at youtube and i think thats a really great one. It shows the new features that are on the newest browsers (most of them on theirs nightly builds).

some of them are:

  1. onhashchange event, just on IE8 (thats a start right). With this it will be possible to make a robust and responsive, ajax application;
  2. input=”date”, just opera haves this at the moment (awsome feature!);
  3. input=”range”, webkit and opera…;
  4. no more doctypes! yes, no jokes!;
  5. no more type=”text/javascript” for the script tag;
  6. more!!!
see the video it’s a good html and javascript learning.

HTML 5: Features you want desperately but still can\’t use

***Ive’ got some great improvements at the meioMask plugin, im just updating the docs localy and trying to add some more features to release the 1.0.3 version :P.

Tags: , , , , , , ,


Oct 12 2008

MeioUpload Behavior 1.0.1 released!

Category: CakePHP, PHPvbmendes @ 19:40

I am glad to show you the MeioUpload Behavior 1.0.1. An improved Upload Behavior for CakePHP 1.2. It makes file uploads as simple as defining a variable.

See the docs.

Features

  • Accepts custom directory for files to be uploaded;
  • Validates the file extension and mime-type due to the behavior configuration;
  • Validates the max file size;
  • Allow custom validation rules;
  • Allow as many thumbnails formats as you want;
  • Allow more then one field to be uploadable, with custom options per field;
  • Stores the directory, filesize, and mime-type in the database if the table has these fields;
  • Allow the use of default files and deleting files without deleting the entire record;
  • Delete files when the record is deleted or updated with a new file;
  • Also works in the $model->saveAll method.

Changelog

v1.0.1

  • Fixed a bug in the create folder method;
  • Now you can use the $validate var of the model to apply the changes to default validation rules;
  • Changed the my_array_merge function, now it’s part of the behavior, name arrayMerge;
  • Allow use of {DS}, {model} and {field} constants in directory name and fields names;
  • Fixed a bug with the replacement of the default names.

v1.0

  • Initial release

Any bugs, features suggestions, and english errors, please tell me.

Tags: , ,