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.
$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 } } } ?>
$this->request->get
. both $this->request->get
and $this->request->post
are instance of zRom_form Class.$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; ?>
$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.<? 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"; } } } } ?>
form-chkr.vld.map.php
found in your etc/conf.d
directory.$this->request->post->initValidator('form-chkr');
is quite long to write controller defines a shortcut zigController::validateForm() which does the same action as above. [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)
$this->db->Table
from your Controller $this->db->Table
will be accesibe as $db->Table
from the rendered view$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. $this->validation->errCols
$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) $this->validation
as an object or zRom_fields class but only invalid fields ar listed in $this->validation->errCols
$this->validation
refers to $this->db->Table->validation
from your controller. $this->validation
as an object or zRom_fields class but only invalid fields ar listed in $this->validation->errCols
$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.
$this->request->file
handle however validation rules/criterias are different. you can take a look at them on Validation Map on upload section. 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 }