[2.1.x] $form->isValid() doesn't merge validators (separate Validation class) #11500

Closed
NZX-DeSiGN opened this Issue Mar 6, 2016 · 1 comment

Projects

None yet

2 participants

@NZX-DeSiGN

Hi there,

I use the version of 2.1.0RC1 Phalcon and I think there is a bug in the function isValid class Phalcon\Forms\Form. When a separate Validation class is used andbinded with a form, the method isValid() overwrite validators declared in the initialize() method of the Validation.
Sorry for my English and see below the context, bug and correction (if I'm not wrong).

Validation class

<?php

namespace App\Validations;

use Phalcon\Validation;
use Phalcon\Validation\Validator\StringLength;

class SaveValidation extends Validation
{
    public function initialize()
    {
        $this->add('name', new StringLength(array(
            'min'            => 3,
            'messageMinimum' => "Name too short (3 chars min)"
        )));
    }
}

Controller class

<?php
namespace App\Admin\Controllers;

use App\Models\Category;
use App\Validations\SaveValidation;

class CategoryController extends ControllerBase
{
    public function newAction()
    {
        // Create new form
        $form = new NewForm();

        // IF is POST
        if ($this->request->isPost()) {
            // Create the new category
            $category = new Category();

            // Updates entity attributes
            $form->bind($_POST, $category);

            // Set form validation
            $form->setValidation( new SaveValidation() );

            // IF the form is valid, save the category
            if ($form->isValid($_POST, $category)) {
                // Save in DB
                if ($category->save()) {
                    $this->flashSession->notice("Success");
                }
            }
        }
    }
}

Phalcon\Forms\Form class with the bug and correction

/**
 * Phalcon\Forms\Form
 */
class Form extends Injectable implements \Countable, \Iterator
{
    public function isValid(var data = null, var entity = null) -> boolean
    {         
        /*  [....]   */

        /**
         * Prepare the validators
         */
        let preparedValidators = [];

        for validator in validators {
            let preparedValidators[] = [name, validator];
        }

        let validation = this->getValidation();
        if typeof validation == "object" {
            if validation instanceof Validation {
                /**
                 * Set the validators to the validation
                 */
                validation->setValidators(preparedValidators);  // Replace this line (326) by the next line
                validation->rules(preparedValidators);          // Replace with rules method to add and not override validators
            }
        } else {
            /**
             * Create an implicit validation
             */
            let validation = new Validation(preparedValidators);
        }

        /*  [....]   */
    }
}
@NZX-DeSiGN NZX-DeSiGN changed the title from Form isValid isn't merge validators (separate Validation class) to [2.1.x] $form->isValid() doesn't merge validators (separate Validation class) Mar 10, 2016
@mbrostami mbrostami referenced this issue Dec 9, 2016
Merged

Form validation some fixes #12466

3 of 3 tasks complete
@sergeyklay sergeyklay added this to the 3.0.3 milestone Dec 20, 2016
@sergeyklay
Collaborator

Fixed in the 3.0.x branch. Thanks you

@sergeyklay sergeyklay closed this Dec 20, 2016
@sergeyklay sergeyklay self-assigned this Dec 24, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment