using a custom jQuery validation class - class

I have some custom validation I'd like to do with jQuery. jQuery validation comes out of the box with a 'required' class. This means that I can have a script declaring
$('form').validate()
and the next textbox will validate correctly, based on the class 'required' I add to it:
<input type="text" value="" name="flowstatus" id="flowstatus" class="required">
My new validation rule is: the text in a textbox should be between 15 and 50 chars long. Is there a way to create my own custom rule/class to implement something like
<input type="text" value="" name="flowstatus" id="flowstatus" class="from15to50">
Note: This is NOT what I need:
('form').validate({
rules: {
flowstatus: {required: true, minlength: 15, maxlength: 50}
}});
This last script is too tightly coupled to the fields I want to validate.

I'm pretty sure that you can add an attribute to the desired element defining its min and max lengths. Something like:
<input type="text" value="" name="flowstatus" id="flowstatus" class="required" minlength="15" maxlength="50">
I'm not quite sure about the custom class solution, however.

Related

Inserting data into a database using forms in web2py

I want to store my data in a database using forms.
How can I do it without using SQLFORM
like in php we use $var = $_POST['var_form']
I created a table in modul file called Produit
db.define_table('Produit',`enter code here`
Field('Nom', requires = IS_NOT_EMPTY()),
Field('Poid', 'float', requires = IS_NOT_EMPTY()),
Field('Prix', 'float', requires = IS_NOT_EMPTY()),
Field('Expiration', 'datetime'),
auth.signature)
And created a form like
{{extend 'layout.html'}}
<form action="create" method=post>
<fieldset>
<legend><h1><center><em>Ajouter un produit</em></center></h1></legend>
Nom:<br>
<input type="text" name="nom" required/>
<br>
Poid:<br>
<input type="text" name="poid" required/>
<br>
Prix:<br>
<input type="text" name="prix" required/>
<br>
Date d'expiration:<br>
<input type="date" name="exp" required/>
<br>
<br><br>
<input type="submit" value="Ajouter">
Use URL helper to specify action of the form.
<form action="{{=URL('create')}}" method=post>
...
</form>
Then in create controller, you can access submitted values using request.vars
db.Produit.insert(Nom=request.vars.nom, Poid=request.vars.poid,
Prix=request.vars.prix, Expiration=request.vars.exp)
This answer has the right idea, but to make things a little easier, you should use HTML field names that exactly match the database table field names. In that case, you can simplify the insert to:
db.Produit.insert(**db.Produit._filter_fields(request.post_vars))
Beware, though, that your manual approach lacks any protection against CSRF attacks (which the web2py form system would provide). As an alternative, note that you can still use SQLFORM even if you want to generate a custom form layout in the view. This approach is covered in the documentation here and here.

Input field with validation crashes when typing fast in Angular 2

I have a form in Angular 2 with a simple validation using Validators.
The html template
<form role="form" #myForm="ngForm" (ngSubmit)="submit()" novalidate [ngFormModel]="form">
<div class="form-group list-element">
<label for="name">name*</label>
<input type="text" name="name" class="form-control" ngControl="name"
#name="ngForm" placeholder="Enter name" [(ngModel)]=user.name >
</div>
</form>
The ControlGroup for the validation
ngOnInit() {
this.form = new ControlGroup({
name: new Control('', Validators.compose([
Validators.required,
Validators.pattern("(([a-zA-Z ]|[0-9])+)*"),
Validators.minLength(5),
Validators.maxLength(80)
]))
});
}
When I type fast (random) characters in the input field (e.g #1KZBZKBjkndedjk###kjbzdzdékj!) the application crashes. This does not happen always so I have not really noticed a pattern.
This is the place where the crash happens, so I think the crash has something to do with the pattern.
That's a known issue https://github.com/angular/angular/issues/7822 and will probably be fixed when this pull request lands https://github.com/angular/angular/pull/7421
First of all this is not related to AngularJS.
This happens because your pattern is wrong I don't know what pattern you need but for example if you need to validate email then use this pattern
& Just Check for your pattern nothing else.
your code....
Validators.pattern("^[_a-z0-9]+(.[_a-z0-9]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,4})$"),
your code ends...
That it!

Zend Form text element with array notation issue

I'm using Zend_Form and have got stuck in a situation. I have an array of some values like below :
$feeTypes = array(1,2,3,4);
and want to create 6 text elements in zend form that should be rendered like following:
<input type="text" name="class_fee_type[1]" />
<input type="text" name="class_fee_type[2]" />
<input type="text" name="class_fee_type[3]" />
<input type="text" name="class_fee_type[4]" />
I tried the following code but don't know that how to associate the above $feeType array.
$class_fee_type = $this->CreateElement('text','class_fee_type')
->setRequired(false)
->setAllowEmpty(false)
->setIsArray(true)
->setRegisterInArrayValidator(false)
->setDecorators(array( array('ViewHelper')
));
I'm not using any subform in this form. This is absolutely simple zend form.
Thanks.
I'm talking from ZF1 point of view:
It seems like there is no standard way of doing what you need. So I would recommend write your own form element and element view helper for that. And passing your indexes array as options to element.
Note: to use setRegisterInArrayValidator - your Zend form element should extend Zend_Form_Element_Multi.

How to handle Zend SubForms when the number of each is unknown

I have a 'customer' form which has a section called 'contacts'. To start with this contacts section will contain the following elements..
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
But the user may want to add another contact which will duplicate the elements with javascript to produce the following:
<input type="text" name="contacts[0][fname]" />
<input type="text" name="contacts[0][sname]" />
<br />
<input type="text" name="contacts[1][fname]" />
<input type="text" name="contacts[1][sname]" />
I know how to produce the first set of elements, however if the form gets submitted and there are errors, how can i ensure that the correct number of 'contacts' elements get rendered?
Ive never had to do this with Zend_Form but i have done it with Symfony 1.4's sfForm which has a similar API and theory of operation. Based on that the basic process is:
In the parent forms constructor intialize some default number of subforms. Youll want to separate out the logic for actually creating and embedding n subforms into a separate method(s). Ill refer to this as the method emebedContacts($count = 1)
Override the isValid and setDefaults methods on the parent form so that they detect the number of subforms in the $data arguments passed to them and then call embedContacts before calling parent::isValid() or parent::setDefaults().
Hope that helps.

Do I really have to remove a second, hidden form field from within a label for my HTML to validate?

We have the following code in which we are getting errors in the w3c validator for "Any input descendant of a label element with a for attribute must have an ID value that matches that for attribute." and "The label element may contain at most one input, button, select, textarea, or keygen descendant." Is this something that should just be ignored by the validator (as it is seeminlgly correct) or should it be changed to appease the w3c? Note this is html5 doctype.
<fieldset>
<label for="user_is_subscribed">
<input type="hidden" value="0" name="user[is_subscribed]">
<input type="checkbox" value="1" name="user[is_subscribed]" id="user_is_subscribed">
Newsletter Signup
</label>
<span class="checkLabel">We will never spam or give away your information</span>
</fieldset>
Thank in advance!
Labels should contain at most one input element. Move the hidden input out of the label. Also, when an input is a descendant of a label, the for attribute is superfluous.
<fieldset>
<input type="hidden" value="0" name="user[is_subscribed]">
<label>
<input type="checkbox" value="1" name="user[is_subscribed]" id="user_is_subscribed">
Newsletter Signup
</label>
<span class="checkLabel">We will never spam or give away your information</span>
</fieldset>
Is this something that should just be ignored by the validator
No
(as it is seeminlgly correct)
It isn't
or should it be changed
Yes
to appease the w3c?
No. It should be changed because it is wrong, and browsers have to error correct to figure out which element the label is associated with.
The label isn't labeling the hidden input, move it elsewhere.