1
votes

I am having a weird issue with my form validations. I have 4 inputs in my form and they are all being validated.

Everything works fine until it validates my last input field which is an email input field. I have two rules set on the email input field, 'rule' => 'notEmpty' , 'rule' => 'email'. The form validates and posts just fine when it only has to check for notEmpty rule but if an invalid email is found it does validate it but it doesn't post even after you input a valid email.

Here are some scenarios:
One: User inputs valid entries for all the inputs
Everything works fine, the form posts and the data is added to the database.

Two: User inputs valid entries for all but the email input but then enters a valid emails in all inputs
Assuming that the user hits submit after only providing input for the first three fields
Everything works fine after they input a valid email address on the first go, the form posts and data is added to the database.

Three: User inputs valid entries for all except the email (provides an invalid email)
It displays the error message but even after providing a valid email address it doesn't do anything. I notice that the page keeps reloading with the same data if you keep clicking the submit button.
Also, the error message goes away if the submit button is clicked again after the initial click

Here is my Model:

class Member extends AppModel
{
    public $validate = array(
        'id' => array(
            'rule' => 'notEmpty'
            ),
        'Student_First_Name' => array(
            'rule' => 'notEmpty'
            ),
        'Student_Last_Name' => array(
            'rule' => 'notEmpty'
            ),
        'Student_Email' => array(
            'emailRule-1' => array(
                'rule' => 'notEmpty',
                'last' => false
                ),
            'emailRule-2' => array(
                'rule' => 'email',
                'message' => 'Please enter a valid email address.'
            ))
        );

}

Here is the Controller:

class MembersController extends AppController
{
    ...
    public function add()
    {
        if($this->request->is('post'))
        {
            $this->Member->create();

            if($this->Member->save($this->request->data))
            {
                $this->Session->setFlash('Member has been added!');
                $this->redirect( array( 'action' => 'members'));
            }
            else
            {
                $this->Session->setFlash('Unable to add member.');
            }
        }
    }
    }

I apologize for such a long post for such a simple/annoyance problem but I hate having stuff like this in my applications/websites.

Thank you.

1
Why are you using 'last' => false? makes no sense to me.. if one didnt input any email there is no need to also check on a valid form of email... - mark
Yea its unnecessary, I was just going over the documentation and was trying different things. - Archon

1 Answers

0
votes

Your form will most likely be another form of post: put.

A quick explanation is:

POST is simulated by FormHelper for 'create' form, or any post form that isn't modifying an existing record.

PUT is for updating existing records.

See http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#FormHelper::create

Your add action counts as "create".

By the way: The cake code shows that it actually SHOULD be a POST (see comments below). So somewhere in your code you make the helper change it from POST to PUT there. Maybe you are doing some unconventional things.

Tip: If you want to be on the safe side, check on both:

if ($this->request->is('post') || $this->request->is('put')) {}

to make sure both post types are valid.

Also try to stick to conventions and use lower_case_underscored fieldnames. And last but not least drop the "id" validation. It's pointless to validate primary keys.