how to make rules in model using yii framework? - yii

In my application I have 1 field name contact_no,in that i want validation like +91-(999)-(846)-1062
If I'm writing like this
array('contact_no','pattern'=>'/^[0-9-()\s+]+$/'),
then i got error like
invalid validation rule. The rule must specify attributes to be validated and the validator name.
Then what to write in model.

the syntax is not correct,try this:
array('contact_no', 'match', 'pattern'=>'/^[0-9-()\s+]+$/'),

What you need is CRegularExpression with a 'pattern'. The validator name for this is 'match'.

Related

Dynamically create variable in Thymeleaf

I have an object in ${object} and a string in ${attribute}.
for example, the object may be a "user" and the attribute may be "email"
Now I want to access ${user.email}. However this needs to be dynamic as it should also work for ${article.name} and whatever else.
I tried following concatenations, but none of them worked
${__${object.attribute}__}
${__${object}__.__${attribute}__}
${__${object}__+'.'+__${attribute}__}
${${object}+'.'+${attribute}}
You can use this:
<div th:text="${object.__${attribute}__}"></div>
Assuming you have a model containing the following test data:
User user = new User("John", "john.jones#foo.com"); // user has name and email
model.put("object", user);
model.put("attribute", "email");
That will generate:
<div>john.foo#bar.com</div>
The only place where you need to use the preprocessor __${...)__ is the attribute variable.
After preprocessing has been performed, you will be left with the following Thymeleaf expression:
<div th:text="${object.email}"></div>
That will then be processed in the usual way to generate the HTML you need.

How to expand(odata-webapi) all the properties without passing $expand in query string in vb.net

I am using odata v5.7.0 for webapi(vb.net).I need all the properties of object should be expanded without using $expand property on the query string.
Ex: http://localhost:26209/ProductList?$expand=customers/products
into
http://localhost:26209/ProductList
Not sure why you would want to do that and there might not be a clear cut way to achive it.Still if you want to get all the products for a customer you will have to go one at a time by using something like this.
http://localhost:26209/ProductList/customers('customer_id')/products
Else if you are using OData 2 there is an EntitySet for every association, so you an directly query the products EntitySet and use $filter instead. The URL will look similar to
http://localhost:26209/Products?$filter=CustomerId eq *
You can check $filter conventions here

Insert data from DB to URL manager

I want URL manager to process URL with the company name with my CompanyController. To do so dynamically I should get company names from my database.
Now I have such rule (but it's not dynamic):
'<alias:(vector|karnasch|tecnomagnete|ruko|bds-maschinen|exact)>' => 'company/view',
(vector|karnasch|tecnomagnete|ruko|bds-maschinen|exact) --> data to this line I want to get from database.
I can get this data (manually establish connection to db), but maybe it's another more beautiful solution with help of Yii functional. Thanks!
You can always create your custom UrlRuleclass. If you only want to parse incoming URLs you can simply return false from the createUrl() method. In the parseUrl() method you query the DB for your company names and inspect if the current URL matches. If not, you simply return false again.
Well, you don't need to do this, you just have to define the right pattern, e.g. :
'contact' => 'contact/form',
// other rules should be set before this one
'<alias:[-\w]+>' => 'company/view',
http://www.yiiframework.com/doc/guide/1.1/en/topics.url#using-named-parameters

disable field on rules model yii

i am trying to disable a field on update rules models but i am having error.
i try like:
array('date', 'constraint', 'readOnly'=>true, 'on'=>'update'),
but i am having this error:
"include(constraint.php): failed to open stream: No such file or directory"
I can disable from view using htmloptions but i need do it from model because on update i need to disable more than 5 fields.
how could i do this?
thx in advance
You are declaring a rule with a validator that doesn't exist, so it's normal that you have an error:
array('date', 'constraint', 'readOnly'=>true, 'on'=>'update'),
This line is doing the following: apply the validator constraint on the date field on update scenario with param readOnly set to true.
The validator constraint doesn't exists has a built in functonnality in Yii so if you havn't created it then it doesn't exist!
Documentation:
Validation rules
Model Rules validation
Edit: In your form you could do something like:
<?php
echo $form->textField(
$model,
'email',
array('readonly'=>($model->scenario == 'update')? true : false)
);
?>
As you can see the readonly value will depend on the scenario.

How to get deeply nested errors to get to my REST API?

First, some background:
I have a Company model, a Project model and a Task model. A Project belongs to a company and a Task belongs_to a Project.
The Project model holds several attributes: company_id, date. These attributes uniquely identify a project
I am letting the users create a task by API by POSTing to a URL that contains the details necessary to identify the Project. For example:
POST /projects/<comnpany_name>/<date>/tasks/
In order to make life easier for the users, in case there is no project with the given details, I'd like to create the project on the fly by the given details, and then to create the task and assign it to the project.
...And my problem is:
When there is a problem to create the project, let's say that the company name is not valid, what is the right way to return the error message and communicate to the user?
I'll explain what I mean: I added a create_by_name_and_company_name method to the Project:
def self.create_by_name_and_company_name(name, company_name)
if company = Company.find_by_name(company_name)
project = Project.create(company_id: company.id,
name: name)
else # cannot create this project, trying to communicate the error
project = Project.new(name: name)
project.errors.add(:company, 'must have a valid name')
end
company
end
I was hoping that by returning an unsaved Company object, with errors set, will be a good way communicate the error (This is similar to how rails work when there's a validation error).
The problem is that when calling valid? on the company object, it removed the error I wrote there and adds the regular validation errors (in this case, company can't be blank).
And a bonus question...
And there is a conceptual problem as well: since I'm creating a model by providing parameters that are being used to create the actual attributes, they doesn't always map nicely to the errors[:attr] hash. In this case it is not so bad and I'm using the company field for the company name parameter, but I guess this can get messier when the parameters provided to the create method are less similar to the model attributes.
So what is the preferred approach to tackle that problem? Is there something basically wrong with that approach? if so, what is the preferred approach?
About overriding the default rails validation error message, you need to write your validation constraint like this:
validates_presence_of :name, :message => "must be a valid name"
I figure that it is best to avoid such nesting and stick to a shallower API.