Пытаюсь реализовать валидацию введенных пользователем данных.
В контроллере имеем:
var $validate = array (
'city_id' => array (
'rule' => 'numeric',
'on' => 'create',
'required' => true,
'allowEmpty' => false,
'message' => 'Invalid e-mail'
),
'first_name' => array (
'rule' => 'alphaNumeric',
'required' => true,
'on' => 'create',
'allowEmpty' => false,
'message' => 'Invalid First_Name'
),
'last_name' => array (
'rule' => array ( 'maxLength', 3 ),
'required' => true,
'on' => 'create',
'allowEmpty' => false,
'message' => 'Invalid Last_Name'
),
'email' => array (
'rule' => array ( 'email', true ),
'on' => 'create',
'allowEmpty' => false,
'required' => true,
'message' => 'Invalid e-mail'
),
'photo' => array (
'rule' => array ( 'file', true ),
'on' => 'create',
'required' => true,
'message' => 'Invalid e-mail'
)
);
…
и
function add()
{
if ( ! empty ( $this->data ) ) {
// выгрузку фото пока пропустим
$this->data['User']['photo'] = "";
if ( $this->User->create ( $this->data ) ) && ( $this->User->validates ( $this->data ) ) {
if ( $this->User->save ( $this->data ) ) {
$this->flash ( "Успешно сохранено", "add" );
} else {
$this->flash ( "Ошибка сохранения", "add" );
}
} else {
$this->set ( "error", "Error of validate!" );
}
} else {
$this->set ( "Countries", $this->Country->find ( "list" ) );
$this->set ( "Cities", $this->City->find ( "list" ) );
}
}
и соотв. вид add.ctp:
<?php
if ( ! empty ( $error ) ) echo $error;
echo "<center><h4>Новый пользователь</h4>";
echo $form->create ( "User", array ( "action" => "add", "enctype" => "multipart/form-data" ) );
echo "Имя: " . $form->input ( "User.first_name", array ( "label" => false ) );
echo "Фамилия: " . $form->input ( "User.last_name", array ( "label" => false ) );
echo "E-mail: " . $form->input ( "User.email", array ( "label" => false ) );
echo $form->select ( "Country.id", $Countries, null, null, array ( "default" => " :: ALL :: " ) ) . "<br>";
echo $form->select ( "User.city_id", $Cities, null, null, array ( "default" => " :: ALL :: " ) ) . "<br>";
echo "Фото: " . $form->file ( "User.photo", array ( "label" => false ) );
echo $form->end ( "Добавить" );
echo $ajax->observeField ( "CountryId", array ( "url" => "/cities/update_cities/", "update" => "UserCityId", "frequency" => 0.5 ));
}
?>
Что бы не вводил в поля, результат один – валидация проходит успешно. |