Custom Validation Class Printing Error Message in Any Case

I have created a Class for a form extending the Phalcon\Forms\Form.

I have created a Custom Validation class to validate the above form extending Phalcon\Validation

When I add data to form, submit it and try to validate the $_POST array it populates the $messages variable even in the case where I submit valid data.



31.7k

What is question ?

edited Nov '14

$messages = $validation->validate($_POST);

If count($messages) is greater than 0 then I am printing the error messages and reloading the Form. but say I enter all valid data then the $messages array should ideally be empty and count($messages) should return 0, but this is not happening $messages still contains the error messages....has such issue been encountered before?

thanks in advance.



31.7k

May be not recive data,but I recommed you method bind of Phalcon

$form = new UserForm($object);
        $form->bind($_POST, $object);

        //  Form isn't valid
        if (!$form->isValid($this->request->getPost())) {
            foreach ($form->getMessages() as $message) {
                $this->flashSession->error($message->getMessage());
            }

Thanks, Duy. Now my validation is in a separate custom validation class...will that work in this case? Is there a way to bind the Validation object to the Form object?

Could someone please help me? I am stuck. When I call

$messages = $validation->validate($_POST);

$messages is always populated with error messages no matter I enter correct or wrong data in the form.

Thanks.

edited Nov '14

My Validation class is

use Phalcon\Validation,
Phalcon\Validation\Validator\PresenceOf,
Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Regex;

 class MemberBulkFormAddVal extends Validation
 {
     public function initialize()
     {
        for ($i = 1; $i <= MEMBER_BULK_ADD_COUNT; $i++) {
            $this->add('member_first_name_$i', new PresenceOf(array(
                'message' => "The First name is required $i"
            )));

            $this->add('member_first_name_$i', new Regex(array(
                'message' => "The First name is invalid $i",
                'pattern' => '/^[A-Za-z ]*$/'
            )));

            $this->add('member_last_name_$i', new PresenceOf(array(
                'message' => "The Last name is required $i"
            )));
        }
    }
 }

My Form Class is

use Phalcon\Forms\Form,
Phalcon\Forms\Element\Text,
Phalcon\Forms\Element\Select,
Phalcon\Forms\Element\Hidden,
Phalcon\Forms\Element\Password,
Phalcon\Forms\Element\Submit;
use Phalcon\Validation;

use Phalcon\Forms\Element\Check;


class MemberBulkAddForm extends Form
{
    /**
     * Forms initializer
     *
     * @param Member $user
     * @param array $options
     */
    public function initialize($user, $options)
    {
        for ($i = 1; $i <= MEMBER_BULK_ADD_COUNT; $i++) {
            $fname = new Text("member_first_name_$i", array('maxlength' => 30 ));
            $this->add($fname);

            $lname = new Text("member_last_name_$i", array('maxlength' => 30 ));
            $this->add($lname);


        }

        $submit = new Submit("submit");
        $this->add($submit);
    }
}

Thanks

How do I link the 2?