I use Yii 1.1.14
I use form to capture an email, not using Yii ajaxValidation/clientValidation
<?php $form = $this->beginWidget('CActiveForm', array(
'id' => 'form-newsletter',
'action' => null,
'focus' => array($model, 'email'),
'htmlOptions' => array(
'role' => 'form',
),
)); ?>
I got this at js console
Uncaught TypeError: undefined is not a function
Which is refer to this
jQuery('#form-newsletter').yiiactiveform({'validateOnSubmit':true,'validateOnChange':true,'errorCssClass':'has-error has-feedback','successCssClass':'has-success has-feedback','inputContainer':'div.form-group','attributes':[{'id':'NewsletterForm_email','inputID':'NewsletterForm_email','errorID':'NewsletterForm_email_em_','model':'NewsletterForm','name':'email','enableAjaxValidation':true,'clientValidation':function(value, messages, attribute) {
I also check that jquery.yiiactiveform.js is present.
The question is, i dont want to use the default ajaxValidation/clientValidation, why is it loaded? and why it tells that yiiactiveform is undefined?
Thank you.
Check if you are including a newer version of jQuery than the one Yii includes (2.x for example) for the validation.
If you use other custom javascripts then must use in your layout code for example:
Yii::app()->clientScript->registerScriptFile(Yii::app()->request->baseUrl . '/js/bootstrap.min.js');
Related
In CakePHP2 we use FormHelper's inputDefault options to set default values for all input within form
For Example :
echo $this->Form->create('User', array(
'inputDefaults' => array(
'required' => false,
'error' => false,
'div' => 'form-group',
'label' => false
)
));
But i am not finding any option in CakePHP3 helper like this, they haven't mentioned removed it or not?
Is anybody there, who know about this....
Thanks
According to migration guide inputDefaults option was removed.
The inputDefaults option has been removed from create().
FormHelper::inputDefaults() has been removed. You can use templates() to define/augment the templates FormHelper uses.
templates() method of FormHelper documentation you can find here.
I have written form tag of YII2 specific as
<?php $form = ActiveForm::begin(['id' => 'builder/saveform','options' => ['method' => 'post']]) ?>
but when i run this, my external javascript is catching an error showing
Error: Syntax error, unrecognized expression: #builder/saveform
What is the error
To change default action add it as the first argument in this format ['<controller>/<action>']
<?php $form = ActiveForm::begin(['action' => ['builder/saveform'],'options' => ['method' => 'post']]) ?>
Example from the Yii2 guide
Also, keep in mind that the method defaults to post, so specifying that is unnecessary.
$form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data'], 'action' => Yii::$app->urlManager->createUrl(['site/signup'])]);
You can also use this snippet for custom form action and other attributes. createUrl method will not affect your url pattern.
for the sake now i have written the following code
<?php $form = ActiveForm::begin(['action' => 'index.php?r=builder/saveform','options' => ['method' => 'post']]) ?>
it is working but is it the proper way to write???
I was able to do it by adding the action into the options like so
<?php $form = ActiveForm::begin(['id' => 'contact-form', 'options' => ['method' => 'post', 'action' => 'site/add']]); ?>
For Yii2
$form = ActiveForm::begin([
'id' => 'login-form',
'options'=>['autocomplete'=>'off','method' => 'post', ],
'action' => '/frontend/web/user-management/auth/login',
'validateOnBlur'=>false,
'fieldConfig' => [
'template'=>"{input}\n{error}",
],
]);
Since there is still no accepted answer, the exact answer to your question is in the Error:
"unrecognized expression: '#builder/saveform' ". You have your 'id' HTML tag of the Form being assigned a value of 'builder/saveform', which contains "/" - an illegal character in terms of HTML4 Specification. I suppose Yii has a "validation" in place resulting in your error.
Now, I believe you are trying to specify the relative URL for the Form submission. For that, please refer to Michael St Clair's answer.
I have a Yii Layout, called Main, it loads all the CSS and the JavaScript, correctly ordered.
So, i have two views, one just have pure HTML, and the other one have something like this :
<?php
$dataProvider=new CActiveDataProvider('agenda');
$dataProvider->pagination->pageSize=5;
$this->widget('zii.widgets.CListView', array(
'dataProvider'=>$dataProvider,
'enablePagination'=>true,
'itemView'=>'_agenda', // refers to the partial view named '_agenda'
'pager' => array(
'class' => 'CLinkPager',
'header' => false,
'htmlOptions' => array('class' => 'pager'),
'maxButtonCount' => '10',
'cssFile'=>'css/my.css',
),
));
?>
My problem :
The second one throw this exception Undefined is not a function and aim to the line with $(document).foundation(); in the code.
Is the only error it throws, and both have the same JavaScripts and CSS imported.
They are incompatible?
Solved, i just had conflicts with the Foundation's jQuery and Yii's jQuery.
The solution is just use this at top :
<?php Yii::app()->clientScript->registerCoreScript('jquery'); ?>
Remove others jQuery and move the foundation.min.js to the bottom.
How do i use the inputDefaults to add a common class to all the input elements in my form. also pls give a brief description of the inputDefaults.
isn't it:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'class' => 'someclass'
)
);
`
You should read the cookbook. theres a good example: http://book.cakephp.org/view/1639/options-inputDefaults
When you create a form you add inputdefaults key in options:
echo $this->Form->create('User', array(
'inputDefaults' => array(
'div' => array('class' => 'someclass')
)
);
After browsing the source file i didn't find anything either. So the only way is to use it explicitly for every call to the input function.
I can't get Zend to autoload a custom form element class. I did things exactly as Marcin describes here (except that my classes start with 'Zend' and not 'my' but I'm getting this error:
Warning: include_once(Zend\Form\Element\Div.php) [function.include-once]: failed to open stream: No such file or directory
I have Zend_Form_Element_Div inside forms\elements\ and Zend_View_Helper_FormDiv inside views\helpers\
Basically, every folder in the error message is missng an 's', the right path is Zend\Forms\Elements\Div.php
I also have this in my bootstrap, though I'm not sure if it's necessary, but I'm also using this for my forms and models folder (and some others, but I don't think there's need to post them all):
<?php
$resourceLoader->addResourceTypes(array(
'model' => array(
'namespace' => 'Model',
'path' => 'models'
),
'element' => array(
'namespace' => 'Element',
'path' => 'elements'
),
'form' => array(
'namespace' => 'Form',
'path' => 'forms'
)
));
?>
(Is there actually any other way of doing this autoloading? Instead of declaring every single folder?)
Update:
Element_Div in application/forms/elements/Div.php
In my forms init() method: $this->addElementPrefixPath('Element_', APPLICATION_PATH . '/forms/elements');
Error I'm getting: Fatal error: Class 'Element_Div' not found in C:\xampplite\htdocs\code\application\forms\PostForm.php on line 63
You essentially have to tell the form where to find custom elements by using:
$form->addElementPrefixPath()
In your case, you would use - either within the form's init() or __construct() method - something like:
$this->addElementPrefixPath('Zend_Form_Element_', APPLICATION_PATH . '/elements);;
However, I have agree with #Marcin. Naming your own classes with the Zend_ pseudo-namespace is ill-advised. Either:
Decide on an application namespace and declare it in your Bootstrap when you create your $resourceLoader
Create an custom library that resides on your include path - probably at the same level as the Zend library - and put your custom stuff out there.
Let me know if you need more details on either of these suggestions and I'll fatten up the explanations a bit.
Update based on comments
Using an empty appnamespace, your call to addElementPrefixPath() now changes to:
$this->addElementPrefixPath('Element_', APPLICATION_PATH . '/elements);
And I guess you could remove the elements entry from the $resourceLoader definition in your Bootstrap since it's really not doing anything.
Update 2
I assumed that you were adding the element to the form using the shortname, something like:
$form->addElement('div', 'my_div');
In this circumstance, we need to tell the $form and its plugin registry where to find an element of type 'div'. That's why we dealt with $form->addElementPrefixPath().
However, from the error message you are reporting, it appears that you are adding your custom element to the form using something like:
$div = new Element_Div();
$form->addElement($div, 'my_div');
In this case, it is not the $form and its plugin registry that has to worry about finding/loading/instantiating the custom element; it is the $autoloader via its $resourceLoader. In that case, there is no need for the $form->addElementPrefixPath(), which is essentially a hint to the form on how to find custom elements invoked by shortname.
What we need is to configure the $resourceLoader back in Bootstrap so it knows where to find the class. Assuming you stick with empty appnamespace (so your class is named Element_Div) and you place the file in application/forms/elements/Div.php, then the $resourceLoader call is as follows:
$resourceLoader->addResourceTypes(array(
'model' => array(
'namespace' => 'Model_',
'path' => 'models'
),
'element' => array(
'namespace' => 'Element_',
'path' => 'forms/elements'
),
'form' => array(
'namespace' => 'Form_',
'path' => 'forms'
)
));
That should do it. [Famous last words, eh?]
I prefer creating forms like this:
$form->addElement(new My_Form_Element_Whatever(array(
'name' => 'my_element',
'label' => 'My element',
)));
or
$form->addElement($whatever = new My_Form_Element_Whatever(array(
'name' => 'my_element',
'label' => 'My element',
)));
$whatever->removeDecorator('Errors');
when I need to further modify the element.