Add custom validator on phone number of customer addres book form (theme) - magento2

I am completely new at magento 2. I want to add custom validation on phone number in customer address book form which can be seen after login by customer. Below is UI.
Form UI:
customer address book form after login
This UI form code is located under app/design/frontend/vendor/template/address/edit.phtml
Code looks like below:
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/** #var \Magento\Customer\Block\Address\Edit $block */
?>
<?php $_company = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Company::class) ?>
<?php $_telephone = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Telephone::class) ?>
<?php $_fax = $block->getLayout()->createBlock(\Magento\Customer\Block\Widget\Fax::class) ?>
<form class="form-address-edit"
action="<?= $block->escapeUrl($block->getSaveUrl()) ?>"
method="post"
id="form-validate"
enctype="multipart/form-data"
data-hasrequired="<?= $block->escapeHtmlAttr(__('* Required Fields')) ?>"
data-mage-init='{"validation":{
"rules": {
"telephone": {
"required":false,
"digits": digits
}
}
}}'>
<fieldset class="fieldset">
<legend class="legend"><span><?= $block->escapeHtml(__('Contact Information')) ?></span></legend><br>
<?= $block->getBlockHtml('formkey') ?>
<input type="hidden" name="success_url" value="<?= $block->escapeUrl($block->getSuccessUrl()) ?>">
<input type="hidden" name="error_url" value="<?= $block->escapeUrl($block->getErrorUrl()) ?>">
<?= $block->getNameBlockHtml() ?>
<?php if ($_company->isEnabled()) : ?>
<?= $_company->setCompany($block->getAddress()->getCompany())->toHtml() ?>
<?php endif ?>
<?php if ($_telephone->isEnabled()) : ?>
<?= $_telephone->setTelephone($block->getAddress()->getTelephone())->toHtml() ?>
<?php endif ?>
<?php if ($_fax->isEnabled()) : ?>
<?= $_fax->setFax($block->getAddress()->getFax())->toHtml() ?>
<?php endif ?>
</fieldset>
<fieldset class="fieldset">
<legend class="legend"><span><?= $block->escapeHtml(__('Address')) ?></span></legend><br>
<?php $_streetValidationClass = $this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('street'); ?>
<div class="field street required">
<label for="street_1" class="label">
<span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?></span>
</label>
<div class="control">
<input type="text"
name="street[]"
value="<?= $block->escapeHtmlAttr($block->getStreetLine(1)) ?>"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('street') ?>"
id="street_1"
class="input-text <?= $block->escapeHtmlAttr($_streetValidationClass) ?>"/>
<div class="nested">
<?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
<?php for ($_i = 1, $_n = $this->helper(\Magento\Customer\Helper\Address::class)->getStreetLines(); $_i < $_n; $_i++) : ?>
<div class="field additional">
<label class="label" for="street_<?= /* #noEscape */ $_i + 1 ?>">
<span><?= $block->escapeHtml(__('Street Address %1', $_i + 1)) ?></span>
</label>
<div class="control">
<input type="text" name="street[]"
value="<?= $block->escapeHtmlAttr($block->getStreetLine($_i + 1)) ?>"
title="<?= $block->escapeHtmlAttr(__('Street Address %1', $_i + 1)) ?>"
id="street_<?= /* #noEscape */ $_i + 1 ?>"
class="input-text <?= $block->escapeHtmlAttr($_streetValidationClass) ?>">
</div>
</div>
<?php endfor; ?>
</div>
</div>
</div>
<?php if ($this->helper(\Magento\Customer\Helper\Address::class)->isVatAttributeVisible()) : ?>
<div class="field taxvat">
<label class="label" for="vat_id">
<span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('vat_id') ?></span>
</label>
<div class="control">
<input type="text"
name="vat_id"
value="<?= $block->escapeHtmlAttr($block->getAddress()->getVatId()) ?>"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('vat_id') ?>"
class="input-text <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('vat_id')) ?>"
id="vat_id">
</div>
</div>
<?php endif; ?>
<div class="field city required">
<label class="label" for="city"><span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('city') ?></span></label>
<div class="control">
<select name="city" id="city" class="<?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('city')) ?>" title="<?= $block->escapeHtmlAttr(__('City')) ?>" data-validate="{'validate-select':true}">
<?php
$cityArr = $block->getCityFromAPI();
if( is_array($cityArr) && !empty($cityArr) ){
foreach( $cityArr as $city ){
$selected = ( $block->escapeHtmlAttr($block->getAddress()->getCity()) == $city['value'] ) ? 'selected="selected"' : '' ;
echo '<option value="'.$city['value'].'" '.$selected.'>'.$city['label'].'</option>';
}
}
?>
</select>
</div>
</div>
<div class="field region required">
<label class="label" for="region_id">
<span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?></span>
</label>
<div class="control">
<select id="region_id" name="region_id"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>"
class="validate-select region_id" <?= /* #noEscape */ !$block->getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>>
<option value=""><?= $block->escapeHtml(__('Please select a region, state or province.')) ?></option>
</select>
<input type="text"
id="region"
name="region"
value="<?= $block->escapeHtmlAttr($block->getRegion()) ?>"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('region') ?>"
class="input-text validate-not-number-first <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('region')) ?>"<?= !$block->getConfig('general/region/display_all') ? ' disabled="disabled"' : '' ?>/>
</div>
</div>
<div class="field zip required">
<label class="label" for="zip">
<span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?></span>
</label>
<div class="control">
<input type="text"
name="postcode"
value="<?= $block->escapeHtmlAttr($block->getAddress()->getPostcode()) ?>"
title="<?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('postcode') ?>"
id="zip"
class="input-text validate-zip-international <?= $block->escapeHtmlAttr($this->helper(\Magento\Customer\Helper\Address::class)->getAttributeValidationClass('postcode')) ?>">
<div role="alert" class="message warning" style="display:none">
<span></span>
</div>
</div>
</div>
<div class="field country required">
<label class="label" for="country"><span><?= /* #noEscape */ $block->getAttributeData()->getFrontendLabel('country_id') ?></span></label>
<div class="control">
<?= $block->getCountryHtmlSelect() ?>
</div>
</div>
<?php if ($block->isDefaultBilling()) : ?>
<div class="message info">
<span><?= $block->escapeHtml(__("It's a default billing address.")) ?></span>
</div>
<?php elseif ($block->canSetAsDefaultBilling()) : ?>
<div class="field choice set billing">
<input type="checkbox" id="primary_billing" name="default_billing" value="1" class="checkbox">
<label class="label" for="primary_billing">
<span><?= $block->escapeHtml(__('Use as my default billing address')) ?></span>
</label>
</div>
<?php else : ?>
<input type="hidden" name="default_billing" value="1" />
<?php endif; ?>
<?php if ($block->isDefaultShipping()) : ?>
<div class="message info">
<span><?= $block->escapeHtml(__("It's a default shipping address.")) ?></span>
</div>
<?php elseif ($block->canSetAsDefaultShipping()) : ?>
<div class="field choice set shipping">
<input type="checkbox" id="primary_shipping" name="default_shipping" value="1" class="checkbox">
<label class="label" for="primary_shipping">
<span><?= $block->escapeHtml(__('Use as my default shipping address')) ?></span>
</label>
</div>
<?php else : ?>
<input type="hidden" name="default_shipping" value="1">
<?php endif; ?>
</fieldset>
<div class="actions-toolbar">
<div class="primary">
<button type="submit"
class="action save primary"
data-action="save-address"
title="<?= $block->escapeHtmlAttr(__('Save Address')) ?>">
<span><?= $block->escapeHtml(__('Save Address')) ?></span>
</button>
</div>
<div class="secondary">
<a class="action back btn btn-light" href="<?= $block->escapeUrl($block->getBackUrl()) ?>">
<span><?= $block->escapeHtml(__('Go back')) ?></span>
</a>
</div>
</div>
</form>
<script type="text/x-magento-init">
{
"#form-validate": {
"addressValidation": {
"postCodes": <?= /* #noEscape */ $block->getPostCodeConfig()->getSerializedPostCodes(); ?>
}
},
"#country": {
"regionUpdater": {
"optionalRegionAllowed": <?= /* #noEscape */ $block->getConfig('general/region/display_all') ? 'true' : 'false' ?>,
"regionListId": "#region_id",
"regionInputId": "#region",
"postcodeId": "#zip",
"form": "#form-validate",
"regionJson": <?= /* #noEscape */ $this->helper(\Magento\Directory\Helper\Data::class)->getRegionJson() ?>,
"defaultRegion": "<?= (int) $block->getRegionId() ?>",
"countriesWithOptionalZip": <?= /* #noEscape */ $this->helper(\Magento\Directory\Helper\Data::class)->getCountriesWithOptionalZip(true) ?>
}
}
}
</script>
So How can I add validation on phone number field in below code?
<?php if ($_telephone->isEnabled()) : ?>
<?= $_telephone->setTelephone($block->getAddress()->getTelephone())->toHtml() ?>
<?php endif ?>

