tom819
(The Twongs)
February 25, 2013, 8:38pm
1
Hey all:
I’m currently building a form builder in Yii where the front end consists of a drag and drop interface. The user can drag as many text, text area, check lists, and radio lists as they desire. The interface would allow the user to make any field required. I’ve built most of the system until I got the the validation portion of my project. Somehow I thought I could use the scenarios but I cant figure out how to dynamically bind a rule to a input field that I did not name. Any suggestions on how I could approach this?
Thanks
le_top
(Ext Yiiframework Com)
February 25, 2013, 9:42pm
2
I suppose that you are using a custom FormModel for that?!
If so, why not create the rules() dynamically based on other properties set somewhere for the model (the same way as in which you determine the form elements).
lubosdz
(Lubosdz)
February 25, 2013, 9:49pm
3
First, you need to create a CForm formular.
Then you define properties in that formular (= your fields).
Each property’s name corresponds to your field name.
Then you can define any validator CForm properties.
You cannot attach validator to a field that has no name, of course.
http://www.yiiframework.com/wiki/56/reference-model-rules-validation/
http://www.yiiframework.com/wiki/266/understanding-scenarios/
tom819
(The Twongs)
February 25, 2013, 10:36pm
4
Thanks for the responses!
The issue here is that the end user can rename the name attribute to any value(s) they wish.
Maybe I’m a bit confused but arent the validators bound to the input via attribute name?
Could I validate the input field through another attribute other than name?
The workflow of what I’m trying to do here is like so:
End user creates a form
Save form
From another module render (partial) the saved form
Use the validation rules established in step 1.
Is that possible?
Thanks!
dniznick
(Dniznick)
February 25, 2013, 11:00pm
5
Hint:
Maybe let the user customize the label name, not the attribute field itself…
tom819
(The Twongs)
February 25, 2013, 11:28pm
6
Okay I tried to wrap my brain around that but I’m not sure how this could help in the following scenario:
Form1: Input name: First Name (not required)
Form2: Input name: First Name (required)
When I render Form 1 the person filling out the form should not be required (via the model’s rule)
whereas the same person filling out form 2 would get Yii’s error message if the field was left blank.
How would I be able to differentiate the rules on the same name in the model? (maybe I’m just not seeing it)
Thanks again for all your help!
look at the yii-user extension. It has dynamic validation rules for the custom profile fields…might help…
traprajith
(Traprajith)
February 26, 2013, 6:04am
8
Okay I tried to wrap my brain around that but I’m not sure how this could help in the following scenario:
Form1: Input name: First Name (not required)
Form2: Input name: First Name (required)
When I render Form 1 the person filling out the form should not be required (via the model’s rule)
whereas the same person filling out form 2 would get Yii’s error message if the field was left blank.
How would I be able to differentiate the rules on the same name in the model? (maybe I’m just not seeing it)
Thanks again for all your help!
just create 2 models for same table with different rules… or
like this , by checking controller action id
Eg:-
public function rules() {
return Yii::app()->controller->id == 'recovery' ? array(
array('password, verifyPassword', 'required'),
array('password, verifyPassword', 'length', 'max'=>128, 'min' => 4,'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => UserModule::t("Retype Password is incorrect.")),
) : array(
array('oldPassword, password, verifyPassword', 'required'),
array('oldPassword, password, verifyPassword', 'length', 'max'=>128, 'min' => 4,'message' => UserModule::t("Incorrect password (minimal length 4 symbols).")),
array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => UserModule::t("Retype Password is incorrect.")),
array('oldPassword', 'verifyOldPassword'),
);
}