I am creating a form using Yii's activeForm.
All aspects of the form are working except that the client side validation does not take care of scenarios.
I would like it if I can get the client validations working based on scenarios.
Below mentioned is the partial code for the form
$form = $this->beginWidget('CActiveForm', array(
'enableClientValidation' => true,
'enableAjaxValidation' => false,
'errorMessageCssClass' => 'has-error',
'htmlOptions' => array(
'class' => 'form-horizontal',
'role' => 'form',
'id' => 'payment-form',
),
'clientOptions' => array(
'id' => 'payment-form',
'validateOnSubmit' => true,
'errorCssClass' => 'has-error',
'successCssClass' => 'has-success',
'inputContainer' => '.form-group',
'validateOnChange' => true,
),
));
FYI the scenario validation is done when the data is pushed to the model, where we have defined the scenarios for the validation. So if you want to validate on client side, the only way is to use "JAVASCRIPT", Or through the ajax, which you have turned off enable the ajax validation 'enableAjaxValidation' => true, to get the client validation using ajax.
Related
I am creating a custom form for my extension. In the form I have few fields that I save in database. My problem is that once I save the fields they get saved in DB but after the page refresh the data in the fields gets empty.
I want these fields data to remain in the fields. How do I do that? Is there a way to do that.
Form code is in this file app\code\community\CompanyNmae\ModuleName\Block\Adminhtml\Suretaxconfig\Edit\Form.php
and save config method is in this file
app\code\community\CompanyNmae\ModuleName\controllers\Adminhtml\ModuleController.php
Code for Form.php is here. There no HTML or PHTML used in the project for my extension.
<?php
class CompanyName_Modulename_Block_Adminhtml_ModuleConfig_Edit_Form extends Mage_Adminhtml_Block_Widget_Form{
protected function _prepareForm() {
$form = new Varien_Data_Form (
array('id'=>'edit_form', 'action'=>$this->getData('action'), 'method'=>'post')
);
$fieldset = $form->addFieldset('base_fieldset', array('legend'=>Mage::helper('modulename')->__('ModuleName Settings'),
'class'=>'fieldset-wide'));
$fieldset->addField('companycode', 'text', array(
'name' => 'companycode',
'label' => Mage::helper('modulename')->__('Client Number'),
'title' => Mage::helper('modulename')->__('Client Number'),
'required' => true,
'after_element_html' => '<small>Enter Client Number</small>'
));
$fieldset->addField('validationkey', 'text', array(
'name' => 'validationkey',
'label' => Mage::helper('modulename')->__('Validation Key'),
'title' => Mage::helper('modulename')->__('Validation Key'),
'required' => true,
'after_element_html' => '<small>Enter Company Validation Key </small>'
));
$fieldset->addField('select_provider_type', 'select', array(
'label' => Mage::helper('modulename')->__('Default Provider Type'),
'class' => 'required-entry',
'required' => true,
'name' => 'providertype',
'onclick' => "",
'onchange' => "",
'value' => '1',
'values' => array('70' => '70', '99' => '99'),
'disabled' => false,
'readonly' => false,
'after_element_html' => '<small>Default Provider Type</small>',
'tabindex' => 1
));
$form->setUseContainer(true);
$this->setForm($form);
return parent::_prepareForm();
}
you can fetch data from database and fill that data into fields or you can use session and store form post values into it before saving the data into database and when it redirects on your form page you can check if values are exists then you fill them in form
Your question is little confusable, I'm assuming that you want to keep fields data field after the form submit....
Is this is the scenario then you can use PHP Sessions to store data in sessions and check on page load if session is set then display it.
Like:
PHP
session_start();
// you can set session on form submit using if(isset($_POST)){//set session here}
$_SESSION['example'] = "Example Value";
HTML
<input type="text" name="example" value="<?=(isset($_SESSION['example']) ? $_SESSION['example'] : ''); ?>"/>
I'm pretty new with Yii2 and have the next unpleasant issue.
I created two forms at same page like
<?php $form = ActiveForm::begin([
// make sure you set the id of the form
'id' => 'create',
'action' => $action,
'options' => ['enctype' => 'multipart/form-data']
]); ?>
<?php $form = ActiveForm::begin([
// make sure you set the id of the form
'id' => 'update',
'action' => $action,
'options' => ['enctype' => 'multipart/form-data']
]); ?>
I use the same model for both form fields like
<?= $form->field($action_model, 'action_name',[
'addon' => ['prepend' => ['content'=>$action_model->getAttributeLabel('action_mobile')]]
])->widget(Typeahead::classname(), [
'options' => ['placeholder' => $action_model->getAttributeLabel('action_placeholder')],
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'local' => Json::encode( $totalLookUp['action_lookUp'] ),
'limit' => 10
]
]
])->label(false);
?>
And here is the issue. In that case I have two forms with same scope of fields, with same names and same id. What for sure will be not valid for W3C. The another one issue that inspite of that fact that client side presubmit javascript validation for both forms work perfect. The typeahed widget works only for first set of fields since it bindid by id.
If i try to override elements id by specify it by widgets options like
<?= $form->field($action_model, 'action_name',[
'addon' => ['prepend' => ['content'=>$action_model->getAttributeLabel('action_mobile')]]
])->widget(Typeahead::classname(), [
'options' => ['id'=> $form_id.'_action_name',
'placeholder' => $action_model->getAttributeLabel('action_placeholder')],
'pluginOptions' => ['highlight'=>true],
'dataset' => [
[
'local' => Json::encode( $totalLookUp['action_lookUp'] ),
'limit' => 10
]
]
])->label(false);
?>
The typeahead works perfect, but in that case validation fails, I mean it just stop working.
So the questuion is, how to make possible adjust validation script and use unique form id's.
From the docs:
If you set a custom id for the input element, you may need to adjust the $selectors accordingly.
For your case:
$form->field($action_model, 'action_name', [
'selectors' => ['input' => '#'.$form_id.'_action_name'],
'addon' => ...
I have some questions about Yii client validation. When I assign clientOptions -> validateOnSubmit to true, Yii automatically validates my form on the client side? Ok, so the validation rules will get rules from rules() method in model? And why when I write 'minlength' => 2 directly into field definition in form template it won't work?
This is the sample of CActiveForm widget:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'register-form',
'enableClientValidation' => true,
'enableAjaxValidation' => false,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
'validateOnType' => false,
'errorCssClass' => 'has-error',
'successCssClass' => 'has-success',
'afterValidate' => 'js:function(form, data, hasError){}'
)));
The enableClientValidation activate jquery based validation on your page. and client options specify how client validation works. Yes, validation rule come from rule() function in the model, so you should define ANY validation rule there.
$form = $this->beginWidget('CActiveForm', array(
'id' => 'reply-form',
'enableAjaxValidation' => true,
'enableClientValidation' => false,
'clientOptions' => array(
'validateOnSubmit' => true,
'validateOnChange' => false,
'beforeValidate' => 'js:function(form){
alert("before");
}',
'afterValidate' => 'js:function(form, data, hasError){
alert("after");
}'
)
));
it seems that afterValidate is not working with beforeValidate in CActiveForm,
if I remove beforeValidate, afterValidate is working fine.
Is anyone here know why its working this way?
just put return true on beforeValidate.
I would like to reload the form, or preferably just the values, by using 'onChange'. There are several elements in the form that needs to be updated when changing the project id.
$this->addElement('select', 'project_id', array(
'label' => t('Project'),
'multiOptions' => $data,
'filters' => array('Int'),
'value' => 0,
'required' => true,
'disableTranslator' => true,
'validators' => array(),
'onChange' => 'Javascript:window.location.reload(false)'
));
You can change your onChange to call a custom javascript function instead - which will change any fields you need to change, and then submmit the form.