Skip to content

Commit

Permalink
added Jquery validation for Formation. Renamed jquery validation for …
Browse files Browse the repository at this point in the history
…Forge
  • Loading branch information
maartenvanvliet committed Mar 6, 2008
1 parent 4d93328 commit cb2cbc7
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 14 deletions.
4 changes: 2 additions & 2 deletions Jquery_Validation.php → Forge_Jquery_Validation.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php
class Jquery_Validation_Core extends ArrayObject {
class Forge_Jquery_Validation_Core extends ArrayObject {

protected $form;
//Factory, useful for chaining
public static function factory($form=null)
{
return new Jquery_Validation($form);
return new Forge_Jquery_Validation($form);
}
//takes forge object as argument
public function __construct($form =null)
Expand Down
117 changes: 117 additions & 0 deletions formation/Jquery_Validation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
class Jquery_Validation_Core extends ArrayObject {

protected $form;

//takes forge object as argument
public function __construct($form =null)
{
if(!empty($form) && $form instanceof Validation)
{
$this->load($form);
}
}
//loads Forge into jquery validation array
public function load(Validation $form)
{

$this->form=$form;

foreach ($this->form as $field=> $input)
{
if($input->get_name()!='')
{
$rules = array();
$messages = array();

foreach($input->get_rules() as $rule)
{
if($input instanceof Element_Group)
continue;

$field_name=($input->get_screen_name()==null) ? $input->get_name() : $input->get_screen_name();

//check all Kohana rules and match them with jQuery validate rules
switch (get_class($rule))
{
case 'Rule_Required':
$rules['required']= true;
$messages['required']=str_replace('{name}',$field_name,$rule->get_message());
break;

case 'Rule_Email':
$rules['email'] = true;
$messages['email']=str_replace('{name}',$field_name,$rule->get_message());
break;

case 'Rule_Length':
$rules['minlength'] = $rule->min_length;
$rules['maxlength'] = $rule->max_length;
$messages['rangelength']=str_replace('{name}',$field_name,$rule->get_message());
break;

case 'Rule_Url':
$rules['url'] = true;
$messages['url']=str_replace('{name}',$field_name,$rule->get_message());
break;

case 'Rule_Digit':
$rules['digit'] = true;
$messages['digit']=str_replace('{name}',$field_name,$rule->get_message());
break;

case 'Rule_Numeric':
$rules['numeric'] = true;
$messages['numeric']=str_replace('{name}',$field_name,$rule->get_message());
break;
}
}
$jquery['rules'][$input->name]=$rules;
$jquery['messages'][$input->name]=$messages;



$this->set_spl($jquery);

}
}
return $this;

}
//Do the spl magic with the array
public function set_spl($jquery_array)
{
//spl magic
$this->exchangeArray($jquery_array);
$this->setFlags(ArrayObject::ARRAY_AS_PROPS | ArrayObject::STD_PROP_LIST);
}
//returns rules and messages as array
public function as_array()
{
return $this->getArrayCopy();
}
//Load an array if you want to bypass Forge
public function load_array(array $array)
{
$this->set_spl($array);
return $this;
}
//returns all rules and messages as json ready to be fed to jquery validation
public function as_json()
{
return json_encode($this->as_array());
}
//Returns string which does the whole validation
public function js_validate(){

return '$().ready(function() {$("#'.$this->form->get_attr('id').'").validate('.$this->as_json().');});';

}
//proxy to jquery_validation()
public function __toString()
{
return $this->js_validate();
}

}
?>
12 changes: 3 additions & 9 deletions formation/libraries/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,13 @@ class Field_Core {
* @param unknown_type $value
* @param Validate $validation_object
*/
public function __construct($name,$value=null,Validation $validation_object=null)
public function __construct($name,$value=null)
{

$this->name=$name;
$this->value=$value;
$this->unfiltered_value=$value;

//Pass validation object for callbacks
if($validation_object!=null)
{
$this->validation=$validation_object;
}


}

/**
Expand Down Expand Up @@ -130,7 +124,7 @@ public function validate()
continue;

$field_name=($this->screen_name==null) ? $this->name : $this->screen_name;
pr($field_name);

if($rule instanceof Rule_Upload_Required)
{
if(!$rule->is_valid($this->upload))
Expand Down
1 change: 1 addition & 0 deletions formation/libraries/Formation.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php defined('SYSPATH') or die('No direct access allowed.');


class Formation_Core extends Validation{

// Form attributes
Expand Down
3 changes: 2 additions & 1 deletion formation/libraries/elements/Element_Hidden.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

class Element_Hidden_Core extends Element_Input {


protected $attr = array
(
Expand All @@ -11,7 +12,7 @@ public function render()
$data = $this->data;
$data[$this->name]=$this->value;

return form::hidden($data);
return form::hidden($data,$this->value);
}
public function label()
{
Expand Down
10 changes: 10 additions & 0 deletions formation/libraries/elements/Element_Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ public function get_attr($key)
}
return false;
}
/**
* Set size attribute
*
* @param unknown_type $size
* @return unknown
*/
public function set_size($size)
{
return $this->set_attr($size);
}
/**
* Returns instance of a label
*
Expand Down
1 change: 0 additions & 1 deletion formation/libraries/rules/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public function __construct(array $arguments=null)
}

}

public function set_language_file($file)
{
$this->language_file=$file;
Expand Down
5 changes: 4 additions & 1 deletion formation/views/formation_template.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?php echo $open; ?>
<?php echo $open; ?><?php foreach($inputs as $input):
if($input instanceof Element_Hidden)
$input->render();
endforeach;?>
<fieldset>
<ol>
<?php if ($legend != ''):
Expand Down

0 comments on commit cb2cbc7

Please sign in to comment.