UsicUserForm.class.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. class UsicUserForm extends sfForm
  3. {
  4. /* public function __construct($defaults = array(), $options = array(), $CSRFSecret = null)
  5. {
  6. return parent::__construct($defaults, $options, $CSRFSecret);
  7. }
  8. */
  9. public function configure()
  10. {
  11. sfWidget::setCharset('UTF-8');
  12. date(m)>8?$year=date(Y):$year=date(Y)-1;
  13. $years = range(1997,$year);
  14. $this->setWidgets(array('surname' => new sfWidgetFormInput(array(),array('maxlength' => 20)),
  15. 'name' => new sfWidgetFormInput(array(),array('maxlength' => 20)),
  16. 'middle_name' => new sfWidgetFormInput(array(),array('maxlength' => 20)),
  17. 'login' => new sfWidgetFormInput(array(),array('maxlength' => 20)),
  18. 'password' => new sfWidgetFormInputPassword(array(), array('maxlength' => 20)),
  19. 'confirm_password' => new sfWidgetFormInputPassword(array(), array('maxlength' => 20)),
  20. 'entering_year'=>new sfWidgetFormChoice(array('choices' => array_combine($years, $years))),
  21. 'status' => new sfWidgetFormPropelChoice(array('model'=> 'ProfessionsTable', 'method'=>'getName')),
  22. 'faculty' => new sfWidgetFormPropelChoice(array('model'=> 'ClassTable', 'method'=>'getFaculty', 'key_method'=>'getFaculty')),
  23. // custom for id=>profession foreach faculty
  24. 'profession' => new sfWidgetFormChoice(array('choices' => array())),
  25. // 'profession' => new sfWidgetFormPropelChoice(array('model'=> 'ClassTable', 'method'=>'getName')),
  26. // select id=>name
  27. 'reader_card' => new sfWidgetFormInput(array(),array('maxlength' => 5)),
  28. 'student_card' => new sfWidgetFormInput(array(),array('maxlength' => 20)),
  29. 'passport' => new sfWidgetFormInput(array(),array('maxlength' => 20))
  30. ));
  31. $this->widgetSchema->setNameFormat('user[%s]');
  32. $this->setValidators(array(
  33. 'surname' => new sfValidatorRegex(array('required' => true, 'pattern' => "/^[А-ЯІЄЇҐ]([а-яієїґ']+)$/u"),
  34. array('invalid'=>'surname must contain only Ukranian letters and start with capital')),
  35. 'name' => new sfValidatorRegex(array('required' => true, 'pattern' => "/^[А-ЯІЄЇҐ]([а-яієїґ']+)$/u"),
  36. array('invalid'=>'name must contain only Ukranian letters and start with capital')),
  37. 'middle_name' => new sfValidatorRegex(array('required' => true, 'pattern' => "/^[А-ЯІЄЇҐ]([а-яієїґ']+)$/u"),
  38. array('invalid'=>'middle name must contain only Ukranian letters and start with capital')),
  39. 'login' => new sfValidatorRegex(array('required' => true, 'pattern' => "/^[a-z][0-9a-z_]*$/"),
  40. array('invalid'=>'login can contain only small latin, numbers, symbol _ and must start with letter')),
  41. 'password' => new sfValidatorRegex(array('required' => true, 'pattern' => "/[a-z0-9\\-=;',.{}\/]/i"),
  42. array('invalid' => 'password must not be empty or contain cyrillic symbols')),
  43. 'confirm_password' => new sfValidatorRegex(array('required' => true, 'pattern' => "/[a-z0-9\\-=;',.{}\/]/i"),
  44. array('invalid' => '')),
  45. 'entering_year' => new sfValidatorChoice(array('required'=>true,'choices'=>$years)),
  46. 'status' => new sfValidatorPropelChoice(array('model'=>'ProfessionsTable')),
  47. 'faculty' => new sfValidatorPropelChoice(array('model'=>'ClassTable', 'column'=>'faculty')),
  48. 'profession' => new sfValidatorPropelChoice(array('model'=>'ClassTable')),
  49. 'reader_card' => new sfValidatorRegex(array('required' => false, 'pattern' => "/^[0-9]{5,5}$/"),
  50. array('invalid'=>'reader card must contain 5 numbers')),
  51. 'student_card' => new sfValidatorRegex(array('required' => false, 'pattern' => "/^[А-ЯІЄЇҐ]{2}([0-9]{8})$/u"),
  52. array('invalid'=>'student card must contain 2 capital ukranian letters and >=8 numbers')),
  53. 'passport' => new sfValidatorString(array('required' => false, 'trim' => true, 'min_length'=>5),
  54. array('min_length' => '%value% should be at least %min_length% characters')),
  55. ));
  56. $this->mergePostValidator( new sfValidatorSchemaCompare('confirm_password', '==', 'password'), array('throw_global_error' => false),
  57. array('invalid' => 'Your passwords don\'t match') );
  58. $this->mergePostValidator( new sfValidatorCallback( array('callback' => array($this,'admitAdding')) ) );
  59. }
  60. public function admitAdding($validator, $values)
  61. {
  62. $fields = ProfessionsTablePeer::getAddingFields($values['status']);
  63. foreach($fields as $key=>$field)
  64. {
  65. if (!empty($values[$field]))
  66. return $values;
  67. }
  68. $ve = new sfValidatorError($validator,$fields['error']);
  69. throw new sfValidatorErrorSchema($validator, array('status' => $ve));
  70. // throw new sfValidatorError($validator,$fields['error']);
  71. }
  72. // not used
  73. public function validForFind($fields)
  74. {
  75. if ($this->isBound())
  76. {
  77. foreach ($fields as $field)
  78. {
  79. if (!$this->getErrorSchema()->offsetExists($field)) {
  80. return true;
  81. }
  82. }
  83. }
  84. else return false;
  85. }
  86. public static $findFields = array('login');
  87. public static function createPasswordForm()
  88. {
  89. $userForm = new UsicUserForm();
  90. $passForm = new sfForm();
  91. $passForm->setWidgetSchema(new sfWidgetFormSchema(array('password'=>$userForm->getWidget('password'),
  92. 'confirm_password' => $userForm->getWidget('confirm_password'))) );
  93. //? $passForm->setValidatorSchema(new sfValidatorSchema(array($userForm->getValidator('password'), $userForm->getValidator('confirm_password'))) );
  94. $passForm->setValidator('password', new sfValidatorRegex(array('required' => true, 'pattern' => "/[a-z0-9\\-=;',.{}\/]/i"),
  95. array('invalid' => 'password must not be empty or contain cyrillic symbols')));
  96. $passForm->setValidator('confirm_password', new sfValidatorRegex(array('required' => true, 'pattern' => "/[a-z0-9\\-=;',.{}\/]/i"),
  97. array('invalid' => '')));
  98. $passForm->getWidgetSchema()->setNameFormat('pass[%s]');
  99. $passForm->mergePostValidator( new sfValidatorSchemaCompare('confirm_password', '==', 'password'), array('throw_global_error' => false),
  100. array('invalid' => 'Your passwords don\'t match') );
  101. return $passForm;
  102. }
  103. public function isNew()
  104. {
  105. return $this->isNew;
  106. }
  107. public function setNewFalse()
  108. {
  109. $this->isNew = 0;
  110. $this->validatorSchema['password']->setOption('required', false);
  111. $this->validatorSchema['confirm_password']->setOption('required', false);
  112. if (isset($this->defaults['profession']))
  113. {
  114. $this->setProfession($this->defaults['profession']);
  115. }
  116. }
  117. public function setProfession($def)
  118. {
  119. $this_prof = ClassTablePeer::retrieveByPk($def);
  120. $this->setDefault('faculty', $this_prof->getFaculty());
  121. $c = new Criteria();
  122. $c->add(ClassTablePeer::FACULTY, $this_prof->getFaculty());
  123. $pr = ClassTablePeer::doSelect($c);
  124. $professions = array();
  125. foreach ($pr as $p)
  126. {
  127. $professions[$p->getId()] = $p->getName();
  128. }
  129. $this->widgetSchema['profession']->addOption('choices', $professions);
  130. $this->setDefault('profession', $this_prof->getId() );
  131. }
  132. protected $isNew = 1;
  133. }