I'm working on a contact form with Codeigniter 4 and SQL Database. The form will submit through the button, so whenever it is clicked, it is validating, and if all of the fields are filled out, it has no trouble displaying the message and save the information to the database, but when the fields are empty, it does not display the error message, and it continues to state that the file does not exist.
Well, I'm stuck with the error message now. I'm not sure what wrong with the code. Am I missing something?
Can anybody help me with this? I appreciate all the help I can get.
Below are my codes:
App/config/Routes.php
$routes->get('contact', 'Contact::contact');
$routes->post('contact/save', 'Contact::save');
App/Controller/Contact.php
<?php
namespace App\Controllers;
use App\Models\ContactModel;
class Contact extends BaseController
{
public function __construct()
{
helper(['url', 'form']);
}
//CONTACT PAGE
public function contact()
{
$data = [
'meta_title' => 'Contact | MFD',
];
return view('page_templates/contact', $data);
}
//SAVE
public function save()
{
$validation = $this->validate([
'name' => [
'rules' => 'required',
'errors' => [
'required' => 'Your full name is required'
]
],
'email' => [
'rules' => 'required|valid_email|is_unique[users.email]',
'errors' => [
'required' => 'Email is required',
'valid_email' => 'You must enter a valid email',
]
],
'title' => [
'rules' => 'required',
'errors' => [
'required' => 'Title is required',
]
],
'content' => [
'rules' => 'required',
'errors' => [
'required' => 'Content is required',
]
],
]);
if (!$validation) {
return view('contact', ['validation' => $this->validator]);
} else {
// Let's Register user into db
$name = $this->request->getPost('name');
$email = $this->request->getPost('email');
$title = $this->request->getPost('title');
$content = $this->request->getPost('content');
$values = [
'name' => $name,
'email' => $email,
'title' => $title,
'content' => $content,
];
$contactModel = new ContactModel();
$query = $contactModel->insert($values);
if ($query) {
return redirect()->to('contact')->with('success', 'Your message are successful sent');
} else {
return redirect()->to('contact')->with('fail', 'Something went wrong');
}
}
}
}
App/Views/page_templates/contact.php
<form action="<?= base_url('contact/save'); ?>" method="post" role="form" class="php-email-form">
<?= csrf_field(); ?>
<?php if (!empty(session()->getFlashdata('error'))) : ?>
<div class="alert alert-danger"><?= session()->getFlashdata('error'); ?></div>
<?php endif ?>
<?php if (!empty(session()->getFlashdata('success'))) : ?>
<div class="alert alert-success"><?= session()->getFlashdata('success'); ?></div>
<?php endif ?>
<div class="row">
<div class="col-md-6 form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" value="<?= set_value('name'); ?>">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'name') : '' ?></span>
</div>
<div class="col-md-6 form-group mt-3 mt-md-0">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" value="<?= set_value('email'); ?>">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'email') : '' ?></span>
</div>
</div>
<div class="form-group mt-3">
<input type="text" class="form-control" name="title" id="title" placeholder="Title">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'title') : '' ?></span>
</div>
<div class="form-group mt-3">
<textarea class="form-control" name="content" rows="5" wrap="hard" placeholder="Message"></textarea>
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'content') : '' ?></span>
</div>
<div style="height: 10px;"></div>
<div class="text-left button">
<button type="submit" name="submit">Send Message</button>
</div>
<div style="height: 10px;"></div>
</form>
Okay, I have fixed the problem with my code. It's really is a small mistake or I could say careless mistake haha feel so dumb right now but all that I need to change and make it work.
Is this part: return view('contact', ['validation' => $this->validator]);
Instead of doing it like that I actually miss placing the file to call the view page properly so the code I only added my the folder name that is
page_templates
and if I include it in the code it should look like this:-
return view('page_template/contact', ['validation' => $this->validator]);
and it works after that haha.
Related
I'm running codeigniter 4.1.5 and php 8.1.0 and when i try to input data this is the error i got
Uncaught ErrorException: strtolower(): Passing null to parameter #1 ($string) of type string is deprecated in C:\xampp\htdocs\cikaryawan\system\Validation\FormatRules.php:253
Stack trace:
#0 [internal function]: CodeIgniter\Debug\Exceptions->errorHandler()
#1 C:\xampp\htdocs\cikaryawan\system\Validation\FormatRules.php(253):
strtolower()
#2 C:\xampp\htdocs\cikaryawan\system\HTTP\RequestTrait.php(151): CodeIgniter\Validation\FormatRules->valid_ip()
#3 C:\xampp\htdocs\cikaryawan\app\Views\errors\html\error_exception.php(206):
CodeIgniter\HTTP\Request->getIPAddress()
#4 C:\xampp\htdocs\cikaryawan\system\Debug\Exceptions.php(229): include('...')
#5 C:\xampp\htdocs\cikaryawan\system\Debug\Exceptions.php(232): CodeIgniter\Debug\Exceptions->CodeIgniter\Debug{closure}()
#6 C:\xampp\htdocs\cikaryawan\system\Debug\Exceptions.php(116): CodeIgniter\Debug\Exceptions->render()
#7 [internal function]: CodeIgniter\Debug\Exceptions->exceptionHandler()
#8 {main}
in my .env folder
CI_ENVIRONMENT = development
database.default.hostname = localhost
database.default.database = cikaryawanauth
database.default.username = root
database.default.password =
database.default.DBDriver = MySQLi
database.default.DBPrefix =
this is my register.php
<body>
<div class="container">
<div class="row" style="margin-top:45px">
<div class="col-md-4 col-md-offset-4">
<h4>Sign Up</h4>
<hr>
<form action="<?= base_url('auth/save') ?>" method="post">
<?= csrf_field(); ?>
<div class="form-group">
<label for="">Name</label>
<input type="text" class="form-control" name="name" placeholder="Enter your name" value="<?= set_value('name'); ?>">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'name') : '' ?></span>
</div>
<div class="form-group">
<label for="">Email</label>
<input type="email" class="form-control" name="email" placeholder="Enter your email" value="<?= set_value('email'); ?>">
<span class=" text-danger"><?= isset($validation) ? display_error($validation, 'email') : '' ?></span>
</div>
<div class="form-group">
<label for="">Password</label>
<input type="password" class="form-control" name="password" placeholder="Enter password">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'password') : '' ?></span>
</div>
<div class="form-group">
<label for="">Confirm Password</label>
<input type="password" class="form-control" name="cpassword" placeholder="Confirm password">
<span class="text-danger"><?= isset($validation) ? display_error($validation, 'cpassword') : '' ?></span>
</div>
<div class="form-group">
<button class="btn btn-primary btn-block" type="submit">Sign Up</button>
</div>
I already have account, login now
</form>
</div>
</div>
</div>
</body>
this is my auth.php as controller
<?php
namespace App\Controllers;
class Auth extends BaseController
{
public function __construct()
{
helper(['url', 'form']);
}
public function index()
{
return view('auth/login');
}
public function register()
{
return view('auth/register');
}
public function save()
{
$validation = $this->validate([
'name' => [
'rules' => 'required',
'errors' => [
'required' => 'Your name is required'
]
],
'email' => [
'rules' => 'required|valid_emails|is_unique[users.email]',
'errors' => [
'required' => 'Email is required',
'valid_email' => 'You must enter a valid email',
'is_unique' => 'Email already taken'
]
],
'password' => [
'rules' => 'required|min_length[5]|max_length[12]',
'errors' => [
'required' => 'Password is required',
'min_lenght' => 'Password must have atleast 5 character',
'max_lenght' => 'Password must not exceed 12 character'
]
],
'cpassword' => [
'rules' => 'required|min_length[5]|max_length[12]|matches[password]',
'errors' => [
'required' => 'Password is required',
'min_lenght' => 'Password must have atleast 5 character',
'max_lenght' => 'Password must not exceed 12 character',
'matches' => 'Confirms password not matches to password'
]
]
]);
if (!$validation) {
return view('auth/register', ['validation' => $this->validator]);
} else {
$name = $this->request->getPost('name');
$email = $this->request->getPost('email');
$password = $this->request->getPost('password');
$values = [
'name' => $name,
'email' => $email,
'password' => $password,
];
$userModels = new \App\Models\UsersModel();
$query = $userModels->insert($values);
if (!$query) {
return redirect()->back()->with('fail', 'something went wrong');
} else {
return redirect()->to('register')->with('success', 'You are now registered');
}
}
}
}
this is what i put in form_helper.php in helpers folder
<?php
function display_error($validation, $field)
{
if ($validation->hasError($field)) {
return $validation->getError($field);
} else {
return false;
}
}
what did i do wrong?
CodeIgniter 4.1.5 does not support PHP 8.1 yet.
Please Use 8.0
or Use develop branch of CI4
https://github.com/codeigniter4/CodeIgniter4/pull/4883
https://github.com/codeigniter4/CodeIgniter4/issues/5436
https://forum.codeigniter.com/thread-80490-post-391352.html#pid391352
https://forum.codeigniter.com/thread-80413.html
You can add the null coalescing operator to this line of code:
$method = strtolower($method);
Replace it with:
$method = strtolower($method ?? '');
This change should be made in the library/RestController file line no 835
i am new to codeigniter 4, i am currently building a registration form. when i submit reg form it give error of required fields even if i fill all fields with correct data.
here is my code snippet
function register() {
$data = [];
helper(['form']);
if($this->request->getMethod() == "post"){
$validation = \Config\Services::validation();
$validation->setRules([
"firstname" => ["label" => "First Name", "rules" => "required|min_length[3]|max_length[20]"],
"lastname" => ["label" => "Last Name", "rules" => "required|min_length[3]|max_length[20]"],
"email" => ["label" => "Email", "rules" => "required|min_length[3]|max_length[20]|valid_email|is_unique[users.email]"],
"password" => ["label" => "Password", "rules" => "required|min_length[8]|max_length[20]"],
"password_confirm" => ["label" => "Confirm Password", "rules" => "matches[password]"],
]);
if($validation->run()){
$user = new UserModel();
$userdata = [
"firstname" => $this->request->getVar("firstname"),
"lastname" => $this->request->getVar("lastname"),
"email" => $this->request->getVar("email"),
"password_confirm" => $this->request->getVar("password_confirm"),
];
$user->save($userdata);
$session = session();
$session->setFlashData("success", "Successful Registration");
return redirect()->to('/');
}else{
$data["validation"] = $validation->getErrors();
}
}
echo view('templates/header', $data);
echo view('register');
echo view('templates/footer');
}
this is the registration form i am trying to validate.
<form class="" action="/register" method="post">
<div class="row">
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="firstname">First Name</label>
<input type="text" class="form-control" name="firstname" id="firstname" value="<?= set_value('firstname') ?>">
<small class="text-danger"><?= isset($validation) ? $validation['firstname'] : null; ?></small>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="lastname">Last Name</label>
<input type="text" class="form-control" name="lastname" id="lastname" value="<?= set_value('lastname'); ?>">
<small class="text-danger"><?= isset($validation) ? $validation['lastname'] : null ; ?></small>
</div>
</div>
<div class="col-12">
<div class="form-group">
<label for="email">Email address</label>
<input type="text" class="form-control" name="email" id="email" value="<?= set_value('email') ?>">
<small class="text-danger"><?= isset($validation) ? $validation['email'] : null ; ?></small>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" name="password" id="password" value="">
<small class="text-danger"><?= isset($validation) ? $validation['password'] :null ; ?></small>
</div>
</div>
<div class="col-12 col-sm-6">
<div class="form-group">
<label for="password_confirm">Confirm Password</label>
<input type="password" class="form-control" name="password_confirm" id="password_confirm" value="">
<small class="text-danger"><?= isset($validation) ? $validation['password_confirm'] :null ; ?></small>
</div>
</div>
</div>
<div class="row">
<div class="col-12 col-sm-4">
<button type="submit" class="btn btn-primary">Register</button>
</div>
<div class="col-12 col-sm-8 text-right">
Already have an account
</div>
</div>
</form>
and here is the output i am getting even if i fill all fields.
If you want to always validate data going into that model, you might want to consider doing the validation inside the model:
https://codeigniter.com/user_guide/models/model.html#validating-data
In case you want to validate the data outside the model you have to tell the validation service on where the data is, because it can be POST, GET or even just an array that you have from something else.
In your case you need to validate the data with your request.
https://codeigniter.com/user_guide/libraries/validation.html?highlight=validation#withrequest
So your validation code should be something like:
if($validation->withRequest($this->request)->run()){ }
This will look for the data in both GET and POST.
If you want to specify and only use POST
if($validation->withRequest($this->request->getPost())->run()){ }
try this style
function register()
{
$data = [];
helper(['form']);
if ($this->request->getMethod() == "post") {
$validation = \Config\Services::validation();
$rules = [
"firstname" => [
"label" => "First Name",
"rules" => "required|min_length[3]|max_length[20]"
],
"lastname" => [
"label" => "Last Name",
"rules" => "required|min_length[3]|max_length[20]"
],
"email" => [
"label" => "Email",
"rules" => "required|min_length[3]|max_length[20]|valid_email|is_unique[users.email]"
],
"password" => [
"label" => "Password",
"rules" => "required|min_length[8]|max_length[20]"
],
"password_confirm" => [
"label" => "Confirm Password",
"rules" => "matches[password]"
]
];
if ($this->validate($rules)) {
$user = new UserModel();
$userdata = [
"firstname" => $this->request->getVar("firstname"),
"lastname" => $this->request->getVar("lastname"),
"email" => $this->request->getVar("email"),
"password_confirm" => $this->request->getVar("password_confirm"),
];
$user->save($userdata);
$session = session();
$session->setFlashData("success", "Successful Registration");
return redirect()->to('/');
} else {
$data["validation"] = $validation->getErrors();
}
}
echo view('templates/header', $data);
echo view('register');
echo view('templates/footer');
}
just put your rules inside an array and pass it to controller validate function..
i hope it's work
I am trying to get form values and passing it in my model to store in the database. Whenever I end up submitting the form, I always get the values as NULL.
form.php
<form action="http://localhost/hbp/review/submit/" method="post">
<input type="hidden" name='<?php echo $this->security->get_csrf_token_name(); ?>' value='<?php echo $this->security->get_csrf_hash(); ?>'>
<div class="form-group">
<label for="exampleInputEmail1">Title</label>
<!-- <input type="text" name="title">-->
<?php echo form_input(['id' => 'title', 'name' => 'title', 'class' => 'form-control', 'placeholder' => 'Enter your review title']); ?>
<small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Review</label>
<!-- <textarea rows="10" cols="100" name="message"></textarea>-->
<?php echo form_textarea(['id' => 'message', 'name' => 'message', 'class' => 'form-control', 'placeholder' => 'Enter your review title', 'rows' => '10', 'cols' => '100']); ?>
</div>
<button name="submit">Hello</button>
</form>
I have tried using both form_input() and simple HTML syntax for rendering form fields. None of them worked.
controller.php
if ($gClient->getAccessToken()) {
$userProfile = $google_oauthV2->userinfo->get();
$name = $userProfile['name'];
$email = $userProfile['email'];
$picture = $userProfile['picture'];
$this->load->library('form_validation');
$this->form_validation->set_rules('title', 'title', 'required');
$this->form_validation->set_rules('message', 'message', 'required');
if ($this->form_validation->run() == FALSE){
echo "error in your form ";
$this->session->set_flashdata('review_error', 'Please fill all the fields!');
$this->load->view('pages/review');
} else {
$this->session->set_flashdata('review_success', 'Thank you for you ');
$this->review_model->set_review($name, $email, $picture);
redirect(base_url() . 'review/');
}
echo "just dies";
model.php
class Review_model extends CI_Model
{
public function set_review($google_name, $google_email, $google_image)
{
$data = array('review_title' => $this->input->post('title'),
'review_content' => $this->input->post('message'),
'email' => $google_email,
'name' => $google_name,
'image_url' => $google_image);
return $this->db->insert('reviews', $data);
}
public function get_review(){
$query = $this->db->get('reviews');
return $query->result_array();
}
}
I keep getting the review_title can't be NULL.
When I submit this form all things are well n good but I want the controller function not shows in url. I want all things are done in same page. Still the url show
localhost/Naveen/CodeIgniter/welcome/insertform
but I don't want the form_open('') show's in url so how it is possible?
controller welcome.php
public function insertform()
{
if (isset($_POST['mysmt']))
{
$this->form_validation->set_rules('fname', 'Name', 'required');
$this->form_validation->set_rules('femail', 'Email', 'trim|required|valid_email');
$this->form_validation->set_rules('fmobile', 'Mobile', 'required');
if ($this->form_validation->run() == FALSE)
{
$this->load->view('welcome_view');
}
else
{
$_POST['fname'];
$_POST['femail'];
$_POST['fmobile'];
if($this->test_model->insert('user_accounts',array('',$_POST['fname'],$_POST['femail'],$_POST['fmobile'])))
{
$success['success']="Thanks For Join Us";
$this->load->view('welcome_view',$success);
}
}
}
else
{
$this->load->view('welcome_view');
}
}
view welcome_view.php
<?php echo form_open('welcome/insertform'); // ?>
<div class="form-group">
<?php
if(isset($success))
{?>
<input type="button" class="form-control btn-success" value="<?php echo $success; ?>">
<?php }
else
{
echo "";
}?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Name <?php echo form_error('fname'); ?></label>
<?php
$entername = array(
'name' => 'fname',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Name',
'class' => 'form-control',
);
echo form_input($entername); ?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Email <?php echo form_error('femail'); ?></label>
<?php
$enteremail = array(
'name' => 'femail',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Email',
'class' => 'form-control',
);
echo form_input($enteremail); ?>
</div>
<div class="form-group">
<label class="control-label" for="focusedInput">Mobile <?php echo form_error('fmobile'); ?></label>
<?php
$entermobile = array(
'name' => 'fmobile',
'value' => '',
'maxlength' => '100',
'placeholder' => 'Enter Mobile',
'class' => 'form-control',
);
echo form_input($entermobile); ?>
</div>
<div class="form-group">
<?php
$f_formsmt = array(
'name' => 'mysmt',
'value' => 'Submit Form',
'class' => 'form-control btn btn-success',
);
echo form_submit($f_formsmt); ?>
</div>
<?php echo form_close(); ?>
You handle it through applications/config/routes.php
consider you url: localhost/Naveen/CodeIgniter/welcome/insertform
$route['add'] = 'welcome/insertform';
now you can use localhost/Naveen/CodeIgniter/add
You can change whatever the string you want for any controller/function using routes.
Hope you understood :)
You need to add the url in route and add link to your link href
In route.php add:
$route['custom_url'] = 'controller/method';
I used Header("location:../{whatever-filename}")
Then gave that "whatever-filename" the proper routing in the config/routes.php.
Routing without using the redirect function would load the appropriate page but the url would still show the controller and method you're using.
Hope it helps
user data has been lost if there is a validation error and error validation messages are also not showing on front end.
Here is my controller
public function add() {
$this->theme = 'Front';
if (!empty($this->logged_in)) {
return $this->redirect(Router::url('/', true) . $this->logged_in['Role']['alias']);
}
$this->set('title_for_layout', __('Create An Account'));
// hanlde post data
if ($this->request->is("post")) {
if (!empty($this->request->data)) {
$this->User->set($this->request->data);
if (!$this->User->validates()) {
$this->User->validationErrors;
$this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
$this->redirect(Router::url('/', true) . 'sign-up/');
}
}
// die('sdsddsaf');
// if (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 2) {
unset($this->request->data['User']['specialization_id']);
$randomSt = $this->Custom->randomString(28);
$this->request->data['User']['activation_key'] = $randomSt;
// }
if ($this->User->save($this->request->data)) {
$user = $this->request->data;
if (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3) {
$this->Session->setFlash(__('Thank you for registering with us. Please check your email inbox/junk.'), 'message', array('class' => 'successs'));
// set mail variable
$to = $this->request->data['User']['email'];
$fname = $this->request->data['User']['first_name'];
$lname = $this->request->data['User']['last_name'];
//$activelink = SITEURL . 'activate?code=' . $randomSt;
$siteurl = Configure::read('Site.title');
$replace = array($fname, $lname, $siteurl);
// Send mail on Registration
$this->sendMail($to, 'RegisterDoctor', $replace);
} else {
$this->Session->setFlash(__('Thank you for registering with us. Please check your e-mail inbox/junk as your e-mail confirmation has just been sent.'), 'message', array('class' => 'successs'));
$activelink = Router::url('/', true) . 'activate/' . $this->User->id . '/' . $randomSt;
// set mail variable
$to = $this->request->data['User']['email'];
$fname = $this->request->data['User']['first_name'];
$lname = $this->request->data['User']['last_name'];
//$activelink = SITEURL . 'activate?code=' . $randomSt;
$siteurl = Configure::read('Site.title');
$replace = array($fname, $lname, $activelink, $siteurl);
// Send mail on Registration
$this->sendMail($to, 'Register', $replace);
}
$this->redirect(array('controller' => 'users', 'action' => 'login'));
}
}
$reponse = array();
$roles = $this->User->rolesSignup();
$genders = $this->User->genders();
$specializations = $this->User->specializations();
$this->set(compact('roles', 'genders', 'specializations'));
}
And below is my view
<?php
echo $this->Form->create('User', array(
'inputDefaults' => array('div' => false, 'label' => false, 'fieldset' => false),
'class' => 'ac p10',
// 'data-togglesd' => "validator",
'role' => 'form',
'type' => 'file',
'onSubmit' => 'return checksubmitPass()'
)
);
?>
<div class="ph10">
<div class="btn-group radio_group w100" data-toggle="buttons">
<label class="btn btn-primary col-md-6 <?php echo (empty($this->request->data['User']['role_id']) || #$this->request->data['User']['role_id'] == 2 ) ? 'active' : ""; ?> btn_roles">
<input type="radio" name="data[User][role_id]" value="2" class="role_id" <?php echo (empty($this->request->data['User']['role_id']) || #$this->request->data['User']['role_id'] == 2 ) ? 'checked="checked"' : ""; ?>> User
</label>
<label class="btn btn-primary col-md-6 <?php echo (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3 ) ? 'active' : ""; ?> btn_roles">
<input type="radio" name="data[User][role_id]" value="3" class="role_id" <?php echo (!empty($this->request->data['User']['role_id']) && $this->request->data['User']['role_id'] == 3 ) ? 'checked="checked"' : ""; ?>> Doctor
</label>
</div>
</div>
<div class="form-group p10 cr">
<div class="cm upload_img round"><img src="<?php echo $this->webroot; ?>img/user_avtar(upload).png" class="user_pic_upload">
<?php echo $this->Form->file('image', array('class' => 'user_img', 'data-fv-file' => 'true', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-field' => 'data[User][image]')); ?>
</div>
<small style="display: none;" class="help-block" data-fv-validator="notEmpty" data-fv-for="data[User][image]" data-fv-result="VALID">This field cannot be left blank.</small>
</div>
<div class="form-group mt10 cr">
<div class="clearfix">
<div class="col-lg-6 first_name"><?php echo $this->Form->input('first_name', array('class' => 'form-control', 'placeholder' => 'First Name', 'data-stripe' => "alphabet", 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-regexp' => 'true', 'data-fv-regexp-regexp' => "^[a-zA-Z\s]+$", 'data-fv-regexp-message' => "The first name can consist of alphabetical characters and spaces only")); ?></div>
<div class="col-lg-6 last_name"><?php echo $this->Form->input('last_name', array('class' => 'form-control', 'placeholder' => 'Last Name', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-regexp' => 'true', 'data-fv-regexp-regexp' => "^[a-zA-Z\s]+$", 'data-fv-regexp-message' => "The last name can consist of alphabetical characters and spaces only", 'required')); ?></div>
</div>
</div>
<div class="form-group ph10 mt10">
<?php echo $this->Form->input('email', array('class' => "form-control", 'placeholder' => 'Email Address', 'data-fv-notempty-message' => __('notEmpty'), 'data-fv-emailaddress-message' => __('validemail'))); ?>
</div>
<div class="form-group mt10 ph10 row_org">
<div id="dropdown-menu">
<?php echo $this->Form->input('specialization_id', array('empty' => 'Select Speciallization', 'class' => 'selectpicker form-control', 'data-fv-notempty-message' => __('notEmpty'), 'required')); ?>
</div>
</div>
<div class="form-group ph10 mt10">
<?php echo $this->Form->input('password', array('class' => "form-control", 'placeholder' => 'Password', 'type' => 'password', 'data-fv-notempty-message' => __('notEmpty'))); ?>
</div>
<span id="result"></span>
<div class="form-group ph10 mt10">
<?php
echo $this->Form->input('verify_password', array(
'class' => "form-control",
'type' => 'password',
'placeholder' => 'Confirm Password',
'data-fv-notempty-message' => __('notEmpty'),
'data-fv-identical' => "true",
'data-fv-identical-field' => "data[User][password]",
'data-fv-identical-message' => __('passwordNotMatch')
)
);
?>
</div>
<div class="form-group mt10">
<div class="clearfix">
<div class="col-lg-6 mobile-custom">
<?php echo $this->Form->input('mobile', array('class' => 'form-control phonenumber', 'placeholder' => 'Mobile number', 'maxlength' => '14', 'minlength' => '10', 'data-fv-stringlength-message' => 'The mobile number must be 10 to 14 characters long', 'data-fv-numeric-message' => __('Please enter valid mobile numbers'), 'data-fv-numeric' => "true")); ?>
</div>
<div class="col-lg-6" id="dropdown-menu">
<?php //echo $this->Form->input('gender', array('class' => 'form-control selectpicker', 'data-fv-notempty-message' => __('notEmpty'),'empty' => 'I Am','error' => false, 'required')); ?>
<select id="UserGender" title="I Am" required="required" data-fv-notempty-message="This field cannot be left blank." class="form-control" name="data[User][gender]" style="display: none;" data-fv-field="data[User][gender]">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Other">Other</option>
</select>
</div>
</div>
</div>
<div class="form-group ph10 mt10"><button type="submit" class="btn btn-info w100">Sign Up</button></div>
<p class="mt10"><small>By Signing up you are agree with <br>Terms & Conditions and Privacy Policy</small></p>
<p class="mt10 signup-text"><strong>Already on HealthDrop?</strong></p>
<div class="ph10">
<?php echo $this->Html->link('Sign In', Router::url('/', true) . 'login', array('class' => 'btn deep btn-primary w100')); ?>
</div>
<?php echo $this->Form->end(); ?>
it was working before bu suddenly stop to showing error and start loosing data.
I have check with this question but it is pointing out at field names in input and I have correct ones.
Any help appreciate.
You lost your data because there is redirect to sign-up url.
$this->redirect(Router::url('/', true) . 'sign-up/');
And after redirection
$this->request->data
is empty because you perform new request, the same with errors
According to your code there are two mistakes I found
you are using ! for validating the validations
Save method itself validate the data so no need to validate again.
So your code should be look like
if ($this->User->save($this->request->data)) {
//your code
}else{
$this->Session->setFlash(__('The Information could not be saved. Please, try again.'), 'message', array('class' => 'danger'));
$this->redirect(Router::url('/', true) . 'sign-up/');
}
and validation error messages are automatically assign to view, no need to worry about them