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.
Related
I'm trying to use the select2entity-bundle for my project to implement auto-complete.
When using the builder, the specification is as follows :
$builder
->add('country', Select2EntityType::class, [
'multiple' => true,
'remote_route' => 'tetranz_test_default_countryquery',
'class' => '\Tetranz\TestBundle\Entity\Country',
'primary_key' => 'id',
'text_property' => 'name',
'minimum_input_length' => 2,
'page_limit' => 10,
'allow_clear' => true,
'delay' => 250,
'cache' => true,
'cache_timeout' => 60000, // if 'cache' is true
'language' => 'en',
'placeholder' => 'Select a country',
// 'object_manager' => $objectManager, // inject a custom object / entity manager
])
What should be specified within the 'remote_route' parameter?
Why should my field be linked to a route? How and where should this route be configured? Should it be linked to a query?
Thanks for your help!
The select needs to update the values to display while he user is typing. It does this by sending a request to a controller you need to write, which collects the relevant values and returns them as json.
Create a controller that returns the list of items to display in the select, and use the name of the route you defined for that controller as the 'remote_route' parameter.
The format for the returned data is documented halfway down the page for the bundle on github https://github.com/tetranz/select2entity-bundle
In the form generated by Symfony I would like to translate the child options, how can this be done?
->add('business', 'choice', array(
'choices' => array('Zakelijk' => true, 'Prive' => false),
'expanded' => true,
'multiple' => false,
'choices_as_values' => true,
))
I tried to include the standard symfony translation code directly in the code above, but then i get a php error.
$this->get('translator')->trans('business');
words.en.yml
Zakelijk: Zakelijk
Prive: Prive
In your form:
'translation_domain' => 'fooo'
'choices' => array('Zakelijk' => true, 'Prive' => false),
You need to use translations. In your bundle you need to create in Resources/Translations with format i18N and one extension (yml or php or xliff). In your form use for example "app.form.zalelijk" with yaml format.
'choices' => array('app.form.zakelijk' => true, 'app.form.prive' => false)
Twig will translate the string in your request locale to render the form.
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.
I have a form in my ZF2 app with a CAPTCHA element as follows :
$this->add(array(
'type' => 'Zend\Form\Element\Captcha',
'name' => 'captcha',
'attributes' => array(
'class'=>'form-control',
),
'options' => array(
'label' => 'Please verify you are human.',
'captcha' => array('class' => 'Dumb')
),
));
I have an input filter attached to the form that validates the other elements in the form (name, email, message). When this is attached to the form the validation for the CAPTCHA field is ignored when checking if valid.
if ($request->isPost()) {
// set the filter
$form->setInputFilter($form->getInputFilter());
$form->setData($request->getPost());
if ($form->isValid()) { ...
If i remove the input filter then the CAPTCHA field is validated correctly but obviously the other fields have no validators. What silly mistake am I making? Is there a "CAPTCHA" validator I have to set in the input filter?
The issue is because, I assume that on your form you have created a method called:
getInputFilter();
which overrides the original getInputFilter(),
there are two solutions:
rename your function on your form to be getInputFilterCustom()
and then modify also:
if ($request->isPost()) {
// set the filter
$form->setInputFilter($form->getInputFilterCustom());
or inside your current getInputFilter() add the logic to validate the captcha.
This is my code to add a captcha image control in a ZF2 form :
$this->add(array(
'name' => 'captcha',
'type' => 'Captcha',
'attributes' => array(
'id' => 'captcha',
'autocomplete' => 'off',
'required' => 'required'
),
'options' => array(
'label' => 'Captcha :',
'captcha' => new \Zend\Captcha\Image(array(
'font' => 'public/fonts/arial.ttf',
'imgDir' => 'public/img/captcha',
'imgUrl' => 'img/captcha'
))
),
));
The others form elements are using validators from the input filter, but i didn't use any validators to make it work.
I hope this can help you.
It is because you don't call the parent getInputFilter() within yours. Simply do
public function getInputFilter()
{
parent::getInputFilter();
//... your filters here
}
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.