Yii Framework Forum: Validating Search Values (For E.g. Advanced Search In Crud) - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Validating Search Values (For E.g. Advanced Search In Crud) Behavior to validate the search values entered in the CRUD view Rate Topic: -----

#1 User is offline   Chris the German 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 11-July 13

Posted 11 July 2013 - 05:54 AM

Summary


In the standard implementation (e.g. using the CRUD generator) the search values in a grid are not validated. This means, if you e.g. enter 'foo' for an integer the SQL statement will bounce.

Therefore, it makes sense to validate also the search values. However, due to the prefixed operator (e.g. <> 10) the standard validation cannot be used.

With the following behaviour one can achive such a validation.

Behavior

<?php

class EnsureValidSearchValuesBehavior extends CBehavior {

    /**
     * Performs the validation for search by cloning the object, stripping away the ops and then calling the standard validate one the clone
     *
     * @return boolean whether the validation is successful without any error.
     * @see validate
     */
    public function validateForSearch() {
        $copy = clone $this->getOwner();
        foreach ($this->getOwner()->getSafeAttributeNames() as $attrib) {
            $value = $this->getOwner()->{$attrib};

            if (is_array($value)) {
                $copy->{$attrib} = NULL; // it is impossible to check the individual values
            } else {
                $value = "$value"; // enforce a string object
                if (preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/', $value, $matches)) {
                    $copy->{$attrib} = $matches[2];
                }
            }
        }

        $valid = $copy->validate();
        
        // Append the errors found to the original model
        $this->getOwner()->addErrors($copy->errors);
        
        return $valid;
    }

    /**
     * Checks via validateForSearch if the search values are proper; if not raises a HTTP 400 exception
     *
     * @see validateForSearch
     */
    public function ensureValidSearchValues() {
        if (!$this->getOwner()->validateForSearch()) {
            throw new CHttpException(400, "At least one search parameter is not valid. Mindestens einer der Suchparameter ist nicht korrekt");
        }
    }

}

?>


Usage


The usage is within the model class

Add the behavior to the function behaviors()


        public function behaviors(){
            return array(
                'EnsureValidSearchValuesBehavior' => array(
                    'class' => 'EnsureValidSearchValuesBehavior',
                ),
            );
        }


Extend the search function to ensure the validity


	public function search()
	{
                // Check for valid search values (using the EnsureValidSearchValuesBehavior)
                $this->ensureValidSearchValues();

                .........
        }
                    


0

#2 User is offline   codesutra 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 596
  • Joined: 15-March 11
  • Location:India

Posted 11 July 2013 - 06:43 AM

Nice Article. Thanks for sharing your thoughts. :)
CodeSutra
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users