mloureiro
mloureiro

Reputation: 949

Cakephp data validation "notEmpty" "required"

I'm a newbie to cake, done the blog tutorial and now I'm trying something simple to start. But I got something I can't understand. On model PersonalInformation I got the data validation for the table fields:

This is my model with the $validate array:

class PersonalInformation extends AppModel {

/**
 * Validation rules
 * @var $validate
 */
public $validate = array(
    "first_name" => array(
        "name"      => array(
            "rule"      => "validName",
            "message"   => 'Only alpha characters and "." (dots)',
        ),
    ),
    "last_name"  => array(
        "name"      => array(
            "rule"      => "validName",
            "message"   => 'Only alpha characters and "." (dots)',
        ),
    ),
    "birth_date" => array(
        "date"      => array(
            "rule"      => "date",
            "message"   => "Enter a valid date",
        ),
    ),
    "nickname"  => array(
        "alphanumeric"  => array(
            "rule"      => "alphaNumeric",
            "message"   => "Only alpha-numeric characters",
        ),
    ),
    "gender"     => array(
        "notEmpty"  => array(
            "rule"          => "notEmpty",
            "message"       => "Can't be empty",
        ),
    ),
);

} // end class

And this is my table:

CREATE TABLE `personal_informations`
(
`id` INT NOT NULL UNIQUE AUTO_INCREMENT,
`first_name` VARCHAR(45),
`last_name` VARCHAR(45),
`birth_date` DATE,
`nickname` VARCHAR(45),
`is_company` TINYINT DEFAULT 0 NOT NULL,
`picture_url` VARCHAR(255),
`gender` ENUM('m','f') NOT NULL,
`notes` TEXT,
`created` DATETIME,
`modified` DATETIME,
PRIMARY KEY (`id`)
) ENGINE=InnoDB CHARACTER SET=utf8;

CREATE INDEX `id_idx` ON personal_informations(id);

And here is the view:

<!-- /PersonalInformations/add -->

<div class="">
<h2>Create a personal information</h2>
    <?php
    echo $this->Form->create();

    echo $this->Form->input( "PersonalInformation.first_name" );
    echo $this->Form->input( "PersonalInformation.last_name" );
    echo $this->Form->input( 
        "PersonalInformation.birth_date", 
        array( 
            "separator" => " - ", 
            "minYear" => date('Y') - 90,
            "maxYear" => date('Y')
    )
    );
    echo $this->Form->input( "PersonalInformation.nickname" );
    echo $this->Form->input( "PersonalInformation.is_company", array( "type" =>     "checkbox" ));
    echo $this->Form->input( "PersonalInformation.piture_url", array( "type" => "file"     ));
    echo $this->Form->radio( "PersonalInformation.gender", array( "m" => __("Male"), "f"     => __("Female")), array( "hiddenField" => false ));
    echo $this->Form->textarea( "PersonalInformation.notes" );

    echo $this->Form->end( "Done" );
    ?>
</div>

So the problem is that for some reason the "nickname" is kind of required, I mean, when I click on submit button with the "nickname" empty it shows "Only alpha-numeric characters". And for the "gender" it doesn't show anything when none is selected.

If there is anything else that I need to post, just let me know.

Upvotes: 1

Views: 11914

Answers (3)

ADmad
ADmad

Reputation: 8100

Add 'allowEmpty' => true for the rule for nickname.

 "nickname"  => array(
        "alphanumeric"  => array(
            "allowEmpty"=> true,
            "rule"      => "alphaNumeric",
            "message"   => "Only alpha-numeric characters",
        ),
    ),

Upvotes: 1

Vijay Choudhary
Vijay Choudhary

Reputation: 848

For your nickname

'nickname' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Nick name is required.'
            )
        )

For Gender:

In the view

echo $this->Form->input('PersonalInformation.gender',array(
          'type'=>'select','empty' => '---- Select----',
          'class'=>'select',
          'options'=>$option_array
          ))

In Model Validate array:

'gender' => array(
            'required' => array(
                'rule' => array('notEmpty'),
                'message' => 'Can\'t be empty.'
            )
        )

Upvotes: 1

Alvaro
Alvaro

Reputation: 41595

For the nickname, have you tried to use required => false ? You can read more about it in the documentation.

"nickname"  => array(
    "alphanumeric"  => array(
        "rule"      => "alphaNumeric",
        "required" => false,
        "message"   => "Only alpha-numeric characters",
    ),
),

Upvotes: 0

Related Questions