Related

update in codeigniter 3, it's run but database didnt changed

i dont understand with my code. it's run, and work. but when i see in database didnt changed.
here's my controller, function perbarui for get id data, get the data id from model (MMobil).
public function perbarui($id = NULL){
$this->load->model('merek');
$this->load->library('form_validation');
$data['merk'] = $this->merek->getList();
$data['detail'] = $this->MMobil->detail($id);
$submit = $this->input->post('submit');
print_r($data);
if ($submit) {
$nomor_kendaraan = $this->input->post('nomor_kendaraan');
$nomor_mesin = $this->input->post('nomor_mesin');
$id_merek = $this->input->post('id_merek');
$tahun_beli = $this->input->post('tahun_beli');
$nama_mobil = $this->input->post('nama_mobil');
$this->form_validation->set_rules('nama_mobil', 'Nama_Mobil', 'required');
$this->form_validation->set_rules('nomor_kendaraan', 'Nomor_Kendaraan', 'required');
$data['detik'] = $this->MMobil->setData($nomor_kendaraan, $nomor_mesin, $id_merek, $tahun_beli, $nama_mobil);
if ($this->form_validation->run() == FALSE) {
$data['errors'] = TRUE;
}else{
$data['detail'] = $this->MMobil->detail($id);
print_r($data);
}
//redirect('Master');
}
$this->load->view('master-detail-mobil', $data);
}
here's my models. function setData for storage variable after click submit. function edit get id data and get variable arrayData from function setData
public function setData($nomor_kendaraan, $nomor_mesin, $id_merek, $tahun_beli, $nama_mobil){
$this->nomor_kendaraan = $nomor_kendaraan;
$this->nomor_mesin = $nomor_mesin;
$this->id_merek = $id_merek;
$this->tahun_beli = $tahun_beli;
$this->nama_mobil = $nama_mobil;
}
public function edit($id){
$arrayData = array(
'nomor_kendaraan' => $this->nomor_kendaraan,
'nomor_mesin' => $this->nomor_mesin,
'id_merek' => $this->id_merek,
'tahun_beli' => $this->tahun_beli,
'nama_mobil' => $this->nama_mobil,
);
$this->db->where('nomor_mesin', $id);
return $this->db->update($this->table,$arrayData);
}
here's my views
<?php $this->load->view('header.php'); ?>
<?php
echo"<h4>UPDATE DATA</h4>";
?>
<form class="form-horizontal" role="form" method="post" action="<?php echo site_url()?>/Master/perbarui/">
<div class="form-group">
<label class="control-label col-sm-2" for="noken">Nomor Kendaraan :</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="nomor_kendaraan" value="<?php
echo #$detail->nomor_kendaraan;
?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="nomes">Nomor Mesin :</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="nomor_mesin" value="<?php
echo #$detail->nomor_mesin;
?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="merk">Merek Mobil :</label>
<div class="col-sm-10">
<select class="form-control" name="id_merek">
<?php foreach ($merk as $mrk) :?>
<option value="<?php echo $mrk->id_merek ?>">
<?php
echo $mrk->nama_merek;
?>
</option>
<?php endforeach?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="thn">Tahun Beli :</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="tahun_beli" value="<?php
echo #$detail->tahun_beli; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="namo">Nama Mobil :</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="nama_mobil" value="<?php
echo #$detail->nama_mobil; ?>">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="nomes"></label>
<div class="col-sm-2">
<input type="submit" class="form-control" name="submit" value="simpan">
</div>
</div>
</form>
<?php $this->load->view('footer.php'); ?>
and here my pictures before click submit and after click submit. i typed print_r for see the data array. if you see data array detail before click submit, there is data in detail. but after click submit data array detail gone.
enter image description here enter image description here

