Loading Please wait...

validation System

In Zigmoyd vaidation is completely different programming unit thats a Validation Map textual INI like configuration file. Zigmoyd reads it and dynamically builds the validation Layer based upon the filters and error messages for the provided fields from the validation map. You can read more on validation map on the Validation Map page.

You can validate a get/post/Upload request. you can set auto validation system on ORM Layers so that the data is always automatically checked with the validation criteria before insert/update operation. so there is no backdoor to insert invalid value on database.

Validate Get or Post fields and ORMs

You can validate GET or post fields in the same way of others e.g. by using a validation map. and initialize validation system with that validation Map. from your Controller $this->request->get and $this->request->post represent's the get and post object. however you can also do the same thing by using $request global variable. then $request->get and $request->post represent's the get and post object.

To initialize validation system you need to invoke zRom_form::initValidator() method. e.g. $this->request->get->initValidator('validationMapName') and $this->request->post->initValidator('validationMapName')

<?php
class someController extends homeController{
  public function someMethod(){
    if($this->request->post->isSubmitted()){
      $this->request->post->initValidator('validationMapName');
    }
    if($this->request->post->isValid){
      //all data entered are ok and valid
    }else{
      //one or more data entered is/are invalid
    }
  }
}
?>
after invoking initValidator() it will validate post form fields according to the orm Map File specified. so after invoking initValidator() method you can check wheather the form is valid or not by invalid by cehcking zRom_form::isValid member variable.
You can use the same way to validate get request too using $this->request->get. both $this->request->get and $this->request->post are instance of zRom_form Class.
Now after validation you will get all form fields as an object under $this->request->post or $this->request->get (if you are using get instead of post) e.g. If you have a Form field called name you will get an object $this->request->post->name of zRom_fields type. on which you can invoke some methods and also use some of its public attributes do see the status of that field.

$this->request->post->name->value
this attribute holds the value entered for name form field
$this->request->post->name->isValid
this attribute holds wheather or not name form field is valid
$this->request->post->name->isValid()
same as th above just an access method
$this->request->post->name->err_msg
this attribute holds the error message applicable for this form field. (Subjected to be changed to errMsg in Next version)
$this->request->post->name->errMsg
access method of err_msg
$this->request->post->name->validationStatus
generally holds an integer value of teh corresponding index of the [[validation]] rule/criteria violeted

You can code a View form.view.php

<!-- form.view.php -->
<div class="<?= ($success) ? 'form-success' : 'form-failure' ?>">
  <?= $statusText ?>
</div>
<? if(!($request->post->isSubmitted() && $request->post->isValid())): ?>
  <div class="form">
    <form method="post">
      <div class="form-elem">
        <label for="name">Name</label><input type="text" name="name" id="name" value="<?= $request->post->valueOf('name') ?>" />
      </div>
      <div class="form-elem">
        <span class="error"><? if($request->post->isSubmitted() && !$request->post->name->isValid())echo $request->post->name->errMsg() ?></span>
      </div>
      <div class="form-elem">
        <label for="email">eMail</label><input type="text" name="email" id="email" value="<?= $postPeer->valueOf('email') ?>" />
      </div>
      <div class="form-elem">
        <span class="error"><?= triggeredErr('name', $request->post) ?></span>
      </div>
    </form>
  </div>
<? endif; ?>
variables Like $success, $statusText comes from controller and as $request is a member variable of controller it becomes local on the view.
$this->request->post is quite big to write so controller defines another variable which is just a reference to $this->request->post that is $this->postPeer which is being used as local variable here in the view.
This big line <? if($request->post->isSubmitted() && !$request->post->name->isValid())echo $request->post->name->errMsg() ?> First checks wheather or not the form has been submitted and if submitted but its not valid we are printing teh error message specified for that field. for the first unmatched criteria. However you might find labourious to write that huge line. so zigmoyd defines a short function. triggeredErr() which checks wheather the form has been submitted or not and if submitted and not valid it returns the error message specified for the first unmatched criteria as a string else returns a nul string ("") so PHP doesn't fire any error even if error_reporting is set to E_ALL.

and we can code like this in the Controller.

