Hi everyone, I’m new here and this is my first crack at using yii.
I have a form with 4 input fields: firstname, lastname, street and city_state. I also have set up a number of defaults in my model script below to show on the form before users enter data.
class PeopleForm extends CFormModel
{
public $firstname = 'First Name*';
public $lastname = 'Last Name*';
public $street = 'Address';
public $city_state = 'City/State';
public function rules()
{
return array(array('firstname, lastname', 'required'));
}
....................
As seen above, I use both ajax and server side validation on my controller. How do I modifiy the rules so that when a user does not enter any text it retruns an error. As it currently is, the form submits the default values for the fields and it passes validation.
Thanks 1cichy. I do set the attributes…forgot to type it in here. It doesn’t make a difference. The validate function treats the submitted default values as actual values and does not return an error.
Thanks softark. I was concerned about the attribute labels, but that is not the problem I am currrently facing. Nevertheless, I appreciate your introducing me to atttribute labels. It does make my error message display better.
The problem I have is similar to this thread but it was not resolved to my satisfaction. My problem is that when the default value on the input box for firstname for instance, is submitted, the validate function accepts it as a valid value. I want the validate function to reject default values (like ‘First Name’, ‘Last Name’, etc) and to return an error message saying that the particular input field is still required.
I think it’s a trade off. Whether you want to make use of “default values” or you want to make use of “require” validator.
It might not be appropriate to use default values for input prompts … I would use "hint" texts instead.
But if you do want to set the default values and treat them as empty, you may write a custom validator … but I won’t thumb up for it. It could be cumbersome, and would not work in client-side in spite of the cost you pay.