Codeigniter 3 select form error not showing on form submit

I was following a youtube tutorial on making a codeigniter college management system
but i am stuck at a point.. when i try to submit the form on wamp server
select form fields not showing requied error on form submit
i have checked the name fields of the form inputs matching the database table but still when the form is submitted without any fiilling any fields
the role and gender select field errors doesnt show
here is my code
controller
welcome.php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
/**
* Index Page for this controller.
*
* Maps to the following URL
* http://example.com/index.php/welcome
* - or -
* http://example.com/index.php/welcome/index
* - or -
* Since this controller is set as the default controller in
* config/routes.php, it's displayed at http://example.com/
*
* So any other public methods not prefixed with an underscore will
* map to /index.php/welcome/<method_name>
* #see https://codeigniter.com/user_guide/general/urls.html
*/
public function index()
{
//$this->load->helper('url');
$this->load->view('home');
}
public function adminRegister()
{
$this->load->model('queries');
$roles = $this->queries->getRoles();
// print_r($roles);
// exit();
$this->load->view('register',['roles'=>$roles]);
}
public function adminLogin()
{
echo 'Login';
}
public function adminSignup()
{
//echo 'Registered succesfully';
$this->form_validation->set_rules('username','Username','required');
$this->form_validation->set_rules('email','Email','required');
$this->form_validation->set_rules('gender','Gender','required');
$this->form_validation->set_rules('role_id','Role','required');
$this->form_validation->set_rules('password','Password','required');
$this->form_validation->set_rules('confpwd','Password Again','required');
$this->form_validation->set_error_delimiters('<div class="text-danger">','</div>');
if($this->form_validation->run()){
echo 'validation passed';
}else{
//echo 'validation error';
echo validation_errors();
}
}
}
and views
register.php
<?php include('inc/header.php');?>
<div class="container mt-2">
<?php echo form_open('welcome/adminSignup',['class'=>'form-hoizontal']);?>
<h3 class="text-center display-4">ADMIN REGISTER</h3>
<hr/>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row">
<div class="col-md-3">
<label for="username" class="mt-2">User name:</label>
</div>
<div class="col-md-9">
<!-- <input type="text" class="form-control" placeholder="User name" id="username"> -->
<?php
$data = array(
'type' => 'text',
'name' => 'username',
'placeholder' => 'Enter Username',
'class' => 'form-control'
);
echo form_input($data); ?>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('username','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="email" class="mt-2">Email address:</label>
</div>
<div class="col-md-9">
<!-- <input type="email" class="form-control" placeholder="Enter email" id="email"> -->
<?php
$data = array(
'type' => 'email',
'name' => 'email',
'placeholder' => 'Enter Email',
'class' => 'form-control'
);
echo form_input($data); ?>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('email','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="gender" class="mt-2">Gender:</label>
</div>
<div class="col-md-9">
<!-- <input type="email" class="form-control" placeholder="Enter email" id="email"> -->
<select class="form-control" name="gender">
<option>Select</option>
<option>Male</option>
<option>Female</option>
</select>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('gender','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="email" class="mt-2">Role:</label>
</div>
<div class="col-md-9">
<select class="form-control" name="role_id">
<option>Select</option>
<?php if(count($roles)) { ?>
<?php foreach ($roles as $role){?>
<option><?php echo $role->rolename;?></option>
<?php }
} ?>
</select>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('role_id','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="password" class="mt-2">Password:</label>
</div>
<div class="col-md-9">
<!-- <input type="password" class="form-control" placeholder="Enter password" id="password"> -->
<?php
$data = array(
'type' => 'password',
'name' => 'password',
'placeholder' => 'Enter Password',
'class' => 'form-control'
);
echo form_input($data); ?>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('password','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div class="row mt-3">
<div class="col-md-3">
<label for="password" class="mt-2">Password Again:</label>
</div>
<div class="col-md-9">
<!-- <input type="password" class="form-control" placeholder="Enter password" id="password"> -->
<?php
$data = array(
'type' => 'password',
'name' => 'confpwd',
'placeholder' => 'Enter Password Again',
'class' => 'form-control'
);
echo form_input($data); ?>
</div>
</div>
</div>
</div>
<div class="col-md-6">
<?php echo form_error('confpwd','<div class="text-danger">','</div>');?>
</div>
</div><!--row-->
<div class="row">
<div class="col-md-6">
<!-- <button type="submit" class="btn btn-dark float-right">Register</button> -->
<div class="float-right">
<?php echo form_submit('Register', 'Register',"class='btn btn-dark'"); ?>
<?php echo anchor('welcome','GO BACK',['class'=>'btn btn-warning']);?>
</div>
</div>
</div>
<?php echo form_close(); ?>
</div>
<?php include('inc/footer.php');?>
here is screenshot
The errors are not showing because your select boxes are always submitting a value. When you select an option in the selectbox and no value attribute is specified, the value after the <option> tag is being sent to the server.
To trigger the required rule, you need to send an empty string.
To do this, you can either use an empty <option> tag for the placeholder:
<select class="form-control" name="gender">
<option></option>
<option>Male</option>
<option>Female</option>
</select>
Or set the value attribute of the placeholder to an empty string:
<select class="form-control" name="gender">
<option value="">Select</option>
<option>Male</option>
<option>Female</option>
</select>
Try below Code:
<select class="form-control" name="gender">
<option></option>
<option value="male">Male</option>
<option valie="female">Female</option>
</select>

How to validate forms in Magento 2 for custom module

I am creating simple custom module for user details in magento 2. It has form with three fileds. I want to validate the that data before I submit. How to achieve this in magento 2? Default magento validators are not working like data-validate={}... Do I have to add any additional js files for validation?
helloworld.phtml file
<form class="form create account form-create-account" action="<?php echo $block->getFormAction() ?>" method="post" enctype="multipart/form-data" data-mage-init='{"validation":{}}'>
<fieldset class="fieldset create account" data-hasrequired="<?php /* #escapeNotVerified */ echo __('* Required Fields') ?>">
<legend class="legend"><span><?php /* #escapeNotVerified */ echo __('Customer Information') ?></span></legend><br>
<div class="field required">
<label for="email_address" class="label"><span><?php /* #escapeNotVerified */ echo __('Email') ?></span></label>
<div class="control">
<input type="email" name="email" autocomplete="email" id="email_address" value="" title="<?php /* #escapeNotVerified */ echo __('Email') ?>" class="input-text" data-validate="{required:true, 'validate-email':true}">
</div>
</div>
<div class="field required">
<label for="First_Name" class="label"><span><?php /* #escapeNotVerified */ echo __('FirstName') ?></span></label>
<div class="control">
<input type="text" name="firstname" id="first" value="" title="<?php /* #escapeNotVerified */ echo __('Email') ?>" class="input-text" data-validate="{required:true}" >
</div>
</div>
<div class="field required">
<label class="label"><span><?php /* #escapeNotVerified */ echo __('LastName') ?></span></label>
<div class="control">
<input type="text" name="lastname" id="first" value="" title="<?php /* #escapeNotVerified */ echo __('LastName') ?>" class="input-text" >
</div>
</div>
<button type="submit" class="action submit primary" <span><?php /* #escapeNotVerified */ echo __('Submit') ?></span></button>
</form>
Below code for validation, for use below code you have to add id in your form and that id you need to use in below javascript code.
<script type="text/javascript">
require([
'jquery',
'mage/mage'
], function($){
var dataForm = $('#custom-form');
dataForm.mage('validation', {});
});
</script>
The javascript library were validation is defined is
lib/web/mage/validation.js
Regarding validation, in Magento, I need a remote validation for form input element like entering username and validate this field before submitting the form to check this username exist or not? I have implemented this way ....is there any better suggestion?
magento-jquery-remote-validation

Magento: Why doesn't login and registration forms validation work when combined on one page

EDIT 051012:
Turns out that the issue was less to do with the forms being on one page but more to conflicts between prototype and jQuery; there was a inclusion of jQuery right at the bottom of the footer which was not set to no conflict. I simply added this after the inclusion of that particular jQuery:
<script type="text/javascript">
//<![CDATA[
$.noConflict();
//]]>
</script>
I'm accepting Pavel Novitsky's answer below because he mentioned, "If assuming that you have no form embedding or JavaScript errors your code should work.." and that made me realize that it might have something to do with Javascript.
I'm currently building an e-commerce site with Magento which requires for both the registration and login forms to sit on one page.
I have successfully integrated both forms on a single page but was not able to login or register.
Here's a very brief overview of how the forms are structured
<!--registration-->
<form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="form-validate">
<!--// form input fields go here-->
</form>
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('form-validate', true);
//]]>
</script>
<!--login-->
<form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="login-form">
<!--// form input fields go here-->
</form>
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('form-validate', true);
//]]>
</script>
I noticed that the variable dataForm was being defined twice over so I switched one of them to dataForm02 to see if the submissions would go through - neither did. So after almost 6 hours of prodding, I decided to remove the javascript validation and to my surprise, the submission on both forms worked.
Does anyone have an idea why this is happening? It is good that I got the submissions on both forms through but this is definitely not ideal because there'd be no form of validations employed on the 2 forms. I'm close to throwing my computer out my 20th storey apartment.
you have one and the same form action for both login and register forms:
<?php echo $this->getPostActionUrl() ?>
This method is defined in different blocks for templates.
If assume that you have no form embedding or JavaScript errors your code should work.Hard to say something without real code.
Just one guess: do you have
//<![CDATA[
var dataForm = new VarienForm('login-form', true);
//]]>
or
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('login-form', true);
//]]>
</script>
?
Make sure you are using different id for forms in both registration and login forms and also make sure you are using same id in scripts. Seems like you have problem with login form ids. Use this for your login script.
<!--login-->
<form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="login-form">
<!--// form input fields go here-->
</form>
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('login-form', true);
//]]>
</script>
You can alternatively using xml you can achieve this functionality. Just you will need to edit customer.xml file. Replace with this one.
<customer_account_login translate="label">
<label>Customer Account Login Form</label>
<!-- Mage_Customer -->
<remove name="right"/>
<remove name="left"/>
<reference name="root">
<action method="setTemplate"><template>page/1column.phtml</template></action>
</reference>
<reference name="content">
<block type="customer/form_login" name="customer_form_login" template="customer/form/login.phtml" />
<block type="customer/form_register" name="customer_form_register" template="customer/form/register.phtml">
<block type="page/html_wrapper" name="customer.form.register.fields.before" as="form_fields_before" translate="label">
<label>Form Fields Before</label>
</block>
</block>
</reference>
</customer_account_login>
Replace your theme/template/presistent/customer/form/register.phtml with this code and let me know the result.
<?php $login = new Mage_Customer_Block_Form_Login(); ?>
<div class="account-login">
<div class="page-title">
<h1><?php echo $login->__('Login or Create an Account') ?></h1>
</div>
<?php //echo $login->getMessagesBlock()->getGroupedHtml() ?>
<form action="<?php echo $login->getPostActionUrl() ?>" method="post" id="login-form">
<div class="col2-set">
<div class="col-1 new-users">
<div class="content">
<h2><?php echo $login->__('New Customers') ?></h2>
<p><?php echo $login->__('By creating an account with our store, you will be able to move through the checkout process faster, store multiple shipping addresses, view and track your orders in your account and more.') ?></p>
</div>
</div>
<div class="col-2 registered-users">
<div class="content">
<h2><?php echo $login->__('Registered Customers') ?></h2>
<p><?php echo $login->__('If you have an account with us, please log in.') ?></p>
<ul class="form-list">
<li>
<label for="email" class="required"><em>*</em><?php echo $login->__('Email Address') ?></label>
<div class="input-box">
<input type="text" name="login[username]" value="<?php echo $login->htmlEscape($login->getUsername()) ?>" id="email" class="input-text required-entry validate-email" title="<?php echo $login->__('Email Address') ?>" />
</div>
</li>
<li>
<label for="pass" class="required"><em>*</em><?php echo $login->__('Password') ?></label>
<div class="input-box">
<input type="password" name="login[password]" class="input-text required-entry validate-password" id="pass" title="<?php echo $login->__('Password') ?>" />
</div>
</li>
<?php echo $login->getChildHtml('form.additional.info'); ?>
<?php echo $login->getChildHtml('persistent.remember.me'); ?>
</ul>
<?php echo $login->getChildHtml('persistent.remember.me.tooltip'); ?>
<p class="required"><?php echo $login->__('* Required Fields') ?></p>
</div>
</div>
</div>
<div class="col2-set">
<div class="col-1 new-users">
<div class="buttons-set">
<button type="button" title="<?php echo $login->__('Create an Account') ?>" class="button" onclick="window.location='<?php echo Mage::helper('persistent')->getCreateAccountUrl($login->getCreateAccountUrl()) ?>';"><span><span><?php echo $login->__('Create an Account') ?></span></span></button>
</div>
</div>
<div class="col-2 registered-users">
<div class="buttons-set">
<?php echo $login->__('Forgot Your Password?') ?>
<button type="submit" class="button" title="<?php echo $login->__('Login') ?>" name="send" id="send2"><span><span><?php echo $login->__('Login') ?></span></span></button>
</div>
</div>
</div>
<?php if (Mage::helper('checkout')->isContextCheckout()): ?>
<input name="context" type="hidden" value="checkout" />
<?php endif; ?>
</form>
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('login-form', true);
//]]>
</script>
</div>
<div class="account-create">
<div class="page-title">
<h1><?php echo $this->__('Create an Account') ?></h1>
</div>
<?php echo $this->getChildHtml('form_fields_before')?>
<?php echo $this->getMessagesBlock()->getGroupedHtml() ?>
<form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="form-validate">
<div class="fieldset">
<input type="hidden" name="success_url" value="<?php echo $this->getSuccessUrl() ?>" />
<input type="hidden" name="error_url" value="<?php echo $this->getErrorUrl() ?>" />
<h2 class="legend"><?php echo $this->__('Personal Information') ?></h2>
<ul class="form-list">
<li class="fields">
<?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?>
</li>
<li>
<label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
<div class="input-box">
<input type="text" name="email" id="email_address" value="<?php echo $this->escapeHtml($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" />
</div>
</li>
<?php if ($this->isNewsletterEnabled()): ?>
<li class="control">
<div class="input-box">
<input type="checkbox" name="is_subscribed" title="<?php echo $this->__('Sign Up for Newsletter') ?>" value="1" id="is_subscribed"<?php if($this->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" />
</div>
<label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label>
</li>
<?php endif ?>
<?php $_dob = $this->getLayout()->createBlock('customer/widget_dob') ?>
<?php if ($_dob->isEnabled()): ?>
<li><?php echo $_dob->setDate($this->getFormData()->getDob())->toHtml() ?></li>
<?php endif ?>
<?php $_taxvat = $this->getLayout()->createBlock('customer/widget_taxvat') ?>
<?php if ($_taxvat->isEnabled()): ?>
<li><?php echo $_taxvat->setTaxvat($this->getFormData()->getTaxvat())->toHtml() ?></li>
<?php endif ?>
<?php $_gender = $this->getLayout()->createBlock('customer/widget_gender') ?>
<?php if ($_gender->isEnabled()): ?>
<li><?php echo $_gender->setGender($this->getFormData()->getGender())->toHtml() ?></li>
<?php endif ?>
</ul>
</div>
<?php if($this->getShowAddressFields()): ?>
<div class="fieldset">
<input type="hidden" name="create_address" value="1" />
<h2 class="legend"><?php echo $this->__('Address Information') ?></h2>
<ul class="form-list">
<li class="fields">
<div class="field">
<label for="company"><?php echo $this->__('Company') ?></label>
<div class="input-box">
<input type="text" name="company" id="company" value="<?php echo $this->escapeHtml($this->getFormData()->getCompany()) ?>" title="<?php echo $this->__('Company') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company') ?>" />
</div>
</div>
<div class="field">
<label for="telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label>
<div class="input-box">
<input type="text" name="telephone" id="telephone" value="<?php echo $this->escapeHtml($this->getFormData()->getTelephone()) ?>" title="<?php echo $this->__('Telephone') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" />
</div>
</div>
</li>
<?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?>
<li class="wide">
<label for="street_1" class="required"><em>*</em><?php echo $this->__('Street Address') ?></label>
<div class="input-box">
<input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet(1)) ?>" title="<?php echo $this->__('Street Address') ?>" id="street_1" class="input-text <?php echo $_streetValidationClass ?>" />
</div>
</li>
<?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
<?php for ($_i = 2, $_n = $this->helper('customer/address')->getStreetLines(); $_i <= $_n; $_i++): ?>
<li class="wide">
<div class="input-box">
<input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet($_i)) ?>" title="<?php echo $this->__('Street Address %s', $_i) ?>" id="street_<?php echo $_i ?>" class="input-text <?php echo $_streetValidationClass ?>" />
</div>
</li>
<?php endfor; ?>
<li class="fields">
<div class="field">
<label for="city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
<div class="input-box">
<input type="text" name="city" value="<?php echo $this->escapeHtml($this->getFormData()->getCity()) ?>" title="<?php echo $this->__('City') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="city" />
</div>
</div>
<div class="field">
<label for="region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label>
<div class="input-box">
<select id="region_id" name="region_id" title="<?php echo $this->__('State/Province') ?>" class="validate-select" style="display:none;">
<option value=""><?php echo $this->__('Please select region, state or province') ?></option>
</select>
<script type="text/javascript">
//<![CDATA[
$('region_id').setAttribute('defaultValue', "<?php echo $this->getFormData()->getRegionId() ?>");
//]]>
</script>
<input type="text" id="region" name="region" value="<?php echo $this->escapeHtml($this->getRegion()) ?>" title="<?php echo $this->__('State/Province') ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
</div>
</div>
</li>
<li class="fields">
<div class="field">
<label for="zip" class="required"><em>*</em><?php echo $this->__('Zip/Postal Code') ?></label>
<div class="input-box">
<input type="text" name="postcode" value="<?php echo $this->escapeHtml($this->getFormData()->getPostcode()) ?>" title="<?php echo $this->__('Zip/Postal Code') ?>" id="zip" class="input-text validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" />
</div>
</div>
<div class="field">
<label for="country" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
<div class="input-box">
<?php echo $this->getCountryHtmlSelect() ?>
</div>
</div>
</li>
</ul>
<input type="hidden" name="default_billing" value="1" />
<input type="hidden" name="default_shipping" value="1" />
</div>
<?php endif; ?>
<div class="fieldset">
<h2 class="legend"><?php echo $this->__('Login Information') ?></h2>
<ul class="form-list">
<li class="fields">
<div class="field">
<label for="password" class="required"><em>*</em><?php echo $this->__('Password') ?></label>
<div class="input-box">
<input type="password" name="password" id="password" title="<?php echo $this->__('Password') ?>" class="input-text required-entry validate-password" />
</div>
</div>
<div class="field">
<label for="confirmation" class="required"><em>*</em><?php echo $this->__('Confirm Password') ?></label>
<div class="input-box">
<input type="password" name="confirmation" title="<?php echo $this->__('Confirm Password') ?>" id="confirmation" class="input-text required-entry validate-cpassword" />
</div>
</div>
</li>
<?php echo $this->getChildHtml('form.additional.info'); ?>
<?php echo $this->getChildHtml('persistent.remember.me'); ?>
</ul>
<?php echo $this->getChildHtml('persistent.remember.me.tooltip'); ?>
</div>
<div class="buttons-set">
<p class="required"><?php echo $this->__('* Required Fields') ?></p>
<p class="back-link"><small>« </small><?php echo $this->__('Back') ?></p>
<button type="submit" title="<?php echo $this->__('Submit') ?>" class="button"><span><span><?php echo $this->__('Submit') ?></span></span></button>
</div>
<?php if (Mage::helper('checkout')->isContextCheckout()): ?>
<input name="context" type="hidden" value="checkout" />
<?php endif; ?>
</form>
<script type="text/javascript">
//<![CDATA[
var dataForm = new VarienForm('form-validate', true);
<?php if($this->getShowAddressFields()): ?>
new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'zip');
<?php endif; ?>
//]]>
</script>
</div>

Codeigniter Form Helper Duplicate from_hidden?

Im totally stumped.. Im creating some form elements using the CI form helper and for some weird reason, its creating a duplicate version.
Here is my PHP
<div id="receiveInventoryItemDetails">
<p><?php echo form_open('#', array("class" => "nyroModal form label-inline"));?></p>
<?php echo form_hidden('item_id', '', "readonly = true"); ?>
<?php echo form_hidden('purchase_order_id', '', "readonly = true"); ?>
<p><?php echo form_label('Item Name', 'item_name');?><?php echo form_input('item_name', '', "readonly = true"); ?></p>
<p><?php echo form_label('Item QTY', 'item_qty');?><?php echo form_input('item_qty', ''); ?></p>
<?php echo form_close();?>
</div>
<div class="buttonrow">
<button class="btn-sec" onclick="inventoryC.receiveSubmitItem();"><span>Add To Inventory</span></button>
</div>
Here is the HTML output
<div id="receiveInventoryItemDetails">
<p><form action="https://mysite.com/#.abl" method="post" accept-charset="utf-8" class="nyroModal form label-inline"></p>
<input type="hidden" name="item_id" value="" />
<input type="hidden" name="item_id" value="" />
<input type="hidden" name="purchase_order_id" value="" />
<p><label for="item_name">Item Name</label><input type="text" name="item_name" value="" readonly = true /></p>
<p><label for="item_qty">Item QTY</label><input type="text" name="item_qty" value="" /></p>
</form>
</div>
<div class="buttonrow">
<button class="btn-sec" onclick="inventoryC.receiveSubmitItem();"><span>Add To Inventory</span></button>
</div>
You can't use html attributes via third parameter. Look at form helper source code
This should be work:
<?php echo form_hidden('item_id', ''); ?>