<?php
class someController extends homeController{//homeController extends zigController Class
  public function someMethod(){
    if($this->request->post->isSubmitted()){
      $this->request->post->initValidator('form-chkr');
      //We could also use $this->validateForm('form-chkr') which does the same thing.
      $this->success = $this->request->post->isValid();
      if($this->success){
        $this->statusText = "Hi! ".$this->request->post->name->value." You have been successfully registered with email adress".$postPeer->valueOf('name');
      }else{
        $this->statusText = "Registration Failed";
      }
    }
  }
}
?>
First we are checking wheather the form has been submitted or not and if submitted initialize the validation system through a validation map names form-chkr.vld.map.php found in your etc/conf.d directory.
as $this->request->post->initValidator('form-chkr'); is quite long to write controller defines a shortcut zigController::validateForm() which does the same action as above.
Note:
in the current version you might need to check wheather the form has been submitted or not bfore initializing validation system but you might not need to do this in latter version cause validateForm() will autocheck whaether the form has been submitted or not.
and this is our Validation Map.
[name]
  required: Your Name must not be left blank
  string:   Your Name Must be a String (Invalid Entry)
[email]
  required: You must enter your age
  email:    valid Email required (Invalid Entry)
so if the user doesn't fills up the name field it fires and required error whose index is 0 and triggeredErr() returns the error message specified for it. in the same way if the user enters an invalid email address the email error will occur who's index is 1 and triggeredErr() return the error message specified for it.

Validation on Orm Layer.

the same validation system is done automatically in case of orm layers too. you access a table like $this->db->Table from your Controller
Note:
always remember all member variables becomes local on the rendered view so $this->db->Table will be accesibe as $db->Table from the rendered view
You can use $this->db->Table->validation and $this->db->Table->errCols object for accessing validation result(s). $this->db->Table->errCols is an associative array that holds key value pair in $this->validation->errCols[invalidFieldName] => "errorMessage" pattern. e.g. you can just run a loop in it and print the invalid column name and the error message fired for it.
Note:
only invalid fields are listed in $this->validation->errCols
You can use boolean $this->validation->isValid member vriable to check wheather the entry is valid or not. each field/column taking part in validation have thier own object of type zRom_fields (planed). so you can use $this->validation->fieldName->isValid() to check wheather or not that particular field is valid and many other things (refer to zRom_fields class docs for more informations)
Warning:
all (both valid and invalid) fields gets listed in $this->validation as an object or zRom_fields class but only invalid fields ar listed in $this->validation->errCols
Note:
Here $this->validation refers to $this->db->Table->validation from your controller.
Warning:
all (both valid and invalid) fields gets listed in $this->validation as an object or zRom_fields class but only invalid fields ar listed in $this->validation->errCols
You can track the errors in the same way like get/post/fileUpload using triggeredErr() function with $this->db->Table->validation object as handle (from controller) however if you are using it from view then $db becomes local so you need to use $db->Table->validation as handle at that time.

See also:
zOrm_base::isValidEntry()

Upload Criteria

Validation of upload fields are done in the similar way with $this->request->file handle however validation rules/criterias are different. you can take a look at them on Validation Map on upload section.
See also:
Upload criterias
By default Zigmoyd upload's everything on transfer/up Directory in your Project's directory. However it can be changed through setdestination()

You use the upload validation exactly in the same way you use GET/POST form validation. through $this->request->file.First you should check wheather or the form has been submitted using $this->request->file->isSubmitted(), however you can also do this through $this->isUploadRequest().

Now Invoke doUpload() Method $this->request->file->doUpload() that does the Uploading job.It returns true/false depending upon success or failure. You can simply code something like.

if($this->request->file->isSubmitted()){
  $this->request->file->initValidator('upload-chkr');
  if($this->request->file->doUpload()){//doUpload() returns false if validation errors occurs
    //Uploaded Successfully
  }else{
    //Problem while Uploading probabbly invalid entry in upload fields.
    //handle it just as we did in GET/POST zone.
  }
}else{
 //Form not yet Submitted
}
for more information on upload see upload page

Generated on Mon Oct 27 23:51:59 2008 for zigmoyd.kdevelop by doxygen 1.5.6