-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added Jquery validation for Formation. Renamed jquery validation for …
…Forge
- Loading branch information
maartenvanvliet
committed
Mar 6, 2008
1 parent
4d93328
commit cb2cbc7
Showing
8 changed files
with
139 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters