I'm using symfony and I want to display errors when the username doesn't fit pattern, how can I do that?
{{ form_widget(form.username, {'attr':{'pattern': '[a-zA-Z]*'} }) }}
Try to add Assert to your property in the entity class.
Check out this for Asserts : http://symfony.com/doc/current/validation.html
And this one for regex : http://symfony.com/doc/current/reference/constraints/Regex.html
Related
I'm trying to get the data property value of my individual from an existing ontology. I'm using OWL API.
For example the name of my inidividual :
`
Set<OWLLiteral> literalA = John.getDataPropertyValues(name, ontology);
I've got this error :
> The method getDataPropertyValues(OWLDataPropertyExpression,
> OWLOntology) is undefined for the type OWLIndividua
`
This should get me : "John"
I've checked on this website the methods I can use to retrieve the information Interface OWLIndividual and it gave me getDataPropertyValues. Unfortunately, I can't make it work.
Any idea ?
Thanks for your help
I have a model
class StudentBasicInfo(models.Model):
usn = models.CharField(blank=False,max_length=10,unique=True,validators=[])
my usn will be in format [0-9][A-Za-z][A-Za-z][0-9][0-9][A-Z][A-Z][0-9][0-9][0-9]
I don't know how to write validation code
create a validator. I'd suggest a RegexValidator like so:
from django.core.validators import RegexValidator
...
class StudentBasicInfo(models.Model):
usn = models.CharField(blank=False,max_length=10,unique=True, validators=[RegexValidator(regex='[0-9][A-Za-z]{2}[0-9]{2}[A-Z]{2}[0-9]{3}', message='Error message goes here')])
I took the liberty of shortening your regex by combining groups that were together. If you want to make the error appear next to the field in the admin you'll have to overload a ModelForm.
I have a model wich checks a lot of things in beforeSave callback. I do not want to use cakephp validation system for that purpose because it is more simple for me in this case.
If the checking chain fails in somewhere I return false and no saving happens, It is the normal work. I want to give back informal error messages in the entity to use it in the controller and/or view.
How I can do that?
Example:
public function beforeSave($event, $entity, $options)
{
if($entity->isNew())
{
if(fail1) $entity->inserterrormessage('XYZ is missing');
if(fail2) $entity->inserterrormessage('Please check if...');
}
}
How I can do that?
Via the errors() method, or as of CakePHP 3.4 the setError() and setErrors() methods.
$entity->errors('propertyName', ['Message']);
$entity->setErrors(['propertyName' => ['Message']]);
$entity->setError('propertyName', ['Message']);
In case the error isn't related to an actual entity property, just choose a special name, like _generic. Alternatively create custom generic error storage functionality in a base entity or trait.
See also
Cookbook > Database Access & ORM > Entities > Validation Errors
API > \Cake\DataSource\EntityTrait::errors()
API > \Cake\DataSource\EntityTrait::setError()
API > \Cake\DataSource\EntityTrait::setErrors()
please see this code:
$this->addField('user_id')->refModel("User");
this search for the "name" field by default! but I want to connect the username field. how can we define which field to be the refrence?
in Model_User
$this->addField('name','username');
See this method:
https://github.com/atk4/atk4/blob/master/lib/SQL/Model.php#L95
but this method is obsolete and you should use hasOne instead of it.
function hasOne($model,$our_field=null,$display_field=null,$as_field=null)
I am using cakephp. I have a form with an element array.
For ex:-
<textarea name="data[User][0][description]>
<textarea name="data[User][1][description]>
From the controller, I need to invalidate (manually) the array field if it is empty and need to show errors to the respective field.
What is the correct syntax for invalidating the field if it an element array ?
I know, the following will work for single element . How it will be for an element array ?
$this->User->invalidate("description");
Unfortunately you cannot invalidate the field with that function.
But what invalidate() does?
function invalidate($field, $value = true) {
if (!is_array($this->validationErrors)) {
$this->validationErrors = array();
}
$this->validationErrors[$field] = $value;
}
It just set validationErrors of the model.
So, you can do following in your Controller (but I also appeal you to move that validation in the Model):
$this->User->validationErrors[1]['description'] = 'Your error message';
The following code will invalidate the second description in the list.
HTH
You can type in view:
<?php
echo $this->Form->error("User.1.description");
?>
Thanks Nik,
your answer helped me, but halfway, because my problem was with a compound field by other subfields.
account_number {
bank_code,
bank_office,
check_digit,
account
}
In this case if we need put in validation error in one subfield, this is the solution:
$this->Model->validationErrors['account_number']['bank_code'][0] = 'Your message error';
I hope this help someone.
Regards.
I had similar problem, since it was for admin panel I showed error message on the first level of field i.e. for this portion only.
If you are validation on controller, just create an array of error with field name and error message, set it in controller and display message if in_array($field, $withErrorArray) in view.