Skip to content

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

Closed
@NZX-DeSiGN

Description

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);
        }

        /*  [....]   */
    }
}

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions